HDU-1035 Robot Motion
http://acm.hdu.edu.cn/showproblem.php?pid=1035
Robot Motion
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5591 Accepted Submission(s): 2604
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.
#include<stdio.h>
#include<string.h>
int n,m,s,num,num1;
char str[][];
int mark[][];
void dfs(int x,int y)
{
int x1,y1;
if(str[x][y]=='W')
{ x1=x;
y1=y-;
if(x1<||x1>n||y1<||y1>m||mark[x1][y1]==)
return ;
if(mark[x1][y1]==)
{
num1++;
mark[x1][y1]=;
dfs(x1,y1);
// mark[x1][y1]=0;
}
else
{
num++;
mark[x1][y1]=;
dfs(x1,y1);
// mark[x1][y1]=0;
}
}
if(str[x][y]=='S')
{ x1=x+;
y1=y;
if(x1<||x1>n||y1<||y1>m||mark[x1][y1]==)
return ;
if(mark[x1][y1]==)
{
num1++;
mark[x1][y1]=;
dfs(x1,y1);
// mark[x1][y1]=0;
}
else
{
num++;
mark[x1][y1]=;
dfs(x1,y1);
// mark[x1][y1]=0;
}
}
if(str[x][y]=='E')
{ x1=x;
y1=y+;
if(x1<||x1>n||y1<||y1>m||mark[x1][y1]==)
return ; if(mark[x1][y1]==)
{
num1++;
mark[x1][y1]=;
dfs(x1,y1);
// mark[x1][y1]=0;
}
else
{ num++;
mark[x1][y1]=;
dfs(x1,y1);
// mark[x1][y1]=0;
}
}
if(str[x][y]=='N')
{
x1=x-;
y1=y;
if(x1<||x1>n||y1<||y1>m||mark[x1][y1]==)
return ;
if(mark[x1][y1]==)
{
num1++;
mark[x1][y1]=;
dfs(x1,y1);
// mark[x1][y1]=0;
}
else
{
num++;
mark[x1][y1]=;
dfs(x1,y1);
// mark[x1][y1]=0;
}
}
}
int main()
{
int i,j;
while(~scanf("%d%d%d",&n,&m,&s))
{
num=,num1=;
if(n==&&m==&&s==)
break;
memset(mark,,sizeof(mark));
getchar();
for(i=;i<=n;i++)
{
for(j=;j<=m;j++)
scanf("%c",&str[i][j]);
getchar();
}
dfs(,s);
if(num1==)
printf("%d step(s) to exit\n",num+);
else if(==num+-num1)
printf("0 step(s) before a loop of %d step(s)\n",num1);
else
printf("%d step(s) before a loop of %d step(s)\n",num+-num1,num1); }
return ;
}
HDU-1035 Robot Motion的更多相关文章
- HDOJ(HDU).1035 Robot Motion (DFS)
HDOJ(HDU).1035 Robot Motion [从零开始DFS(4)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DF ...
- [ACM] hdu 1035 Robot Motion (模拟或DFS)
Robot Motion Problem Description A robot has been programmed to follow the instructions in its path. ...
- hdu 1035 Robot Motion(dfs)
虽然做出来了,还是很失望的!!! 加油!!!还是慢慢来吧!!! >>>>>>>>>>>>>>>>> ...
- HDU 1035 Robot Motion(dfs + 模拟)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1035 这道题比较简单,但自己一直被卡,原因就是在读入mp这张字符图的时候用了scanf被卡. ...
- hdu 1035 Robot Motion(模拟)
Problem Description A robot has been programmed to follow the instructions in its path. Instructions ...
- 题解报告:hdu 1035 Robot Motion(简单搜索一遍)
Problem Description A robot has been programmed to follow the instructions in its path. Instructions ...
- (step 4.3.5)hdu 1035(Robot Motion——DFS)
题目大意:输入三个整数n,m,k,分别表示在接下来有一个n行m列的地图.一个机器人从第一行的第k列进入.问机器人经过多少步才能出来.如果出现了循环 则输出循环的步数 解题思路:DFS 代码如下(有详细 ...
- hdoj 1035 Robot Motion
Robot Motion Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- hdu1035 Robot Motion (DFS)
Robot Motion Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
随机推荐
- linux下查找文件
1,find 经常在linux下工作,总要查找一些文件,于是就搜索的学习了一下 find 指定目录 指定条件 指定动作 举例:find . -name "my*" 查找 当前目录下 ...
- 7z 压缩命令行工具
命令行压缩解压一 7z 1) 简介7z,全称7-Zip, 是一款开源软件.是目前公认的压缩比例最大的压缩解压软件.主页:http://www.7-zip.org/中文主页:http://7z.spar ...
- IE str.trim() 不兼容问题解决方法
本文实例分析了javascript在IE下trim函数无法使用的解决方法: 首先,javascript的trim函数在firefox或者chrome下面使用没有问题: 1 2 3 4 5 <sc ...
- gif图简介
多媒体教程 - GIF 图 GIF 是在 Web 上使用的主要图像格式之一. 本文详细讲解了 GIF 图像的特性和使用技巧. 理解图像格式 无论是 HTML 还是 XHTML 都没有规定图像的官方格式 ...
- USACO 2.2 Party Lamps 派对灯 (lamps)
题目描述 在IOI98的节日宴会上,我们有N(10<=N<=100)盏彩色灯,他们分别从1到N被标上号码.这些灯都连接到四个按钮: 按钮1:当按下此按钮,将改变所有的灯:本来亮着的灯就熄灭 ...
- php中include文件夹分析
include是包含很多php文件的一种汇总:一般放在文件夹最外层. <?php header("content-type:text/html;charset=utf-8") ...
- 从文章"避免复制与粘贴"到文章"Extract Method"的反思(1)
看了一个比我牛的人的博客园的博文"避免复制和粘贴".里面提到了重构手法Extract Method. 所以又搜了一下Extract Method. 这里先自我理解Extract ...
- OC语言-02面向对象的三大特性
01封装 #import <Foundation/Foundation.h> @interface Student : NSObject { //@public 成员变量尽量不使用 int ...
- 完美方案——iOS的WebView自适应内容高度
/////////////////////////////初始化,self.view是父控件///////////////////////////////// _webView = [[UIWebVi ...
- Spring MVC和Struts2的区别
1. 机制:spring mvc的入口是servlet,而struts2是filter,这样就导致了二者的机制不同. 2. 性能:spring会稍微比struts快.spring mvc是基于方法的设 ...