USACO2.4.1 The Tamworth Two (ttwo)

来源:互联网 发布:surface rt ubuntu 编辑:程序博客网 时间:2024/06/05 02:48

记录方向和坐标,直接模拟,直到相遇。如何判断永远不相遇呢?

对于牛或者人状态只有(10*10*4)400种,所以对于整个过程最多有(400*400)160000种状态。

也就是说,如果超过160000步,那么肯定会出现有的状态出现了2次以上,那么就肯定是一个死循环,永远不会相遇。



/*ID:xsy97051LANG:C++TASK:ttwo*/#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;char dat[11][11],go[4][2]={{-1,0},{0,1},{1,0},{0,-1}};int main(){freopen("ttwo.in","r",stdin);freopen("ttwo.out","w",stdout);memset(dat,0,sizeof(dat)); int cx,cy,fx,fy,dc=0,df=0;        for(int i=1;i<=10;i++)        for(int j=1;j<=10;j++)        {       char c;            cin>>c;            if(c=='*') { dat[i][j]=1; continue;}            if(c=='F') { fx=i; fy=j;}            if(c=='C') { cx=i; cy=j;}        }        int time=0,x,y;    while(time++<160000)    {        if((fx==cx)&&(fy==cy)) break;                x=fx+go[df][0];        y=fy+go[df][1];        if((dat[x][y]==1)||(x<1)||(x>10)||(y<1)||(y>10)) df=(df+1)%4;        else     {                fx=x;                fy=y;            }        x=cx+go[dc][0];        y=cy+go[dc][1];        if((dat[x][y]==1)||(x<1)||(x>10)||(y<1)||(y>10))    dc=(dc+1)%4;        else     {                cx=x;                cy=y;            }    }     if(time<=160000) cout<<time-1<<endl;    else cout<<0<<endl;return 0;}


0 0