poj 1573 Robot Motion
来源:互联网 发布:网络变压器厂商 编辑:程序博客网 时间:2024/06/05 06:29
Robot Motion
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 12822 Accepted: 6219
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 5NEESWEWWWESSSNWWWW4 5 1SESWEEESNWNWEENEWSEN0 0 0
Sample Output
10 step(s) to exit3 step(s) before a loop of 8 step(s)
这题不用读题就知道意思
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;char ch[1002][1002];int vis[1002][1002];int main(){ int n,m,i,j; int h; int ok ; int sum,sum1; while(~scanf("%d %d %d",&n,&m,&h)&&(n != 0&&m != 0&&h != 0)) { ok = 1; for(i = 0;i < n; i++) { scanf("%s",ch[i]); } int g = 0,w = h-1; memset(vis,0,sizeof(vis)); sum = 0; while(g >= 0&&g < n&&w >= 0&&w < m) { if(vis[g][w] == 1) { ok = 0; sum++; break; } else if(ch[g][w] == 'E') { vis[g][w] = 1; w++; } else if(ch[g][w] == 'W') { vis[g][w] = 1; w--; } else if(ch[g][w] == 'S') { vis[g][w] = 1; g++; } else if(ch[g][w] == 'N') { vis[g][w] = 1; g--; } sum++; } if(ok == 1) { printf("%d step(s) to exit\n",sum); } else { int x = 0,y = h-1; sum1 = 0; while(x >= 0&&x < n&&y >= 0&&y < m) { if(x == g&&y == w) { break; } else if(ch[x][y] == 'E') { y++; } else if(ch[x][y] == 'W') { y--; } else if(ch[x][y] == 'S') { x++; } else if(ch[x][y] == 'N') { x--; } sum1++; } printf("%d step(s) before a loop of %d step(s)\n",sum1,sum-sum1-1); } }}
代码菜鸟,如有错误,请多包涵!!
0 0
- poj 1573 Robot Motion
- Poj 1573 Robot Motion
- poj 1573 Robot Motion
- POJ 1573 - Robot Motion
- poj-1573-Robot Motion
- POJ 1573 Robot Motion
- POJ 1573 Robot Motion
- POJ-1573-Robot Motion
- poj 1573 Robot Motion
- POJ 1573 Robot Motion
- POJ 1573 Robot Motion.
- poj 1573 Robot Motion
- POJ -1573 Robot Motion
- POJ 1573:Robot Motion
- poj 1573 Robot Motion
- poj 1573 Robot Motion
- POJ 1573 Robot Motion
- Robot Motion POJ 1573
- NYOJ 43 24 Point game(dfs)
- 问题三 实时错误‘3704’
- sdsds
- apache服务,或者说httpd服务,如何启动,如何开机启动
- Glide加载圆形image第一次显示占位图的原因
- poj 1573 Robot Motion
- Android中px、dp、sp之间相互转换
- 解决android timePicker 和 datePicker因系统版本差异而引起的样式改变
- SP:创建Windows应用程序的要点详解
- CoreThink开发(十一)首页控制器判断移动设备还是PC并做相应处理
- emmc kernel driver流程
- 小米面试题——并查集(关于朋友圈问题)
- javaIO流部分总结
- android 如何使用Service