hdoj 1035 Robot Motion 【DFS 暴力】
2015-06-23 22:01
309 查看
Total Submission(s): 7965 Accepted Submission(s): 3680
Problem Description
![](http://acm.hdu.edu.cn/data/images/1035-1.gif)
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
题意:给出一个n*m矩阵和机器人的起点,起点为第一行第s列的点。 矩阵上有4种字符,其中当机器人走到N时,表明机器人需要向上走一步;S 向下走一步; E 向右走一步 ;W向左走一步。 当机器人走出矩阵 或者 走的路径成环时结束(两种情况成立一种就行),然后输出机器人走出矩阵的步数 或者 成环前走的步数以及环的步数(对应上面成立的情况)。
思路:DFS 没什么好解释的,row <= 10, cul <= 10。 果断不会超时。我用rec[x][y] 记录第一次走到(x, y)的步数,初始化为-1。其中越界时好处理,直接输出即可;当处理成环时,若对应的rec[x][y]不为-1时,说明该坐标已走过,rec[x][y]就是成环前的步数,而当前步数减去rec[x][y]就是环的步数。详情看代码。
0ms:
Robot Motion
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7965 Accepted Submission(s): 3680
Problem Description
![](http://acm.hdu.edu.cn/data/images/1035-1.gif)
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)
题意:给出一个n*m矩阵和机器人的起点,起点为第一行第s列的点。 矩阵上有4种字符,其中当机器人走到N时,表明机器人需要向上走一步;S 向下走一步; E 向右走一步 ;W向左走一步。 当机器人走出矩阵 或者 走的路径成环时结束(两种情况成立一种就行),然后输出机器人走出矩阵的步数 或者 成环前走的步数以及环的步数(对应上面成立的情况)。
思路:DFS 没什么好解释的,row <= 10, cul <= 10。 果断不会超时。我用rec[x][y] 记录第一次走到(x, y)的步数,初始化为-1。其中越界时好处理,直接输出即可;当处理成环时,若对应的rec[x][y]不为-1时,说明该坐标已走过,rec[x][y]就是成环前的步数,而当前步数减去rec[x][y]就是环的步数。详情看代码。
0ms:
#include <cstdio> #include <cstring> #include <queue> #include <stack> #include <map> #include <string> #include <cstdlib> #include <cmath> #include <algorithm> using namespace std; char Map[11][11];//图 int rec[11][11];//记录步数 int n, m, s; void init()//初始化 { memset(rec, -1, sizeof(rec)); } void getMap() { for(int i = 0; i < n; i++) scanf("%s", Map[i]); } bool judge(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; } void DFS(int x, int y, int step)//当前坐标, 已走的步数 { if(!judge(x, y))//越界出去 { printf("%d step(s) to exit\n", step); return ; } if(rec[x][y] != -1)//已经查到 成环 { printf("%d step(s) before a loop of %d step(s)\n", rec[x][y], step-rec[x][y]); return ; } else rec[x][y] = step;//记录步数 switch(Map[x][y]) { case 'N': DFS(x-1, y, step+1); break;//步数注意加一 case 'S': DFS(x+1, y, step+1); break; case 'E': DFS(x, y+1, step+1); break; case 'W': DFS(x, y-1, step+1); break; } } int main() { while(scanf("%d%d", &n, &m), n||m) { scanf("%d", &s); init(); getMap(); DFS(0, s-1, 0); } return 0; }
相关文章推荐
- java多线程并发系列之锁的深入了解
- 平凡人生
- Docker折腾笔记
- mac+eclipse+svn+maven经验总结
- MapReduce求海量数据中的最大值
- cocoapods错误整理
- 观察者模式 Observer
- awk应用-获取文件的信息(二)
- Maven依赖jar包的查询
- Using Raw Devices for Oracle Databases
- NRF24L01+的使用
- Replication 线程
- javascript函数传递参数-----按值传递(google chrome)
- Subclipse logoEclipse的SVN插件 Subclipse编辑/纠错
- 《开源框架那点事儿18》:为什么要先从测试用例编写和文档编写开始?
- Epic - Palindromes
- 快速排序枢纽值(基元)选择方法(转载)
- DP之数塔
- Java 抽奖算法
- 解决mongodb连接失败问题