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. H5 App如此强悍,要降薪的恐怕已不只是iOS程序员

    2015年的最后几天,移动开发圈里最为火爆的话题之一无疑是“iOS程序员月薪降至12K”这则报道. 有人认为这是O2O创业遇冷所致,也有人认为这是iOS生态过于封闭致使智能硬件等新领域对iOS开发者的 ...

  2. CRUD生成器DBuilder设计与实现

    源码位于github:https://github.com/lvyahui8/dbuilder.git .文中图片如果太小看不清楚,请右键点击“在新标签页中打开”即可看到原图 有兴趣还可以加QQ群交流 ...

  3. PHP学习心得(十)——控制结构

    if 结构是很多语言包括 PHP 在内最重要的特性之一,它允许按照条件执行代码片段. if 语句可以无限层地嵌套在其它 if 语句中,这给程序的不同部分的条件执行提供了充分的弹性. else 延伸了  ...

  4. 换行符‘\n’和回车符‘\r’

    顾名思义,换行符就是另起一行,回车符就是回到一行的开头,所以我们平时编写文件的回车符应该确切来说叫做回车换行符 '\n' 10 换行(newline)'\r' 13 回车(return) 也可以表示为 ...

  5. 网络设备作用和工作ISO层

    物理层——中继器和集线器 二者都起数字信号放大和中转的作用. 中继器 Repeater 用来延长网络距离的互连设备.REPEATER可以增强线路上衰减的信号,它两端即可以连接相同的传输媒体,也可以连接 ...

  6. AngularJS的使用方法

    1.第一个ng demo <div ng-app="" ng-init="name='Kevin'"> <input ng-model=&qu ...

  7. org

    controller层单元测试报错 org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of ...

  8. 8位灰度图在LCD上显示

    一.概述 1.灰度 灰度使用黑色调表示物体,即用黑色为基准色,不同的饱和度的黑色来显示图像.每个灰度对象都具有从 0%(白色)到灰度条100%(黑色)的亮度值. 使用黑白或灰度扫描仪生成的图像通常以灰 ...

  9. 【mapping】 springmvc的注解mapping无法生效的问题

    springmvc 始终无法加载 注解 map, 解决办法 八月 11, 2015 8:24:42 下午 org.springframework.web.servlet.DispatcherServl ...

  10. 保护模式下GDTR,LDTR,全局描述符表,局部描述符表和选择器的关系

    这张图要注意:右边两个0-15,其中上面的是LDTR,  下面的是选择子. 图下第五个标线,是两个线交叉的,实际上第五个线是指向右边水平的那个线. 没有箭头的两组线分别表示GDT的区间,LDT的区间 ...