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 ...
随机推荐
- 如何下载到最新的版本的Oracle Database
其实这不是一个很困难的事情,但是发现好多同学都不知道,其实只需直接访问Oracle的官网就可以找到,鉴于Oracle经常改到下载面也我这里直接粘贴下载地址 http://www.oracle.com/ ...
- KMP算法_读书笔记
下面是KMP算法的实现伪代码: KMP_MATCHER ( T, P ) . n = T.length . m = P.length . next = COMPUTE_PREFIX_FUNCTION ...
- 手把手教你Windows下Go语言的环境搭建
1.想写GO语言首先得下载go语言的开发包 官方下载地址:https://code.google.com/p/go/downloads/list 我用的是Win7 64位的操作系统,截图如下: 2.把 ...
- This 在 C# 中的含义
这涉及到c# 中的oo思想,其实不管在c# 或其他编码语言中,很多抽象的概念当你项目经验多了,自然而然就会对这些东西理解的更透彻点,更加具象. 这里有一些面向对象编程的概念需要说明:类(Class)的 ...
- Mybatis的学习总结二:使用Mybatis对表进行CRUD操作【参考】
一.使用Mybatis对表进行CRUD操作------基于XML的实现 1.定义SQL的映射文件 2.在conf.xml中进行注册. 2.创建测试类 [具体过程参考:Mybatis的学习总结一] 二. ...
- 【html】【5】html class属性css样式
必看参考: http://www.divcss5.com/css3-style/ http://www.jb51.net/css/142448.html http://www.w3school.com ...
- HDU1862EXCEL排序
其实最近都没有兴趣做排序题目,因为我觉得纯粹排序对我而言进步不大,但是舍友TLE了,叫我试一试. 整道题的思路很简单啦,我用的是快排,比较的原则也给得很清楚,不必多言,我没有用stdlib的快排,也没 ...
- Qt5对付中文真好用
Qt好多C++程序员都在用,Qt4大家可能用的多,到了Qt5不熟悉的人到是很多,其中我喜欢的特性也是和Qt4大不一样的地方就是对中文的处理. Qt4中使用“QTextCodec::setCodecFo ...
- 让QMainWindow也表现出QDialog的exec函数的特征
前几天在做毕业设计项目的时候,使用的PyQt4,想实现这么样一个功能: 场景描述:主窗口a(QMainWindow类型)和主窗口b(QMainWindow),b是通过a窗口中某一个按钮弹出来的. 功能 ...
- javascript判断设备类型-手机(mobile)、安卓(android)、电脑(pc)、其他(ipad/iPod/Windows)等
使用device.js检测设备并实现不同设备展示不同网页 html代码: <!doctype html> <html> <head> <meta charse ...