HDOJ 4740 - The Donkey of Gui Zhou 模拟

               题意:

                       A,B在一个N*N的格子中走..若A碰到了边界或者自己走过的位置..就会向右转后再向前走..而B碰到了边界或者自己走过的位置..就会向左转后再向前走..但是A,B在一次转向后又需要转向则停下来了..每个单位时间..A,B都向前走一格(或者停住了)...问A,B能否相遇..在哪个点相遇..

               题解:

                       直接模拟就好..注意的是范围是0~N-1而不是1~N


Program:

#include<iostream>
#include<stack>
#include<queue>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<cmath>
#define ll long long
#define oo 1000000007
#define eps 1e-5
#define MAXN 3005
#define MAXM 3000005
using namespace std;  
int face[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
bool A[1005][1005][2],f1,f2;
int main()
{ 
      int n,R1,C1,D1,R2,C2,D2,R,C;    
      while (~scanf("%d",&n) && n)
      {
               memset(A,false,sizeof(A));
               scanf("%d%d%d",&R1,&C1,&D1),scanf("%d%d%d",&R2,&C2,&D2);
               f1=true,f2=true;
               while (f1 || f2)
               {
                       if (R1==R2 && C1==C2) break;
                       if (f1)
                       {
                              A[R1][C1][0]=true;
                              R=R1+face[D1][0],C=C1+face[D1][1];
                              if (R<0 || R==n || C<0 || C==n || A[R][C][0])
                              {
                                      D1++;
                                      if (D1==4) D1=0;
                                      R=R1+face[D1][0],C=C1+face[D1][1];
                                      if (R<0 || R==n || C<0 || C==n || A[R][C][0]) 
                                            f1=false;
                                      else 
                                            R1=R,C1=C;
                              }else R1=R,C1=C;
                       }
                       if (f2)
                       {
                              A[R2][C2][1]=true;
                              R=R2+face[D2][0],C=C2+face[D2][1];
                              if (R<0 || R==n || C<0 || C==n || A[R][C][1])
                              {
                                      D2--;
                                      if (D2<0) D2=3;
                                      R=R2+face[D2][0],C=C2+face[D2][1];
                                      if (R<0 || R==n || C<0 || C==n || A[R][C][1]) 
                                            f2=false;
                                      else 
                                            R2=R,C2=C;
                              }else R2=R,C2=C;
                       }
               }
               if (R1==R2 && C1==C2) printf("%d %d\n",R1,C1);
                                else printf("-1\n");
      }
      return 0;
}


发布了619 篇原创文章 · 获赞 95 · 访问量 79万+
展开阅读全文

The Donkey of Gui Zhou

11-19

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: ![](http://acm.hdu.edu.cn/data/images/C478-1003-1.jpg) 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 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览