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 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

HINT

题意

驴和老虎在n*n的格子里面跑呀跑,驴遇到障碍或者自己走过的路,就会往右转,而老虎往左转,问你最早在哪儿相遇。

注意,如果转一次还是不能走的话,就会停下来

题解:

啊,读懂题,用bfs搞一搞就好了……

模拟每一步

代码

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
const int maxn=;
#define mod 1000000009
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************** int dy[]={,,-,};
int dx[]={,,,-};//这是驴++,老虎--
int vis1[][];
int vis2[][];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
if(n==)
break;
memset(vis1,,sizeof(vis1));
memset(vis2,,sizeof(vis2));
int x=read(),y=read(),z=read();
int xx=read(),yy=read(),zz=read();
int flag=;
int flag1=,flag2=;
int flag11=,flag22=;
int tt=;
while(tt<)
{
flag1=,flag2=;
vis1[x][y]=;
vis2[xx][yy]=;
if(x==xx&&y==yy)
{
printf("%d %d\n",x,y);
flag=;
break;
}
int nowx=x,nowy=y,nowz=z;
for(int i=;i<;i++)
{
if(flag11==)
break;
int nextx=nowx,nexty=nowy,nextz=nowz;
nextz=(nowz+i+)%;
nextx+=dx[nextz];
nexty+=dy[nextz];
if(nextx<||nextx>=n)
continue;
if(nexty<||nexty>=n)
continue;
if(vis1[nextx][nexty])
continue;
flag1=;
x=nextx,y=nexty,z=nextz;
break;
}
nowx=xx,nowy=yy,nowz=zz;
for(int i=;i<;i++)
{
if(flag22==)
break;
int nextx=nowx,nexty=nowy,nextz=nowz;
nextz=(nowz-i+)%;
nextx+=dx[nextz];
nexty+=dy[nextz];
if(nextx<||nextx>=n)
continue;
if(nexty<||nexty>=n)
continue;
if(vis2[nextx][nexty])
continue;
flag2=;
xx=nextx,yy=nexty,zz=nextz;
break;
}
if(flag1==&&flag2==)
tt++;
if(flag1==)
flag11=;
if(flag2==)
flag22=;
}
if(!flag)
printf("-1\n");
}
}

hdu 4740 The Donkey of Gui Zhou bfs的更多相关文章

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

    Problem Description There was no donkey ,) , the down-right cell ,N-) and the cell below the up-left ...

  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 (模拟)

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

  4. hdu 4740 The Donkey of Gui Zhou

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

  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 1312 Red and Black --- 入门搜索 BFS解法

    HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...

  7. hdu 3247 AC自动+状压dp+bfs处理

    Resource Archiver Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Ot ...

  8. HDU 1430 魔板(康托展开+BFS+预处理)

    魔板 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  9. HDU 5025:Saving Tang Monk(BFS + 状压)

    http://acm.hdu.edu.cn/showproblem.php?pid=5025 Saving Tang Monk Problem Description   <Journey to ...

随机推荐

  1. 自定义控件如何给特殊类型的属性添加默认值 z

    定义控件如何给特殊类型的属性添加默认值了,附自定义GroupBox一枚 标题有点那啥,但确实能表达我掌握此法后的心情. 写自定义控件时往往会有一个需求,就是给属性指定一个默认值(就是可以在VS中右键该 ...

  2. 【LeetCode】9 & 234 & 206 - Palindrome Number & Palindrome Linked List & Reverse Linked List

    9 - Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Som ...

  3. 第三次阅读赵炯博士的《linux内核代码完全注释》:序

    这是我第三次阅读linux内核代码完全注释了,当然前两次也没有读完,第一次读到第五章,第二次第七章. 所以说,赵炯博士对我最大的帮助时介绍了intel386的结构,以及内核编程的方法. 至于真正的内核 ...

  4. PHPCMS V9实现硬件地址MAC绑定访问技术实现

    目的:会员登录需要 用户名.密码.身份识别码(新增字段) 效果:  解决方法: 目前数据库中macaddress字段已经添加,修改了phpcms\modules\member\index.php 63 ...

  5. Python中list的实现

    原文链接这篇文章介绍了Python中list是如何实现的.在Python中list特别有用.让我们来看下list的内部是如何实现的.来看下面简单的程序,在list中添加一些整数并将他们打印出来. &g ...

  6. 转】Mahout分步式程序开发 聚类Kmeans

    原博文出自于: http://blog.fens.me/hadoop-mahout-kmeans/ 感谢! Mahout分步式程序开发 聚类Kmeans Hadoop家族系列文章,主要介绍Hadoop ...

  7. 转】Mahout分步式程序开发 基于物品的协同过滤ItemCF

    原博文出自于: http://blog.fens.me/hadoop-mahout-mapreduce-itemcf/ 感谢! Posted: Oct 14, 2013 Tags: Hadoopite ...

  8. Spark RDD概念学习系列之RDD的依赖关系(宽依赖和窄依赖)(三)

    RDD的依赖关系?   RDD和它依赖的parent RDD(s)的关系有两种不同的类型,即窄依赖(narrow dependency)和宽依赖(wide dependency). 1)窄依赖指的是每 ...

  9. Connecting Physics Bodies

    [Connecting Physics Bodies] The kinds of joints you can create in Sprite Kit. You add or remove join ...

  10. vim之grep

    [vim之grep] :vimgrep 用于多文件搜索,如 1):vim[grep] start_stage *   在当前目录下(不包括子目录)搜索 2)  :vim[grep] start_sta ...