HDU - 4740 The Donkey of Gui Zhou

The Donkey of Gui Zhou

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 413    Accepted Submission(s): 165


Problem Description
There was no donkey in the province of Gui Zhou, China. A trouble maker shipped one and put it in the forest which could be considered as an N×N grid. The coordinates of the up-left cell is (0,0) , the down-right cell is (N-1,N-1) and the cell below the up-left cell is (1,0)..... A 4×4 grid is shown below:

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.
 

 

Input
There are several test cases.

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)
 

 

Output
For each test case, if the donkey and the tiger would meet in a cell, print the coordinate of the cell where they meet first time. If they would never meet, print -1 instead.
 

 

Sample Input
2 0 0 0 0 1 2 4 0 1 0 3 2 0 0
 

 

Sample Output
-1 1 3
 

 

Source
 

 

Recommend
liuyiding
 
纯模拟,注意条件判断。
 
 1 #include<cstdio>
 2 #include<cstring>
 3 bool mapd[1005][1005],mapt[1005][1005];
 4 int N;
 5 const int dir[]={ 0,+1, 0,-1};
 6 const int dic[]={+1, 0,-1, 0};
 7 bool ok(int r,int c){
 8     return (0<=r && r<N && 0<=c && c<N);
 9 }
10 bool turnright(int&r,int&c,int&D){  //donkey
11     D=(D+1)%4;
12     int tr=r+dir[D];
13     int tc=c+dic[D];
14     if(ok(tr,tc) && !mapd[tr][tc]){
15         r=tr,c=tc,mapd[tr][tc]=true;
16         return true;
17     }
18     else return false;
19 }
20 bool turnleft(int&r,int&c,int&D){  //tiger
21     D=(D+3)%4;
22     int tr=r+dir[D];
23     int tc=c+dic[D];
24     if(ok(tr,tc) && !mapt[tr][tc]){
25         r=tr,c=tc,mapt[tr][tc]=true;
26         return true;
27     }
28     else return false;
29 }
30 int main(){
31     int Rd,Cd,Rt,Ct,Dd,Dt,rd,cd,rt,ct,ans,ansR,ansC;
32     bool stayd,stayt;
33     while(scanf("%d",&N),N){
34         memset(mapd,false,sizeof mapd);
35         memset(mapt,false,sizeof mapt);
36         scanf("%d%d%d",&Rd,&Cd,&Dd);
37         scanf("%d%d%d",&Rt,&Ct,&Dt);
38         if(Rd==Rt && Cd==Ct){
39             printf("%d %d\n",Rd,Cd);
40             continue;
41         }
42         rd=Rd,cd=Cd,rt=Rt,ct=Ct,stayd=stayt=false,ans=0;
43         mapd[rd][cd]=mapt[rt][ct]=true;
44         while(1){
45             //donkey
46             if(!stayd){
47                 if(ok(rd+dir[Dd],cd+dic[Dd]) && !mapd[rd+dir[Dd]][cd+dic[Dd]])
48                     rd+=dir[Dd],
49                     cd+=dic[Dd],
50                     mapd[rd][cd]=true;
51                 else if(!turnright(rd,cd,Dd))stayd=true;
52             }
53             //tiger
54             if(!stayt){
55                 if(ok(rt+dir[Dt],ct+dic[Dt]) && !mapt[rt+dir[Dt]][ct+dic[Dt]])
56                     rt+=dir[Dt],
57                     ct+=dic[Dt],
58                     mapt[rt][ct]=true;                
59                 else if(!turnleft(rt,ct,Dt))stayt=true;    
60             }
61             if(rd==rt && cd==ct){
62                 ans=1;
63                 ansR=rd;
64                 ansC=cd;
65                 break;
66             }else{
67                 if(stayd && stayt){
68                     ans=-1;
69                     break;
70                 }
71             }
72         }
73         if(ans==1)printf("%d %d\n",ansR,ansC);
74         else printf("-1\n");
75     }
76     return 0;
77 }

 

posted @ 2013-09-16 21:16  刚铎-赏金猎人  阅读(296)  评论(0编辑  收藏  举报