模拟题,状态只有10*10*4=400种,两个人最多400*400=160000种,也就是说,如果超过160000步,那么肯定会出现有的状态出现了2次以上,那么就肯定是一个死循环,永远不会相遇。
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
freopen("ttwo.in","r",stdin);
freopen("ttwo.out","w",stdout);
int map[11][11],fx,fy,cx,cy,cnt=0;
int dic[][2]={-1,0,0,1,1,0,0,-1};
char c;
memset(map,0,sizeof(map));
for(int i=1;i<=10;i++)
for(int j=1;j<=10;j++)
{
cin>>c;
if(c=='*')
map[i][j]=1;
else if(c=='F')
{
fx=i;
fy=j;
}
else if(c=='C')
{
cx=i;
cy=j;
}
}
int d1=0,d2=0,x,y;
while(1)
{
if(cx==fx&&cy==fy)
break;
x=fx+dic[d1][0];
y=fy+dic[d1][1];
if(map[x][y]||x>10||x<1||y>10||y<1)
d1=(d1+1)%4;// 转向
else
{
fx=x;
fy=y;
}
x=cx+dic[d2][0];
y=cy+dic[d2][1];
if(map[x][y]||x>10||x<1||y>10||y<1)
d2=(d2+1)%4;
else
{
cx=x;
cy=y;
}
cnt++;
if(cnt>160000)
break;
}
if(cnt>160000)
cout<<"0"<<endl;
else
cout<<cnt<<endl;
return 0;
}