john和牛在二维的图里面跑,john要抓住牛。
牛和john在遇到墙或者图的边界时都会花一单位的时间顺时针转动90度。否则花一单位时间往前走一格,当牛和john走到同一个格子中时,john抓住了牛。
求john抓住牛要多久。抓不住时输出0.
一开始想了很久如何判断抓不住的情况,相通了之后还是很简单的。
john和牛都走到了以前走到位置,且保持着以前相同的方向。则说明陷入了循环(再也抓不住了)
/*
ID: modengd1
PROG: ttwo
LANG: C++
*/
#include <iostream>
#include <stdio.h>
#include <memory.h>
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
using namespace std;
bool vis[10][10][4][10][10][4];
char input[10][10];
struct position
{
pair<int,int> P;
int dir;
};
void Move(position& x)
{
//装墙或者出地图
int nextX=x.P.first+dx[x.dir],nextY=x.P.second+dy[x.dir];
if(input[nextX][nextY]=='*'||nextX<0||nextX>9||nextY<0||nextY>9)
{
x.dir=(x.dir+1)%4;
return ;
}
//往前走
x.P.first=nextX;
x.P.second=nextY;
}
int main()
{
freopen("ttwo.in","r",stdin);
freopen("ttwo.out","w",stdout);
position john,cow;
memset(vis,false,sizeof(vis));
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
scanf("%c",&input[i][j]);
if(input[i][j]=='F')
{
john.P.first=i;
john.P.second=j;
john.dir=0;
}
else if(input[i][j]=='C')
{
cow.P.first=i;
cow.P.second=j;
cow.dir=0;
}
}
getchar();
}
int ans=0;
position& J=john;
position& C=cow;
while(true)
{
if(john.P.first==cow.P.first&&john.P.second==cow.P.second)
break;
if(vis[john.P.first][john.P.second][john.dir][cow.P.first][cow.P.second][cow.dir])
{
ans=0;
break;
}
vis[john.P.first][john.P.second][john.dir][cow.P.first][cow.P.second][cow.dir]=true;
Move(J);
Move(C);
ans++;
}
cout<<ans<<endl;
return 0;
}