在线编程在线课堂在线测评Anycodes在线编程

编程论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

How to use bs4??
本帖最后由 carry0987 于
Double Queue 问题描述 : The new founded Balkan Investment Group Bank (
John 问题描述 : Little John is playing very funny game
linux-command Linux命令大全搜索工具,内容包含Linux命令
Coati 是一款跨平台的代码查看工具,适用于 C/C++ 和 Java。商业软件。特性:1. 索引
系统可承载海量并发,消息收发确认机制 保障消息必达 系统采用动态智
全平台视频监控,支持安卓苹果以及pcweb,支持海康大华等主流dvr,全部源码以及文档 单聊、群聊、商
如何访问类的私有属性? 下面以 TPathData 为例,
问题:从 XE4 以来,Firemonkey 曲线绘图在移动平台不平滑的问题一直令人诟病,提交到官方的 QC 也是族繁不及备载,官方似乎有意的
操作数据库(RODBC)   odbcConnect(dsn, uid="", p
数据模式:mode函数显示任何对象的模式。常见的单个的
系统可承载海量并发,消息收发确认机制 保障消息必达 系统采用动态智
RabbitMQ与PHP(一) 项
Iease团队扩编预备中,盼望能有Ruby或者java工程师加盟。全职兼职都可以。有爱好的伴侣请与我接洽。 邮件:i
ruby 怎么设置装备摆设GTK2,求教指导下!
#include #include #include #include using namespace std; int main() {
标题如图所示: 有n盏灯,编号1~n。一开端灯都是关着的
成熟的消息收发确认机制,支持万人大群 支持开发自定义的消息sdk接口,扩展性超强 支持单/
成熟的消息收发确认机制,支持万人大群 支持开发自定义的消息sdk接口,扩展性超强 支持单/
1. 注意列表和集合的区别 set 列表表现形式: list_1
Ajax   Ajax即“Asynchronous Javascript And
大师好,我比来在做布谷鸟优
分辨提取A和B图像Harris角点,接下来须要对

[HDU杭电] HDU 1035 Robot Motion-DFS-[解题报告] C++

[复制链接]
发表于 2016-3-26 22:17:52 | 显示全部楼层 |阅读模式
Robot Motion


问题描述 :

HDU 1035 Robot Motion-DFS-[解题报告] C++

HDU 1035 Robot Motion-DFS-[解题报告] C++
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.
输入:
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.
输出:
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.
样例输入:

  1. 3 6 5
  2. NEESWE
  3. WWWESS
  4. SNWWWW
  5. 4 5 1
  6. SESWE
  7. EESNW
  8. NWEEN
  9. EWSEN
  10. 0 0
复制代码
样例输出:

  1. 10 step(s) to exit
  2. 3 step(s) before a loop of 8 step(s)
复制代码
       hdu 1035 Robot Motion        深搜。
  1. #include <stdio.h>
  2. #include <string.h>

  3. #define MAX_N 11

  4. char map[MAX_N][MAX_N];
  5. int visited[MAX_N][MAX_N];
  6. int n, m;
  7. int sx, sy;
  8. int step, loopStep;

  9. bool dfs(int x, int y, int* s) {
  10.        
  11.         if (x < 1 || x > n || y < 1 || y > m) {
  12.                 return false;
  13.         }else if (visited[x][y] != -1) {
  14.                 loopStep = visited[x][y];
  15.                 return  true;
  16.         }


  17.         visited[x][y] = (*s)&#43;&#43;;
  18.         if (map[x][y] == 'N') {
  19.                 return dfs(x -1, y, s);
  20.         } else if (map[x][y] == 'S') {
  21.                 return dfs(x &#43; 1, y, s);
  22.         } else if (map[x][y] == 'W') {
  23.                 return dfs(x, y - 1, s);
  24.         } else if (map[x][y] == 'E') {
  25.                 return dfs(x, y &#43; 1, s);
  26.         }
  27.        

  28.         return false;
  29. }

  30. int main() {
  31.         int i, j;

  32.         while (scanf("%d%d", &n, &m) != EOF && n || m) {
  33.                 scanf("%d", &sy);
  34.                
  35.                 for (i = 1; i <= n; i&#43;&#43;) {
  36.                         scanf("%s", map[i] &#43; 1);
  37.                 }

  38.                 sx = 1;
  39.                 loopStep = step = 0;
  40.                 memset(visited, -1, sizeof(visited));
  41.                 if (dfs(sx, sy, &step)) {
  42.                         printf("%d step(s) before a loop of %d step(s)\n", loopStep, step - loopStep);
  43.                 } else {
  44.                         printf("%d step(s) to exit\n", step);
  45.                 }
  46.         }

  47.         return 0;
  48. }
复制代码
在线编程(http://www.anycodes.cn)&编程论坛(http://www.52exe.cn)感谢您的支持!
回复

使用道具 举报

发布主题 上个主题 下个主题 快速回复 返回列表 官方QQ群
在线客服
客 服 中 心
群 机 器 人
网站二维码
收 起 客 服

QQ|Archiver|手机版|小黑屋|Anycodes ( ICP14002806Anycodes在线编程

GMT+8, 2018-11-15 04:44 , Processed in 1.789486 second(s), 71 queries .

Powered by Anycodes

© 2001-2013 吉林市群龙科技有限公司 Inc.

快速回复 返回顶部 返回列表