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
 #include <stdio.h>
#include <string.h>
#define N 1005 bool vis1[N][N];
bool vis2[N][N];
int dir[][]= {,,,,,-,-,};
int T; bool inside(int x,int y)
{
if(x>= &&x<T && y>=&&y<T) return true;
return false;
} int main()
{
int r1,c1,d1,r2,c2,d2;
int x1,y1,x2,y2;
while(scanf("%d",&T),T)
{
memset(vis1,false,sizeof(vis1));
memset(vis2,false,sizeof(vis2));
scanf("%d %d %d",&r1,&c1,&d1);
scanf("%d %d %d",&r2,&c2,&d2); bool ok1 = true,ok2 = true;
bool flag=false;
while()
{
if(r1==r2 && c1==c2)
{
flag = true;
break;
}
if(!ok1 && !ok2) break;
vis1[r1][c1] = true;
vis2[r2][c2] = true;
if(ok1)
{
x1 = r1 + dir[d1][];
y1 = c1 + dir[d1][];
if(inside(x1,y1) && !vis1[x1][y1])
{
r1 = x1;
c1 = y1;
}
else
{
x1 = r1 + dir[(d1+)%][];
y1 = c1 + dir[(d1+)%][];
if(inside(x1,y1) && !vis1[x1][y1])
{
r1 = x1;
c1 = y1;
d1 = (d1+)%;
}
else ok1 = false;
}
}
if(ok2)
{
x2 = r2 + dir[d2][];
y2 = c2 + dir[d2][];
if(inside(x2,y2) && !vis2[x2][y2])
{
r2 = x2;
c2 = y2;
}
else
{
x2 = r2 + dir[(d2+)%][];
y2 = c2 + dir[(d2+)%][];
if(inside(x2,y2) && !vis2[x2][y2])
{
r2 = x2;
c2 = y2;
d2 = (d2+)%;
}
else ok2 = false;
}
}
}
if(flag) printf("%d %d\n",r1,c1);
else puts("-1");
} return ;
}

模拟暴力题(双向广搜)

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

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

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

  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(暴力搜索)

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

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

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

  6. 2013杭州网络赛C题HDU 4640(模拟)

    The Donkey of Gui Zhou Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  7. Android View之用户界面...

    PS:Android的控件真的是很多...现在还在忙到控件...也是神了.... 学习内容: 1.Spinner下拉菜单... 2.AutoComplete TextView自动完成文本框... 1. ...

  8. 微信小程序 自定义组件 多列选择器 对象数组 ObjectArray 自关联 三级联动

    使用方法 在 Page.json 注册组件 { "usingComponents": { "address-picker": "/component/ ...

  9. JAVA GUI编程学习笔记目录

    2014年暑假JAVA GUI编程学习笔记目录 1.JAVA之GUI编程概述 2.JAVA之GUI编程布局 3.JAVA之GUI编程Frame窗口 4.JAVA之GUI编程事件监听机制 5.JAVA之 ...

随机推荐

  1. TeeChart 的应用

    TeeChart 是一个很棒的绘图控件,不过由于里面没有注释,网上相关的资料也很少,所以在应用的时候只能是一点点的试.为了防止以后用到的时候忘记,我就把自己用到的东西都记录下来,以便以后使用的时候查询 ...

  2. HttpWebRequest中的SendChunked

    MSDN上说:When SendChunked is true, the request sends data to the Internet resource in segments. The In ...

  3. PHP学习心得(六)——变量

    PHP 中的变量用一个美元符号后面跟变量名来表示.变量名是区分大小写的.一个有效的变量名由字母或者下划线开头,后面跟上任意数量的字母,数字,或者下划线.$this 是一个特殊的变量,它不能被赋值.PH ...

  4. python import

    在执行 import module 时 会从 1 当前目录 2 pythonpath(可以通过 os.sys.path 查看) 3 python 安装目录   b import 了 a, c impo ...

  5. 常用面试sql语句

    1.编写一条sql语句,要修改一个字段的俩个值,比如把字段sex中的男改为女,女改为男. update m set m=(case when m='男' then '女' else '男' end) ...

  6. 制作第一个UI图集

    按钮分有两种形式,一种是普通按钮,也就是一张没有文字的按钮图片,在需要用时,就在上面写上不同的.当前所需要的文字.量一种按钮则是图片按钮,这种按钮的特点是整个按钮就是一张图片,它既是按钮也是图片. 在 ...

  7. Css3 圆角和渐变色问题(IE9)

    border-radius: 4px; filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#569B0E', endC ...

  8. 谷歌笔试题——排序,只允许0和其他元素交换

    2.2 长度为n的数组乱序存放着0至n-1. 现在只能进行0与其他数的swap,请设计并实现排序. 这题有一个隐含条件:即数组元素是连续的,即0--n-1,当你排好序后,你会发现数组元素和该元素的下标 ...

  9. oracle数据库的建表,删除字段,添加字段,修改字段,修改字段......

    1. 使用oracle创建一张表: SQL> create table loginuser( id ,), username ), password ), email ), descriable ...

  10. 再写FFT模板

    没什么好说的,今天又考了FFT(虽然不用FFT也能过)但是确实有忘了怎么写FFT了,于是乎只有重新写一遍FFT模板练一下手了.第一部分普通FFT,第二部分数论FFT,记一下模数2^23*7*17+1 ...