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函數傳參好像不會改變傳入對象,要用*取地址來傳,於是恍然大悟。
傳入的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????