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 ...
随机推荐
- SQLite数据库和JPA简单介绍
SQLite数据库和JPA简单介绍 一.SQLite简单使用 SQLite是遵循ACID的关系数据库管理系统,它的处理速度很快,它的设计目标是嵌入式的,只需要几百K的内存就可以了. 1.下载SQLit ...
- 2015-11-02-js
1.对象 创建方式有两种,一时通过new 后加object构造函数,二是用字面量法, var box=new object(); var box={ name='bokeyuan'; }; 访问对象: ...
- 文本框的onchange事件,如何兼容各大浏览器
在项目中经常会遇到对用户输入的数据进行实时校验,而不是等文本框失去焦点或用户手动点击校验. 首先分析下在哪些情况下文本框会产生change事件. 1.用户通过键盘入正常字符时: 2.用户通过键盘输入非 ...
- Maven,预加载资源文件
预加载资源文件需要先启用功能: <build> <resources> <resource> <directory>src/main/resources ...
- 1、Hadoop架构
1.Hadoop 是一个能够对大量数据进行分布式处理的软件框架,实现了Google的MapReduce编程模型和框架,能够把应用程序分割成许多小的工作单元放到任何集群节点上执行. 作业(job):一个 ...
- Codeforces Educational Codeforces Round 15 C. Cellular Network
C. Cellular Network time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- html5 canvas图片马赛克
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- Python使用UUID库生成唯一ID(转)
原文:http://www.cnblogs.com/dkblog/archive/2011/10/10/2205200.html 资料: Python官方Doc:<20.15. uuid — U ...
- cocos2d-x中有一个JniHelper类详细使用
主体思路 通过JNI获取java虚拟机,再获取当前程序的JNI环境,通过JNI环境获取需要调用的java类信息,再获取需要调用的java类中的函数信息.再通过JNI环境调用,使用类信息.函数信息,调用 ...
- CXF整合Spring发布WebService实例
一.说明: 上一篇简单介绍了CXF以及如何使用CXF来发布一个简单的WebService服务,并且介绍了客户端的调用. 这一篇介绍如何使用CXF与spring在Web项目中来发布WebService服 ...