hdu4740 搜索(会爆栈,需要手动开辟)
来源:互联网 发布:电脑小说阅读软件 编辑:程序博客网 时间:2024/06/05 11:46
http://acm.hdu.edu.cn/showproblem.php?pid=4740
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.
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)
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
20 0 00 1 240 1 03 2 00
Sample Output
-11 3
Source
2013 ACM/ICPC Asia Regional Hangzhou Online
/**hdu4740 搜索(会爆栈,需要手动开辟)题目大意:有一头驴和一只虎,二者在一个n*n的棋盘里乱跑,判断是否有一个时间点二者跑到同一个格子上。至于他们跑到方式,还是看题吧解题思路:两次dfs用两个vector分别存驴和虎在i时间所处的位置点,找vector1的i和vector2的i里面的坐标第一重合的就行了*/#pragma comment(linker, "/STACK:10240000000000,10240000000000")#include <stdio.h>#include <string.h>#include <algorithm>#include <iostream>#include <vector>using namespace std;vector <pair<int,int> > vec,vec1;bool flag[1005][1005];int n;void dfs(int x,int y,int r,int num)///驴向左{ flag[x][y]=1; vec.push_back(make_pair(x,y)); if(r==1) { if(flag[x+1][y]==0&&x+1<=n) { dfs(x+1,y,r,num++); } else { if(flag[x][y-1]==0&&y-1>0) { dfs(x,y-1,2,num++); } } } else if(r==2) { if(flag[x][y-1]==0&&y-1>0) { dfs(x,y-1,r,num++); } else { if(flag[x-1][y]==0&&x-1>0) { dfs(x-1,y,3,num++); } } } else if(r==3) { if(flag[x-1][y]==0&&x-1>0) { dfs(x-1,y,r,num++); } else { if(flag[x][y+1]==0&&y+1<=n) { dfs(x,y+1,0,num++); } } } else { if(flag[x][y+1]==0&&y+1<=n) { dfs(x,y+1,r,num++); } else { if(flag[x+1][y]==0&&x+1<=n) { dfs(x+1,y,1,num++); } } }}void dfs1(int x,int y,int r,int num)///虎向右{ flag[x][y]=1; vec1.push_back(make_pair(x,y)); if(r==1) { if(flag[x+1][y]==0&&x+1<=n) { dfs1(x+1,y,r,num++); } else { if(flag[x][y+1]==0&&y+1<=n) { dfs1(x,y+1,0,num++); } } } else if(r==2) { if(flag[x][y-1]==0&&y-1>0) { dfs1(x,y-1,r,num++); } else { if(flag[x+1][y]==0&&x+1<=n) { dfs1(x+1,y,1,num++); } } } else if(r==3) { if(flag[x-1][y]==0&&x-1>0) { dfs1(x-1,y,r,num++); } else { if(flag[x][y-1]==0&&y-1>0) { dfs1(x,y-1,2,num++); } } } else { if(flag[x][y+1]==0&&y+1<=n) { dfs1(x,y+1,r,num++); } else { if(flag[x-1][y]==0&&x-1>0) { dfs1(x-1,y,3,num++); } } }}int main(){ while(~scanf("%d",&n)) { if(n==0)break; int x,y,r; scanf("%d%d%d",&x,&y,&r); memset(flag,0,sizeof(flag)); vec.clear(); dfs(x+1,y+1,r,0); int m=vec.size(); /*for(int i=0;i<m;i++) { printf("%d %d\n",vec[i].first,vec[i].second); }*/ scanf("%d%d%d",&x,&y,&r); memset(flag,0,sizeof(flag)); vec1.clear(); dfs1(x+1,y+1,r,0); int m1=vec1.size(); /*for(int i=0;i<m1;i++) { printf("%d %d\n",vec1[i].first,vec1[i].second); }*/ bool cnt=0; for(int i=0; i<m&&i<m1&&cnt==0; i++) { if(vec[i].first==vec1[i].first&&vec[i].second==vec1[i].second) { x=vec[i].first-1; y=vec[i].second-1; cnt=1; } } if(cnt==0) { if(m<m1) { for(int i=m; i<m1; i++) { if(vec[m-1].first==vec1[i].first&&vec[m-1].second==vec1[i].second) { x=vec1[i].first-1; y=vec1[i].second-1; cnt=1; } } } else { for(int i=m1; i<m; i++) { if(vec[i].first==vec1[m1-1].first&&vec[i].second==vec1[m1-1].second) { x=vec[i].first-1; y=vec[i].second-1; cnt=1; } } } } if(cnt==0) printf("-1\n"); else printf("%d %d\n",x,y); } return 0;}
0 0
- hdu4740 搜索(会爆栈,需要手动开辟)
- hdu4740 不错的简单搜索
- hdu4740
- HDU4740
- hdu4740【杭州网赛、模拟、有点搜索?】
- HDU4740-模拟
- block开辟内存(五)
- 动态内存开辟(c)
- xcode7需要手动配置网络
- 技术需要搜索
- 字符串替换(新开辟空间)
- 大数相加(不开辟额外空间)
- C中的动态开辟(malloc)
- 顺序表(静态开辟内存)
- 顺序表(动态开辟内存)
- 通讯录动态版(动态内存开辟)
- JNI基础(八)开辟C进程
- 【C】简单动态内存开辟(1)!!
- 计算机视觉方面的文献及代码下载
- 微信公共平台开发(一):服务器配置
- Javascript 封装方法
- 线程中CreateEvent和SetEvent及WaitForSingleObject的用法
- 数据结构(JAVA版本)练习之集合 简易图书管理系统
- hdu4740 搜索(会爆栈,需要手动开辟)
- Linux 的多线程编程的高效开发经验
- select 与 fd_set
- 冒泡排序
- 日期拆分并赋值给calendar
- vs2013 本地化
- IOS版本更新过程中遇到未能创建图标的问题
- 深思
- 第二章 环境搭建