POJ 1573 Robot Motion(模拟)
2015-08-01 10:32
495 查看
Robot Motion
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
Sample Output
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 11427 | Accepted: 5541 |
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 0
Sample Output
10 step(s) to exit 3 step(s) before a loop of 8 step(s)
从给定的点出发 E,S,W,N,代表四个方向,若能走出去,输出走了多少步
走发现环,输出走入这个环时的步数 和 环的步数
直接模拟就可以了
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #define M 5000001 using namespace std; int xx[]= {-1,1,0,0}; int yy[]= {0,0,-1,1}; int a[1001][1001]; int b[1000][1001]; int sum[1001][1001]; int n,m,s; void bfs() { int x,y; memset(b,0,sizeof(b)); memset(sum,0,sizeof(sum)); x=1; y=s; int t=0; while(x>=1&&x<=n&&y>=1&&y<=m&&b[x][y]==0) { b[x][y]=1; //标记该点已走过 int dx=x+xx[a[x][y]]; int dy=y+yy[a[x][y]]; sum[x][y]=t; //记录走到(x,y)的步数 t++; //总的步数 x=dx; y=dy; } if(b[x][y]) printf("%d step(s) before a loop of %d step(s)\n",sum[x][y],t-sum[x][y]); else printf("%d step(s) to exit\n",t); } int main() { int i,k; char e; while(scanf("%d%d%d",&n,&m,&s)&&(m||n||s)) { getchar(); for(i=1; i<=n; i++) { for(k=1; k<=m; k++) { scanf("%c",&e); if(e=='N') a[i][k]=0; else if(e=='S') a[i][k]=1; else if(e=='W') a[i][k]=2; else if(e=='E') a[i][k]=3; } getchar(); } bfs(); } return 0; }
相关文章推荐
- Shiro学习(21)授予身份及切换身份
- poj 2828
- PAT (Advanced Level) 1098. Insertion or Heap Sort (25) 直插和堆排
- 时间选择器wheelView
- hdu1150 Machine Schedule(最小点覆盖)
- 【Win10】联想Z50-70升级到Win10,声音变成环绕声解决办法
- 【NOIP2003】加分二叉树题解
- android 微信支付成功,未返回回调页面
- 2015ACM多校对抗赛第四场 hdu 5336
- 详解Python3中的Sequence type的使用
- Dijkstra算法(戴克斯特拉算法)
- Shiro学习(20)无状态Web应用集成
- Bootstrap 固定定位(Affix)
- 同一时间同一帐号只能登陆在一台电脑
- 矩阵优化的模板(用的时候注意要修改呀)
- 微信开发:客服接口向用户发送消息
- poj 1019 Number Sequence 【组合数学+数字x的位宽函数】
- C语言计算某天是一年中的第几天(熟练使用状态位flag)
- 面试30 求数组中最小的k个数
- 获得手势所在的图片坐标 & CGRectContainsPoint