hdu 4740 The Donkey of Gui Zhou(dfs模拟好题)

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

2013 ACM/ICPC Asia Regional Hangzhou Online

题意:

在一个N*N的方格里,有一只驴和一只虎,两者以相同的速度(一格一格地走)同时开始走。走法是:往东南西北某一个初始方向走,两者都不能重复走自己走过的路,但是对方走过的路自己可以走,如果遇到墙壁或者自己走过的路,则驴向右转,虎向左转,如果还不能继续往前走,就停在原地不动。如果驴和老虎能同一时间在一个格子里面相遇,则输出坐标,否则输出-1 。

分析:

深搜。

1)当两者坐标相同时,相遇;

2)两者同时在不同地方都停下时,肯定不相遇;

3)如果有一个停下了,记录当前坐标,否则往初始方向继续遍历。

  1 #pragma comment(linker, "/STACK:1024000000,1024000000")
  2 #include<iostream>
  3 #include<cstdio>
  4 #include<cstring>
  5 #include<cmath>
  6 #include<math.h>
  7 #include<algorithm>
  8 #include<queue>
  9 #include<set>
 10 #include<bitset>
 11 #include<map>
 12 #include<vector>
 13 #include<stdlib.h>
 14 #include <stack>
 15 using namespace std;
 16 #define PI acos(-1.0)
 17 #define max(a,b) (a) > (b) ? (a) : (b)
 18 #define min(a,b) (a) < (b) ? (a) : (b)
 19 #define ll long long
 20 #define eps 1e-10
 21 #define MOD 1000000007
 22 #define N 1006
 23 #define inf 1e12
 24 int n;
 25 int flag;
 26 int p,q;
 27 int dirx[]={0,1,0,-1};
 28 int diry[]={1,0,-1,0};
 29 int vis1[N][N],vis2[N][N];
 30 void dfs(int a,int b,int c,int x,int y,int z){
 31    vis1[a][b]=1;
 32    vis2[x][y]=1;
 33    if(flag==1){
 34       return;
 35    }
 36    if(a==x && b==y){
 37       flag=1;
 38       printf("%d %d\n",a,b);
 39       return;
 40    }
 41
 42    if(p && q){
 43       flag=1;
 44       printf("-1\n");
 45       return;
 46    }
 47    int aa,bb,xx,yy;
 48
 49    if(p){
 50       aa=a;
 51       bb=b;
 52    }else{
 53       aa=a+dirx[c];
 54       bb=b+diry[c];
 55       if(aa<0 || aa>=n || bb<0 || bb>=n || vis1[aa][bb]==1){
 56          c=(c+1)%4;
 57          aa=a+dirx[c];
 58          bb=b+diry[c];
 59          if(aa<0 || aa>=n || bb<0 || bb>=n || vis1[aa][bb]==1){
 60             p=1;
 61             aa=a;
 62             bb=b;
 63          }
 64       }
 65    }
 66
 67    if(q){
 68       xx=x;
 69       yy=y;
 70    }else{
 71       xx=x+dirx[z];
 72       yy=y+diry[z];
 73       if(xx<0 || xx>=n || yy<0 || yy>=n || vis2[xx][yy]==1){
 74          z=(z-1+4)%4;
 75          xx=x+dirx[z];
 76          yy=y+diry[z];
 77          if(xx<0 || xx>=n || yy<0 || yy>=n || vis2[xx][yy]==1){
 78             q=1;
 79             xx=x;
 80             yy=y;
 81          }
 82       }
 83    }
 84    dfs(aa,bb,c,xx,yy,z);
 85 }
 86 int main()
 87 {
 88    while(scanf("%d",&n)==1){
 89       if(n==0){
 90          break;
 91       }
 92       memset(vis1,0,sizeof(vis1));
 93       memset(vis2,0,sizeof(vis2));
 94       int a,b,c,x,y,z;
 95       scanf("%d%d%d",&a,&b,&c);
 96       scanf("%d%d%d",&x,&y,&z);
 97       if(a==x && b==y){
 98          printf("%d %d\n",a,b);
 99          continue;
100       }
101       flag=0;
102       p=q=0;
103       dfs(a,b,c,x,y,z);
104
105    }
106     return 0;
107 }

时间: 2024-07-30 13:05:15

hdu 4740 The Donkey of Gui Zhou(dfs模拟好题)的相关文章

hdu 4740 The Donkey of Gui Zhou bfs

The Donkey of Gui Zhou Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4740 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

HDU 4740 The Donkey of Gui Zhou

The Donkey of Gui Zhou Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 474064-bit integer IO format: %I64d      Java class name: Main There was no donkey in the province of Gui Zhou, China. A trouble maker sh

【hdu】The Donkey of Gui Zhou(搜索)

一开始的思路是,先走驴子,之后走老虎,每次走的时候记录他们的达到该位置的步数,如果一样就可以相遇,特殊处理末尾. 但是没办法过,之后索性让他们一起走,一起修改移动的坐标,结果过了... 真心不知道错哪了.. 11662502 2014-09-16 00:43:01 Accepted 4740 31MS 8300K 2547 B G++ KinderRiven #include<cstdio> #include<cstring> #include<cstdlib> #in

[HDU 5135] Little Zu Chongzhi&#39;s Triangles (dfs暴搜)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5135 题目大意:给你n条边,选出若干条边,组成若干个三角形,使得面积和最大.输出最大的面积和. 先将边从小到大排序,这样前面的两条边加起来如果不大于第三条边就可以跳出,这是一个存在性条件. dfs(int idx,int now,int cnt,int nowmax)代表我当前处理的是第idx条边,已经加入边集的有cnt条边,当前的边的长度和为now,组成的最大面积和为nowmax. 暴力枚举每个三

HDU 4771 Stealing Harry Potter&#39;s Precious dfs+bfs

Stealing Harry Potter's Precious Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and his owl. When Hogwarts school is in holiday, Harry Potter has to go back to uncle Vernon's home. But he can't bring his

hdu 1312 DFS算法简单题

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 此题与油田那题很像是练习深搜的好题,题意是从"@"开始,遍历整个图,找到连接的 "."有多少个 但要考虑变化,简单处理下就行了,主要是斜角的不要了,而且判断结束方式也不一样 具体看代码吧 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

HDU 2553 N皇后问题(深搜DFS)

N皇后问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1757    Accepted Submission(s): 772   Problem Description 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上.你的任务是,对于给定的N,求出

hdu 1010 Tempter of the Bone (DFS+剪枝)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 68206    Accepted Submission(s): 18719 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

HDU 1010 Tempter of the Bone(DFS+奇偶性剪枝)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 70665    Accepted Submission(s): 19487 Problem Description The doggie found a bone in an ancient maze, which fascinated him a