The Donkey of Gui Zhou
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 389 Accepted Submission(s): 153
The donkey lived happily until it saw a tiger far away. The donkey had never seen a tiger ,and the tiger had never seen a donkey. Both of them were frightened and wanted to escape from each other. So they started running fast. Because they were scared, they were running in a way that didn't make any sense. Each step they moved to the next cell in their running direction, but they couldn't get out of the forest. And because they both wanted to go to new places, the donkey would never stepped into a cell which had already been visited by itself, and the tiger acted the same way. Both the donkey and the tiger ran in a random direction at the beginning and they always had the same speed. They would not change their directions until they couldn't run straight ahead any more. If they couldn't go ahead any more ,they changed their directions immediately. When changing direction, the donkey always turned right and the tiger always turned left. If they made a turn and still couldn't go ahead, they would stop running and stayed where they were, without trying to make another turn. Now given their starting positions and directions, please count whether they would meet in a cell.
In each test case:
First line is an integer N, meaning that the forest is a N×N grid.
The second line contains three integers R, C and D, meaning that the donkey is in the cell (R,C) when they started running, and it's original direction is D. D can be 0, 1, 2 or 3. 0 means east, 1 means south , 2 means west, and 3 means north.
The third line has the same format and meaning as the second line, but it is for the tiger.
The input ends with N = 0. ( 2 <= N <= 1000, 0 <= R, C < N)
0 0 0
0 1 2
4
0 1 0
3 2 0
0
1 3
感想
:现在才发现当时自己把题目读复杂了,怪不得自己搞了半天最后还是WA了。题意是王道,题意理解错了都是扯淡。好在我看见这个模拟水题之后想到了以前做的那两个兔子的模拟,和吉吉说了下,吉吉后来拿了一血,虽然不早,但毕竟是一血。
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std; int dir[4][2]= //往东南西北四个方向
{
{0,1},{1,0},{0,-1},{-1,0}
};
int visidon[1005][1005];
int visitig[1005][1005]; int main()
{
int n,i,j;
int donx,dony,tigx,tigy,pdon,ptig;
while(scanf("%d",&n)&&n)
{
memset(visidon,0,sizeof(visidon));
memset(visitig,0,sizeof(visitig));
scanf("%d%d%d",&donx,&dony,&pdon); //驴子的坐标与方向
scanf("%d%d%d",&tigx,&tigy,&ptig); //老虎的坐标与方向
visidon[donx][dony]=1;
visitig[tigx][tigy]=1;
int flag=0;
int fla1=0,fla2=0;//代表驴子和老虎不能转向
if(donx==tigx&&dony==tigy) //开始就在一起,直接输出
{
cout<<donx<<" "<<dony<<endl;
continue;
}
else
{
while(1)
{
if(fla1&&fla2)
{
break;
}
int cx1,cy1,cx2,cy2;
cx1=donx,cy1=dony,cx2=tigx,cy2=tigy;
if(!fla1) //驴子还可以走
{
cx1=donx+dir[pdon][0];
cy1=dony+dir[pdon][1];
}
if(!fla2) //老虎还可以走
{
cx2=tigx+dir[ptig][0];
cy2=tigy+dir[ptig][1];
} if(!fla1) //驴子还可以走
{
if(cx1>=0&&cx1<n&&cy1>=0&&cy1<n&&!visidon[cx1][cy1]) //可以沿着方向走
{
donx=donx+dir[pdon][0];
dony=dony+dir[pdon][1];
visidon[donx][dony]=1;
//cout<<"驴子:"<<donx<<" "<<dony<<endl;
}
else //转了一次方向
{
pdon=(pdon+1+4)%4;
cx1=donx+dir[pdon][0];
cy1=dony+dir[pdon][1];
if(cx1>=0&&cx1<n&&cy1>=0&&cy1<n&&!visidon[cx1][cy1]) //可以沿着方向走
{
donx=donx+dir[pdon][0];
dony=dony+dir[pdon][1];
visidon[donx][dony]=1;
//cout<<"驴子:"<<donx<<" "<<dony<<endl;
}
else
fla1=1;
//转了一次方向还是不能走,那就停下来
}
} if(!fla2) //老虎还可以走
{
if(cx2>=0&&cx2<n&&cy2>=0&&cy2<n&&!visitig[cx2][cy2])
{
tigx=tigx+dir[ptig][0];
tigy=tigy+dir[ptig][1];
visitig[tigx][tigy]=1;
//cout<<"老虎:"<<tigx<<" "<<tigy<<endl;
}
else
{
ptig=(ptig-1+4)%4;
cx2=tigx+dir[ptig][0];
cy2=tigy+dir[ptig][1];
if(cx2>=0&&cx2<n&&cy2>=0&&cy2<n&&!visitig[cx2][cy2])
{
tigx=tigx+dir[ptig][0];
tigy=tigy+dir[ptig][1];
visitig[tigx][tigy]=1;
//cout<<"老虎:"<<tigx<<" "<<tigy<<endl;
}
else
fla2=1;
}
}
if(donx==tigx&&dony==tigy) //说明撞在一起
{
flag=1;
break;
}
}
if(!flag)
puts("-1");
else
{
printf("%d %d\n",donx,dony);
}
}
}
return 0;
}