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/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的更多相关文章
- 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 ...
- hdu 4740 The Donkey of Gui Zhou(暴力搜索)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4740 [题意]: 森林里有一只驴和一只老虎,驴和老虎互相从来都没有见过,各自自己走过的地方不能走第二次 ...
- HDU 4740 The Donkey of Gui Zhou (模拟)
由于一开始考虑的很不周到,找到很多bug.....越改越长,不忍直视. 不是写模拟的料...................... 反正撞墙或者碰到已经走过的点就会转向,转向后还碰到这两种情况就会傻站 ...
- hdu 4740 The Donkey of Gui Zhou
1.扯犊子超多if else 判断的代码,华丽丽的TLE. #include<stdio.h> #include<string.h> #define N 1010 int ma ...
- The Donkey of Gui Zhou
Problem Description There was no donkey in the province of Gui Zhou, China. A trouble maker shipped ...
- HDU 1312 Red and Black --- 入门搜索 BFS解法
HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...
- hdu 3247 AC自动+状压dp+bfs处理
Resource Archiver Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Ot ...
- HDU 1430 魔板(康托展开+BFS+预处理)
魔板 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- HDU 5025:Saving Tang Monk(BFS + 状压)
http://acm.hdu.edu.cn/showproblem.php?pid=5025 Saving Tang Monk Problem Description <Journey to ...
随机推荐
- hdu 4508 湫湫系列故事——减肥记I(完全背包)
题意:完全背包 思路:完全背包 可以直接转化为 多重背包,num[i]=_v/c[i];//转为多重背包然后运用 多重背包 3种解法如下码1: #include<iostream> #in ...
- Oracle 学习用
最近在学习Oracle数据库 ,公司用这个,学习到现在,唉!苦逼的程序员呀! 也只能在这里发发牢骚了,这个是转载的 ,发完睡觉! oracle sql语句 一.ORACLE的启动和关闭 .在单机环境下 ...
- 一道关于java 类初始化 成员初始化的笔试题的解析
代码如下: java笔试题public class Mapplication { private static int n; private static Mapplication m1 = new ...
- PostgreSQL的 fdw 跨库使用
create extension postgres_fdw; ',dbname 'postgres'); create user mapping for android_market server s ...
- QCon2013上海站总结 -- 前端开发
选择这个专题开始主要有两个原因:一是这次会议关于前端开发的内容不多.二是我做过几年前端开发,这个专题对我来说会容易点:) 这次QCon上海关于前端开发有一个Keynote,一个Javascript专题 ...
- 应用删除后 Launchpad 上仍有应用图标无法删除的解决方法
应用删除后 Launchpad 上仍有应用图标上带有问号且无法删除时,可以将 launchpad 重置. 在终端输入: defaults write com.apple.dock ResetLaunc ...
- 【MySql】在Linux下安装MySql数据库
[参数环境] 1.Host OS:Win7 64bit 2.VM: VMware 11.1.0 3.Client OS:CentOS 6 4.系统中已安装的openssl版本: openssl-1.0 ...
- Codeforces Round #367 (Div. 2) A. Beru-taxi (水题)
Beru-taxi 题目链接: http://codeforces.com/contest/706/problem/A Description Vasiliy lives at point (a, b ...
- POP3&SMTP&IMAP
[POP3&SMTP&IMAP] IMAP是什么? IMAP,即Internet Message Access Protocol(互联网邮件访问协议),您可以通过这种协议从邮件服务器上 ...
- HDU 4596 Yet another end of the world (数学,扩展欧几里德)
题意:给出n组x,y,z.判断是否存在一个id使得id%x1∈(y1,z1),id%x2∈(y2,z2). 析: 设 id/x1=a , id/x2=b ,则 id-a*x1=u; (1) id- ...