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

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 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)

#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的更多相关文章

  1. HDOJ(HDU).1035 Robot Motion (DFS)

    HDOJ(HDU).1035 Robot Motion [从零开始DFS(4)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DF ...

  2. [ACM] hdu 1035 Robot Motion (模拟或DFS)

    Robot Motion Problem Description A robot has been programmed to follow the instructions in its path. ...

  3. hdu 1035 Robot Motion(dfs)

    虽然做出来了,还是很失望的!!! 加油!!!还是慢慢来吧!!! >>>>>>>>>>>>>>>>> ...

  4. HDU 1035 Robot Motion(dfs + 模拟)

    嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1035 这道题比较简单,但自己一直被卡,原因就是在读入mp这张字符图的时候用了scanf被卡. ...

  5. hdu 1035 Robot Motion(模拟)

    Problem Description A robot has been programmed to follow the instructions in its path. Instructions ...

  6. 题解报告:hdu 1035 Robot Motion(简单搜索一遍)

    Problem Description A robot has been programmed to follow the instructions in its path. Instructions ...

  7. (step 4.3.5)hdu 1035(Robot Motion——DFS)

    题目大意:输入三个整数n,m,k,分别表示在接下来有一个n行m列的地图.一个机器人从第一行的第k列进入.问机器人经过多少步才能出来.如果出现了循环 则输出循环的步数 解题思路:DFS 代码如下(有详细 ...

  8. hdoj 1035 Robot Motion

    Robot Motion Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  9. hdu1035 Robot Motion (DFS)

    Robot Motion Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

随机推荐

  1. UDP—Socket,套接字聊天简单的聊天程序。

    思路:(发送端) 1.既然需要聊天.就应该怎么建立聊天程序,,DatagramSocket对象http://www.w3cschool.cc/manual/jdk1.6/ DatagramSocket ...

  2. Using native GDI for text rendering in C#

    Using native GDI for text rendering in C# Aug12by Arthur To complete my previous post on text render ...

  3. linux下安装多个mysql实例(摘自国外:How to create multiple mysql instance in CentOS 6.4 and Red Hat 6.4)

    How to create multiple mysql instance in CentOS 6.4 and Red Hat 6.4 from:http://sharadchhetri.com/20 ...

  4. iOS 正则表达式-判断邮箱、手机号

    判断是否是邮箱 -(BOOL)isValidateEmail:(NSString *)email { NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[ ...

  5. O-C相关04:类方法的概述与定义和调用

    类方法的概述与定义和调用 1, 类方法的概述 类方法(class method)在其他编程语言中常常称为静态方法(例如 Java 或 C# 等). 与实例方法不同的是,类方法只需要使用类名即可调用, ...

  6. Git命令详解【2】

    git的工作区   git 安装 sudo apt-get insall git 查看git 版本 git --version   git的配置 #配置用户名 git config --global ...

  7. ubuntu14.04.1 LTS 64bits较快的更新源

    网上关于ubuntu更新源的帖子一大堆,但是我使用网易源的时候,执行sudo apt-get update命令的时候,总是在最后几步出现hash校验的问题,虽然没什么大的影响,但是对于患有强迫症晚期综 ...

  8. AS3.0函数定义的方法

    在AS3.0中函数的定义有两种方法: 函数语句定义法: function 函数名(参数1:参数类型,参数2:参数类型):返回值类型{ 函数折行的语句 } function testAdd(a:int, ...

  9. jQuery 获取父元素、子元素、同级元素

    详情:http://www.w3school.com.cn/jquery/jquery_traversing_ancestors.asp   parent() 方法返回被选元素的直接父元素.(常用) ...

  10. PHP扩展Redis编译安装

    PHP扩展Redis编译安装 1.下载PHP官方Redis源码包  wget http://pecl.php.net/get/redis-2.2.4.tgz  注:我用的是Redhat系统,ubunt ...