HDU 1035Robot Motion(不带回溯的DFS)
2013-10-16 15:01
218 查看
Robot Motion
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5394 Accepted Submission(s): 2511
Problem Description
A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in a grid. The possible instructions are
N north (up the page)
S south (down the page)
E east (to the right on the page)
W west (to the left on the page)
For example, suppose the robot starts on the north (top) side of Grid 1 and starts south (down). The path the robot follows is shown. The robot goes through 10 instructions in the grid before leaving the grid.
Compare what happens in Grid 2: the robot goes through 3 instructions only once, and then starts a loop through 8 instructions, and never exits.
You are to write a program that determines how long it takes a robot to get out of the grid or how the robot loops around.
Input
There will be one or more grids for robots to navigate. The data for each is in the following form. On the first line are three integers separated by blanks: the number of rows in the grid, the number of columns in the grid, and the number of the column in
which the robot enters from the north. The possible entry columns are numbered starting with one at the left. Then come the rows of the direction instructions. Each grid will have at least one and at most 10 rows and columns of instructions. The lines of instructions
contain only the characters N, S, E, or W with no blanks. The end of input is indicated by a row containing 0 0 0.
Output
For each grid in the input there is one line of output. Either the robot follows a certain number of instructions and exits the grid on any one the four sides or else the robot follows the instructions on a certain number of locations once, and then the instructions
on some number of locations repeatedly. The sample input below corresponds to the two grids above and illustrates the two forms of output. The word "step" is always immediately followed by "(s)" whether or not the number before it is 1.
Sample Input
3 6 5
NEESWE
WWWESS
SNWWWW
4 5 1
SESWE
EESNW
NWEEN
EWSEN
0 0
Sample Output
10 step(s) to exit
3 step(s) before a loop of 8 step(s)
题目大意:题目意思很简单,当时主要是输出看麻烦了,The word "step" is always immediately followed by "(s)" whether or not the number
before it is 1.他说的意思是不管前面是多大,都加s,自己却在判断是不是大于1之类的,不能主观臆断啊,这题直接DFS即可!
题目地址:Robot Motion
AC代码:
#include<iostream> #include<cstring> #include<string> #include<cmath> #include<cstdio> #include<map> using namespace std; map <int,int> mq; int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}}; //定义四个方向 char a[4]={'E','S','W','N'}; //分别与四个方向对应 char p[15]; int map1[15][15]; //存储每个位置的方向 int visi[15][15]; //存储每个点的访问状态 int flag; //flag表示有无出现环的情况 int tx,ty; //循环的起始坐标 int r,c; int res1,res2; void dfs(int x,int y,int step) { if(x<0||x>=r||y<0||y>=c) { res1=step; return ; } if(visi[x][y]&&!flag) { tx=x,ty=y; res1=step; flag=1; return ; } else if(visi[x][y]&&flag) { res2=step; return ; } visi[x][y]=1; int xx,yy; xx=dir[map1[x][y]][0],yy=dir[map1[x][y]][1]; x=xx+x,y=yy+y; dfs(x,y,step+1); } int main() { for(int i=0;i<4;i++) mq[a[i]]=i; int sx,sy,i,j; while(scanf("%d%d",&r,&c)) { flag=0; if(r==0&&c==0) break; scanf("%d",&sy); sy--; sx=0; for(i=0;i<r;i++) { scanf("%s",p); for(j=0;j<c;j++) map1[i][j]=mq[p[j]]; } /*for(i=0;i<r;i++) { for(j=0;j<c;j++) cout<<map1[i][j]<<" "; cout<<endl; }*/ memset(visi,0,sizeof(visi)); dfs(sx,sy,0); if(flag) { memset(visi,0,sizeof(visi)); dfs(tx,ty,0); } if(!flag) //没有loop循环 printf("%d step(s) to exit\n",res1); else printf("%d step(s) before a loop of %d step(s)\n",res1-res2,res2); } return 0; }
相关文章推荐
- hdu 5113 Black And White (dfs回溯+剪枝)
- HDU 1045 Fire Net(DFS回溯)
- hdu 5547 Sudoku dfs 暴力回溯 解题报告
- HDU 1016 dfs+回溯
- HDU 2553 八皇后问题(DFS+回溯)
- HDU - 5004 KAMI(回溯+dfs)
- HDU 1016 Prime Ring Problem (DFS回溯)
- hdu 2616 dfs回溯暴力
- HDU 5045 Contest(DFS 回溯)
- HDU 1010题解这是一道简单的DFS加回溯题,看懂后就会对递归和回溯有较深刻的理解。
- HDU---1427(24点游戏,DFS回溯)
- HDU 1241 Oil Deposits DFS无回溯 求油田的个数
- HDU——3786找出直系亲属(DFS+回溯)
- HDU 1045 dfs + 回溯
- HDU-1045 Fire Net(DFS+回溯)
- HDU1016 Prime Ring Problem(DFS回溯)
- dfs减枝回溯! HDU 5113
- HDU 2614 Beat(dfs+回溯)
- HDU-1015 Safecracker(DFS+暴力回溯 / 五重循环)
- HDU 2553 N皇后问题 (DFS_回溯)