hdu 4740 The Donkey of Gui Zhou dfs 搜索 解题报告
2016-07-09 17:45
465 查看
The Donkey of Gui Zhou
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1930 Accepted Submission(s): 646
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
思路: dfs直接暴力。
代码:
#include <iostream>
#include <cstring>
#include <string>
#include <queue>
#include <cstdio>
#include <algorithm>
using namespace std;
int fx[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
int n, flag;
int visa[1010][1010], visb[1010][1010];
void dfs(int ax, int ay, int af, int bx, int by, int bf)
{
if(flag) return ;
if(ax == bx && ay == by)
{
flag = 1;
printf("%d %d\n", ax, ay);
return ;
}
int f = 0;
for(int i = af; i < af + 2; i++)
{
int tx = ax + fx[i%4][0], ty = ay + fx[i%4][1];
if(tx >= 0 && ty >= 0 && tx < n && ty < n && !visa[tx][ty])
{
f = 1;
ax = tx;
ay = ty;
af = i;
visa[ax][ay] = 1;
break;
}
}
for(int i = bf + 4; i > bf + 2; i--)
{
int tx = bx + fx[i%4][0], ty = by + fx[i%4][1];
if(tx >= 0 && ty >= 0 && tx < n && ty < n && !visb[tx][ty])
{
f = 1;
bx = tx;
by = ty;
bf = i;
visb[bx][by] = 1;
break;
}
}
if(f) dfs(ax, ay, af, bx, by, bf);
}
int main()
{ int ax, ay, af, bx, by, bf;
while(scanf("%d", &n), n)
{
flag = 0;
memset(visa, 0, sizeof(visa));
memset(visb, 0, sizeof(visb));
scanf("%d%d%d", &ax, &ay, &af);
scanf("%d%d%d", &bx, &by, &bf);
visa[ax][ay] = 1;
visb[bx][by] = 1;
dfs(ax, ay, af, bx, by, bf);
if(!flag) printf("-1\n");
}
return 0;
}
相关文章推荐
- 搜狗百度360市值齐跌:搜索引擎们陷入集体焦虑?
- 本人即将筹备败家日志,敬请期待!
- IE:使用搜索助手
- C++深度优先搜索的实现方法
- 基于文本的搜索
- php实现搜索一维数组元素并删除二维数组对应元素的方法
- 使用Sphinx对索引进行搜索
- asp 多关键词搜索的简单实现方法
- C#使用foreach语句搜索数组元素的方法
- WordPress中用于获取搜索表单的PHP函数使用解析
- JavaScript中数组的排序、乱序和搜索实现代码
- jquery ztree实现树的搜索功能
- 【经典源码收藏】jQuery实用代码片段(筛选,搜索,样式,清除默认值,多选等)
- C#编程实现Excel文档中搜索文本内容的方法及思路
- Win2003利用dfs(分布式文件系统)在负载均衡下的文件同步配置方案
- sqlserver中在指定数据库的所有表的所有列中搜索给定的值
- 可以用来搜索当前页面内容的js代码
- 全文搜索和替换
- javascript搜索自动提示功能的实现第1/3页
- iOS应用中UISearchDisplayController搜索效果的用法