P1518 [USACO2.4] 两只塔姆沃斯牛 The Tamworth Two

其實確實就一道模擬題,一開始覺得是搜索,後面看了一段時間題目知道了模擬,就開始瘋狂模擬,代碼不斷地優化可讀性和邏輯,感覺很不錯,但是輸出一直是零。

這是當時代碼

#include<iostream> #include<algorithm> using namespace std; struct moveThings { int face = 1; int x, y; }; moveThings john,cows; int dirX[5] = {0,-1,0,1,0}; int dirY[5] = {0,0,1,0,-1}; char map[12][12]; void read() { for(int i = 1; i <= 10; i++) { for(int j = 1; j <= 10; j++) { cin>>map[i][j]; if(map[i][j] == 'F') { john.x = i, john.y = j; } if(map[i][j] == 'C') { cows.x = i, cows.y = j; } } } } bool isMiss(moveThings a, moveThings b) { return a.x == b.x && a.y == b.y; } bool isValid(int x, int y) { return x >= 1 && x <= 10 && y >= 1 && y <= 10 && map[x][y] != '*'; } void setState(moveThings T) { int tx = T.x + dirX[T.face], ty = T.y + dirY[T.face]; if(isValid(tx, ty)) { T.x = tx;T.y = ty; } else { T.face = T.face + 1 <= 4 ? T.face + 1 : 1; } } void getAns() { int count = 0; while(++count <= 5000) { setState(cows); setState(john); if(isMiss(cows,john)) { cout<<count; return; } } cout<<0; return; } int main() { read(); getAns(); return 0; }

當真算是百思不得其解,調試了一遍發現傳入對象cows和john的x,y坐標從未動過。

一開始以爲是判斷函數的問題,後面突然想到之前康復訓練聼CS50X上面提到C函數傳參好像不會改變傳入對象,要用*取地址來傳,於是恍然大悟。

1|0傳入的COWS和JOHN未曾變過,居然栽在了語法上

這是修改後的代碼

#include<iostream> #include<algorithm> using namespace std; struct moveThings { int face = 1; int x, y; }; moveThings john,cows; int dirX[5] = {0,-1,0,1,0}; int dirY[5] = {0,0,1,0,-1}; char map[12][12]; void read() { for(int i = 1; i <= 10; i++) { for(int j = 1; j <= 10; j++) { cin>>map[i][j]; if(map[i][j] == 'F') { john.x = i, john.y = j; } if(map[i][j] == 'C') { cows.x = i, cows.y = j; } } } } bool isMiss(moveThings a, moveThings b) { return a.x == b.x && a.y == b.y; } bool isValid(int x, int y) { return x >= 1 && x <= 10 && y >= 1 && y <= 10 && map[x][y] != '*'; } void setState(moveThings* T) { int tx = T->x + dirX[T->face], ty = T->y + dirY[T->face]; if(isValid(tx, ty)) { T->x = tx;T->y = ty; } else { T->face = T->face + 1 <= 4 ? T->face + 1 : 1; } } void getAns() { int count = 0; while(++count <= 5000) { setState(&cows); setState(&john); if(isMiss(cows,john)) { cout<<count; return; } } cout<<0; return; } int main() { read(); getAns(); return 0; }

AC.

題外話:我看了看我一九年的代碼,靠了好尬啊啊啊

/* luoguID:Kdlyh Blog:lougu.org/blog/fivehours/ Daring:Chtholly */ #include <cstdio> #include <iostream> const int N = 12; const int dirx[]={-1,0,1,0}, diry[]={0,1,0,-1}; char g[N][N]; int nx,ny; int fx,fy; int fdir; int ndir; bool flag1,flag2; int step; bool in_side(int x,int y) { return (x>=1 && x<=10 && y>=1 && y<=10); } void Input() { std::ios::sync_with_stdio(false); for(int i=1; i<=10; i++) { for(int j=1; j<=10; j++) { std::cin>>g[i][j]; if(g[i][j] == 'F') { fx=i,fy=j; } if(g[i][j] == 'C') { nx=i,ny=j; } } } } void Solve() { while(fx != nx || fy != ny) { flag1=flag2=false; //printf("step=%d x=%d y=%d\n",step,nx,ny); step++; if(!in_side(fx+dirx[fdir],fy+diry[fdir]) || g[fx+dirx[fdir]][fy+diry[fdir]] == '*') { fdir=(fdir+1)%4; } else { fx+=dirx[fdir],fy+=diry[fdir]; } if(!in_side(nx+dirx[ndir],ny+diry[ndir]) || g[nx+dirx[ndir]][ny+diry[ndir]] == '*') { ndir=(ndir+1)%4; } else { nx+=dirx[ndir],ny+=diry[ndir]; } if(step > 1e7) { printf("0"); return ; } } std::cout<<step; } int main(void) { Input(); Solve(); return 0; }

所以説爲什麽要標明Daring:Chtholly????


__EOF__

本文作者Kdlyh
本文链接https://www.cnblogs.com/kdlyh/p/17776976.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   加固文明幻景  阅读(5)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
点击右上角即可分享
微信分享提示