// usaco-The Tamworth Two /* * 类型:模拟 * 对于人 和 牛 各有10*10*4=400种状态,所以总共有400*400=160000种状态 * 模拟执行160000次,判断是否达到最终状态 * 用sign[i][j]记录是否达到该状态 i,j分别为人 和 牛的状态表示 * !!!当同一状态出现两次时,则这两种状态之间出现循环 * 在出现循环之前如果没有达到同一 点 ,则永远达不到 */ #include <iostream> #include <fstream> #include <algorithm> #include <queue> #include <stack> #include <map> #include <string> #include <cmath> #include <cstring> #include <cstdlib> using namespace std; #define MAXN 402 #define INF 0x7f7f7f7f bool sign[MAXN][MAXN]; int fx,fy,cx,cy; int dir[4][2]={-1,0,0,1,1,0,0,-1}; struct node{ int x,y; }sf,sc; string str[10]; ifstream fin("ttwo.in"); ofstream fout("ttwo.out"); void init() { int i,j; for(i=0; i!=10; ++i) for(fin>>str[i],j=0; j!=str[i].length(); ++j){ if(str[i][j]=='F'){ fx=i; fy=j; } else if(str[i][j]=='C'){ cx=i; cy=j; } } } bool is_con(int x,int y) { if(x>=0 && y>=0 && x<10 && y<10 && str[x][y]!='*') return true; return false; } void proc() { int fxx,fyy,cxx,cyy; int dic=0; int dif=0; int T=160000; sign[(fx*10+fy)*4+dif][(cx*10+cy)*4+dic]=true; while(T--){ fxx=fx+dir[dif][0]; fyy=fy+dir[dif][1]; cxx=cx+dir[dic][0]; cyy=cy+dir[dic][1]; if(is_con(fxx,fyy)){ fx=fxx; fy=fyy; } else dif=(dif+1)%4; if(is_con(cxx,cyy)){ cx=cxx; cy=cyy; } else dic=(dic+1)%4; if(fx==cx && fy==cy){ fout<<160000-T<<endl; return ; } if(sign[(fx*10+fy)*4+dif][(cx*10+cy)*4+dic]){ fout<<"0"<<endl; return ; } else sign[(fx*10+fy)*4+dif][(cx*10+cy)*4+dic]=true; } } int main(void) { init(); proc(); return 0; }