杭电1035 Robot Motion
来源:互联网 发布:哈密顿算法 编辑:程序博客网 时间:2024/06/06 04:26
Robot Motion
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4609 Accepted Submission(s): 2129
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)题目分析:题意是一个机器人在数组里面走N向上走,S向下走,E向右走,W向下走。判断走几步机器人走出该数组或者走几步之后进入一个为多大的循环里面。我的AC代码:#include <iostream>#include <cstdio>#include <cstring>using namespace std;int h,w,b[20][20];char a[20][20];int ok(int x,int y){if(x<0||y<0||x>=h||y>=w)return 0;return 1;}void DFS(int s){int x,y,xx,yy;memset(b,0,sizeof(b));b[0][s-1]=1;x=0;y=s-1;while (1){xx=x,yy=y;if(a[x][y]=='N')xx--;else if(a[x][y]=='S')xx++;else if(a[x][y]=='E')yy++;else if(a[x][y]=='W')yy--;if(!ok(xx,yy)){printf("%d step(s) to exit\n",b[x][y]);return;}else if(b[xx][yy]!=0){printf("%d step(s) before a loop of %d step(s)\n",b[xx][yy]-1,b[x][y]-b[xx][yy]+1);return;}else{b[xx][yy]=b[x][y]+1;x=xx;y=yy;}}}int main(){int i,j,s;while (scanf("%d%d%d",&h,&w,&s),h+w+s){for(i=0;i<h;i++)for(j=0;j<w;j++)cin>>a[i][j];DFS(s);}}
- 杭电1035 Robot Motion
- 杭电 hdu 1035 Robot Motion
- 杭电 HDU 1035 Robot Motion
- 杭电 HDU 1035 Robot Motion
- 杭电oj----1035 Robot Motion
- 杭电OJ——1035 Robot Motion
- 杭电ACM OJ 1035 Robot Motion 继续水
- 杭电ACM1035——Robot Motion
- 杭电ACM—HDU1035 Robot Motion
- 杭电oj(Java版)——1035 Robot Motion (DFS 枚举)
- hdu 1035 Robot Motion
- HDU 1035 Robot Motion
- hdoj 1035 Robot Motion
- hdu 1035 Robot Motion
- hdu 1035 Robot Motion
- hdu 1035 Robot Motion
- HDU 1035 Robot Motion
- 1035 Robot Motion
- 段寄存器的结构
- 第一部分 基本语言 第三章 标准库类型(3.3标准库vector类型)
- VC++程序导出数据到EXCEL
- 5-3 长方体类
- STM32F10x 学习笔记4(CRC计算单元 续)
- 杭电1035 Robot Motion
- 大龄程序员的出路在哪里?
- 安卓-生命周期简述
- 关系型数据库基础之:关系型数据库管理系统简介
- 1038 Biker's Trip Odometer
- 内存和CPU频率匹配方法的探讨 转载
- linux c获取系统信息
- PowerPC 学习笔记
- MooseFS 1.6.27