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 (,) , the down-right cell is (N-,N-) and the cell below the up-left cell is (,)..... A × 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 = . ( <= N <= , <= 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 - instead.
Sample Input

 
Sample Output
-
 
 
Source
 

题意:

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

分析:

深搜。

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

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

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

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 1006
#define inf 1e12
int n;
int flag;
int p,q;
int dirx[]={,,,-};
int diry[]={,,-,};
int vis1[N][N],vis2[N][N];
void dfs(int a,int b,int c,int x,int y,int z){
vis1[a][b]=;
vis2[x][y]=;
if(flag==){
return;
}
if(a==x && b==y){
flag=;
printf("%d %d\n",a,b);
return;
} if(p && q){
flag=;
printf("-1\n");
return;
}
int aa,bb,xx,yy; if(p){
aa=a;
bb=b;
}else{
aa=a+dirx[c];
bb=b+diry[c];
if(aa< || aa>=n || bb< || bb>=n || vis1[aa][bb]==){
c=(c+)%;
aa=a+dirx[c];
bb=b+diry[c];
if(aa< || aa>=n || bb< || bb>=n || vis1[aa][bb]==){
p=;
aa=a;
bb=b;
}
}
} if(q){
xx=x;
yy=y;
}else{
xx=x+dirx[z];
yy=y+diry[z];
if(xx< || xx>=n || yy< || yy>=n || vis2[xx][yy]==){
z=(z-+)%;
xx=x+dirx[z];
yy=y+diry[z];
if(xx< || xx>=n || yy< || yy>=n || vis2[xx][yy]==){
q=;
xx=x;
yy=y;
}
}
}
dfs(aa,bb,c,xx,yy,z);
}
int main()
{
while(scanf("%d",&n)==){
if(n==){
break;
}
memset(vis1,,sizeof(vis1));
memset(vis2,,sizeof(vis2));
int a,b,c,x,y,z;
scanf("%d%d%d",&a,&b,&c);
scanf("%d%d%d",&x,&y,&z);
if(a==x && b==y){
printf("%d %d\n",a,b);
continue;
}
flag=;
p=q=;
dfs(a,b,c,x,y,z); }
return ;
}

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

  1. 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/showproble ...

  2. hdu 4740 The Donkey of Gui Zhou(暴力搜索)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4740 [题意]: 森林里有一只驴和一只老虎,驴和老虎互相从来都没有见过,各自自己走过的地方不能走第二次 ...

  3. hdu 4740 The Donkey of Gui Zhou

    1.扯犊子超多if else 判断的代码,华丽丽的TLE. #include<stdio.h> #include<string.h> #define N 1010 int ma ...

  4. HDU 4740 The Donkey of Gui Zhou (模拟)

    由于一开始考虑的很不周到,找到很多bug.....越改越长,不忍直视. 不是写模拟的料...................... 反正撞墙或者碰到已经走过的点就会转向,转向后还碰到这两种情况就会傻站 ...

  5. The Donkey of Gui Zhou

    Problem Description There was no donkey in the province of Gui Zhou, China. A trouble maker shipped ...

  6. HDU 5438 Ponds dfs模拟

    2015 ACM/ICPC Asia Regional Changchun Online 题意:n个池塘,删掉度数小于2的池塘,输出池塘数为奇数的连通块的池塘容量之和. 思路:两个dfs模拟就行了 # ...

  7. hdu 2191 珍惜现在,感恩生活 多重背包入门题

    背包九讲下载CSDN 背包九讲内容 多重背包: hdu 2191 珍惜现在,感恩生活 多重背包入门题 使用将多重背包转化为完全背包与01背包求解: 对于w*num>= V这时就是完全背包,完全背 ...

  8. Vijos P1114 FBI树【DFS模拟,二叉树入门】

    描述 我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全“1”串称为I串,既含“0”又含“1”的串则称为F串. FBI树是一种二叉树1,它的结点类型也包括F结点,B结点和I结点三种 ...

  9. hdu 4740【模拟+深搜】.cpp

    题意: 给出老虎的起始点.方向和驴的起始点.方向.. 规定老虎和驴都不会走自己走过的方格,并且当没路走的时候,驴会右转,老虎会左转.. 当转了一次还没路走就会停下来.. 问他们有没有可能在某一格相遇. ...

随机推荐

  1. Path Sum 解答

    Question Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...

  2. LeeCode-Remove Linked List Elements

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  3. 【LeetCode练习题】Maximum Depth of Binary Tree

    Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the n ...

  4. 一起来学canvas (前言)

    codeyy - 一依丫丫 一起来学canvas (前言)

  5. (step6.1.3)hdu 1875(畅通工程再续——最小生成树)

    题目大意:本题是中文题,可以直接在OJ上看 解题思路:最小生成树 1)本题的关键在于把二维的点转化成一维的点 for (i = 0; i < n; ++i) { scanf("%d%d ...

  6. Linux 多线程开发

    在任何一个时间点上,线程是可结合的(joinable)或者是分离的(detached).一个可结合的线程能够被其他线程收回其资源和杀死.在被其他线程回收之前,它的存储器资源(例如栈)是不释放的.相反, ...

  7. poj 3311 Hie with the Pie(状态压缩dp)

    Description The Pizazz Pizzeria prides itself or more (up to ) orders to be processed before he star ...

  8. N种方法妙讲LIS算法

    LIS算法经典汇总 假设存在一个序列d[1..9] = 2 1 5 3 6 4 8 9 7,可以看出来它的LIS长度为5.下面一步一步试着找出它.我们定义一个序列B,然后令 i = 1 to 9 逐个 ...

  9. Linux转发性能评估与优化-转发瓶颈分析与解决方式(补遗)

    补遗 关于网络接收的软中断负载均衡,已经有了成熟的方案,可是该方案并不特别适合数据包转发,它对server的小包处理非常好.这就是RPS.我针对RPS做了一个patch.提升了其转发效率. 下面是我转 ...

  10. cocos2dx CCControlButton button大事

    =================================.cpp文件 <pre name="code" class="cpp">bool ...