模拟
来源:互联网 发布:淘宝主图批量修改 编辑:程序博客网 时间:2024/04/30 00:24
原题http://acm.hdu.edu.cn/showproblem.php?pid=1035
有时候,A题需要点运气
代码虐我千百遍,我待代码如初恋
Robot Motion
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6597 Accepted Submission(s): 3081
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 5NEESWEWWWESSSNWWWW4 5 1SESWEEESNWNWEENEWSEN0 0
Sample Output
10 step(s) to exit3 step(s) before a loop of 8 step(s)
#include <stdio.h>#include <stdlib.h>#include <malloc.h>#include <limits.h>#include <ctype.h>#include <string.h>#include <string>#include <math.h>#include <queue>#include <stack>#include <set>//#include <map>#include <deque>#include <vector>#include <algorithm>#include <iostream>using namespace std;#define MAXN 10 + 5char map[MAXN][MAXN];int vis[MAXN][MAXN];int n,m,st;void find(int a){ int i,j,x,y; int mark = 0; i = 0; j = a; vis[i][j] = 1; int count = 0; while(mark == 0){ if(map[i][j] == 'N'){ i = i-1; j = j; if(i<0 || j<0 || i>=n || j>=m){ mark = 1; count++; break; } else if(vis[i][j] == 1){ x = i; y = j; mark = 2; break; } else{ vis[i][j] = 1; count++; continue; } } if(map[i][j] == 'S'){ i = i+1; j = j; if(i<0 || j<0 || i>=n || j>=m){ mark = 1; count++; break; } else if(vis[i][j] == 1){ mark = 2; x = i; y = j; break; } else{ vis[i][j] = 1; count++; continue; } } if(map[i][j] == 'E'){ i = i; j = j+1; if(i<0 || j<0 || i>=n || j>=m){ mark = 1; count++; break; } else if(vis[i][j] == 1){ x = i; y = j; mark = 2; break; } else{ vis[i][j] = 1; count++; continue; } } if(map[i][j] == 'W'){ i = i; j = j-1; if(i<0 || j<0 || i>=n || j>=m){ mark = 1; count++; break; } else if(vis[i][j] == 1){ x = i; y = j; mark = 2; break; } else{ vis[i][j] = 1; count++; continue; } } } if(mark == 1){ printf("%d step(s) to exit\n",count); } else{ int sum1 = 0; int sum2 = 0; int flag = 0; i = 0; j = a; while(flag == 0){ if(i==x && j==y){ flag = 1; break; } else{ if(map[i][j] == 'N'){ i = i-1; j = j; sum1++;continue; } if(map[i][j] == 'S'){ i = i+1; j = j; sum1++;continue; } if(map[i][j] == 'E'){ i = i; j = j+1; sum1++;continue; } if(map[i][j] == 'W'){ i = i; j = j-1; sum1++;continue; } } } int flag1 = 0; if(map[x][y] == 'N'){ i = x-1; j = y; } if(map[x][y] == 'S'){ i = x+1; j = y; } if(map[x][y] == 'E'){ i = x; j = y+1; } if(map[x][y] == 'W'){ i = x; j = y-1; } sum2 = 1; while(flag1 == 0){ if(i==x && j==y){ flag1++; break; } if(map[i][j] == 'N'){ i = i-1; j = j; sum2++;continue; } if(map[i][j] == 'S'){ i = i+1; j = j; sum2++;continue; } if(map[i][j] == 'E'){ i = i; j = j+1; sum2++;continue; } if(map[i][j] == 'W'){ i = i; j = j-1; sum2++;continue; } } printf("%d step(s) before a loop of %d step(s)\n",sum1,sum2); }}int main(){ int i,j; while(~scanf("%d%d%d",&n,&m,&st)){ if(n==0 && m==0 && st==0){ break; } //getchar(); memset(vis,0,sizeof(vis)); for(i=0;i<n;i++){ scanf("%s",map[i]); } find(st-1); } return 0;}
0 0
- 模拟
- 模拟
- 模拟
- 模拟
- 模拟
- 模拟
- 模拟
- 模拟
- 模拟
- 模拟:
- 模拟
- 模拟
- 模拟
- 模拟
- 模拟
- 模拟
- 模拟
- 模拟
- uva 11796 Dog Distance(计算几何, 基础)
- 随机数范围扩展方法总结
- 软件开发评估-——投入知多少?
- 编辑距离(Minimum Edit Distance)
- 策略模式和状态模式的区别2
- 模拟
- C++设计模式——建造者模式(一般模式,省略指挥者、引入钩子)
- 开启程序动画效果
- cocos2d-x 3.2 |如何在Layer中实现用户触摸
- dll调用
- 年度总结--学习的日子总是过得很快
- ZOJ 2676 Network Wars 最小割 分数规划
- 极限编程下的TDD
- Cocos2d-x3.2 中简单的滚屏效果