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. XHTML学习进度备忘

    书签:XHTML 高级教程没有看:另外跳过的内容有待跟进 __________________ 学习资源:W3School. _________________ 跳过的内容: 1.“XHTML 简介” ...

  2. UDP模式与TCP模式的区别

    1.TCP有连接状态,而UDP没有. 2.TCP应用层使用无需考虑包的大小及发送情况,而UDP需要. 3.TCP中IP包大小的决定者是Socket,而UDP为应用层.

  3. The Stereo Action Dimension

    Network MIDI on iOS - Part 1   This is an app I wrote to try out some ideas for networked MIDI on iP ...

  4. mysql系统表加trigger和对特定的库禁用 DDL 语句

    给 mysql 系统表加上 trigger 1 Reply 默认情况下,mysql 是不能给系统表,例如 mysql.user 加上触发器的.会提示 ERROR 1465 (HY000): Trigg ...

  5. <转载>linux下内存泄露查找、BUG调试

    先收藏着,抽空好好看看:http://www.ibm.com/developerworks/cn/linux/l-pow-debug/ 简介 调试程序有很多方法,例如向屏幕上打印消息,使用调试器,或者 ...

  6. linux du命令: 显示文件、目录大小

    介绍:du命令用于显示指定文件(夹)在磁盘中所占的空间信息.假如指定的文件参数实际上是一个目录,就要计算该目录下的所有文件.假如 没有提供文件参数,执行du命令,显示当前目录内的文件占用空间信息. 语 ...

  7. tomcat的host配置

    本机 etc\hosts 首先了解C:\WINDOWS\system32\drivers\etc\hosts文件配置 127.0.0.1 static1.ezsins.com #adoble ps c ...

  8. mysql show processlist 命令详解

    命令格式 SHOW [FULL] PROCESSLIST SHOW PROCESSLIST显示哪些线程正在运行.您也可以使用mysqladmin processlist语句得到此信息.如果您有SUPE ...

  9. 关于datatable的一些操作以及使用adapter对数据的操作

    private void updateToolStripMenuItem_Click(object sender, EventArgs e) {//将数据更新回数据库 //获取源数据 DataTabl ...

  10. emWin(ucGUI)在PC机上模拟的按键响应多次解决办法 worldsing

    emWin(ucgui) 在PC端的模拟器,默认的按键机制是"按抬都Msg",当在按下键盘时,会收到一个key值-1,在按键没有离开时一直维持,当按键松开时还发送一个key值-0的 ...