hdu-1573 Robot Motion
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 10219 | Accepted: 4977 |
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
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
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 0
Sample Output
10 step(s) to exit
3 step(s) before a loop of 8 step(s)
Source
//主要是推断是否构成循环。依据题目特点。能够设置一个标记数组,假设走过了就不能在走,直到退出循环。
用另外一个数组表示到这一个坐标的步数。那么退出循环的时候事实上已经到曾经走过的点。输出步数,然后循环节就是总步数减去当前步数。
#include<stdio.h>
#include<string.h>
int main()
{
int n,m,k,l,p,step;
char s[11][11];
int map[11][11];
while(scanf("%d %d",&n,&m)!=EOF)
{
if(n==0||m==0)break;
scanf("%d",&k);
for(int i=0;i<n;i++)
scanf("%s",s[i]);
l=0;
p=k-1;
step=0;
while(l>=0&&l<n&&p>=0&&p<m&&s[l][p]!='0')
{
if(s[l][p]=='N')
{
s[l][p]='0';
map[l][p]=++step; //先计算当前步数,然后在改变坐标值
l--;
}
else if(s[l][p]=='S')
{
s[l][p]='0';
map[l][p]=++step;
l++;
}
else if(s[l][p]=='E')
{
s[l][p]='0';
map[l][p]=++step;
p++;
}
else if(s[l][p]=='W')
{
s[l][p]='0';
map[l][p]=++step;
p--;
}
}
if(s[l][p]=='0') //表示循环 了、
printf("%d step(s) before a loop of %d step(s)\n",map[l][p]-1,step+1-map[l][p]);
else printf("%d step(s) to exit\n",step);
}
return 0;
}
hdu-1573 Robot Motion的更多相关文章
- 模拟 POJ 1573 Robot Motion
题目地址:http://poj.org/problem?id=1573 /* 题意:给定地图和起始位置,robot(上下左右)一步一步去走,问走出地图的步数 如果是死循环,输出走进死循环之前的步数和死 ...
- HDOJ(HDU).1035 Robot Motion (DFS)
HDOJ(HDU).1035 Robot Motion [从零开始DFS(4)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DF ...
- POJ 1573 Robot Motion(BFS)
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12856 Accepted: 6240 Des ...
- Poj OpenJudge 百练 1573 Robot Motion
1.Link: http://poj.org/problem?id=1573 http://bailian.openjudge.cn/practice/1573/ 2.Content: Robot M ...
- POJ 1573 Robot Motion(模拟)
题目代号:POJ 1573 题目链接:http://poj.org/problem?id=1573 Language: Default Robot Motion Time Limit: 1000MS ...
- POJ 1573 Robot Motion
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12978 Accepted: 6290 Des ...
- poj 1573 Robot Motion【模拟题 写个while循环一直到机器人跳出来】
...
- [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(模拟)
Problem Description A robot has been programmed to follow the instructions in its path. Instructions ...
- hdu 1035 Robot Motion(dfs)
虽然做出来了,还是很失望的!!! 加油!!!还是慢慢来吧!!! >>>>>>>>>>>>>>>>> ...
随机推荐
- Spring简单的小例子SpringDemo,用于初略理解什么是Spring以及JavaBean的一些概念
一.开发前的准备 两个开发包spring-framework-3.1.1.RELEASE-with-docs.zip和commons-logging-1.2-bin.zip,将它们解压,然后把Spri ...
- C#_Socket网络编程实现的简单局域网内即时聊天,发送文件,抖动窗口。
最近接触了C#Socket网络编程,试着做了试试(*^__^*) 实现多个客户端和服务端互相发送消息 发送文件抖动窗口功能 服务端: using System; using System.Collec ...
- js中this的指向
在js中this的指向对于新手来说一定是个难题,但是如果你真正理解了的话,也就没什么问题啦,下面就来讲讲this吧. JS中,this的值取决于调用的模式(调用对象),而JS中共有4种调用模式: 1. ...
- Android --------- 利用SharedPreferences存取数据
//向SharedPreferences中存放数据 //1.定义SharedPreferences对象,通过getSharedPreferences方法得到 SharedPreferences sp ...
- angularjs中ng-switch的用法
<!DOCTYPE html> <html lang="zh-CN" ng-app="app" ng-controller="ctr ...
- Android 命令
连接调试 adb connect 127.0.0.1:6555
- javascript改变背景/字体颜色(Through the javascript to change the background and font color)
鼠标移动到.移出DIV时修改DIV的颜色: 1.Change the font and Div background color--function <div style="width ...
- 在Web开发方面Java跟PHp八大对比
在Web开发方面Java跟PHp八大对比 <本文摘自百度经验,用来简单对比一下这两种语言> 一. 语言比较 PHP是解释执行的服务器脚本语言,首先php有简单容易上手的特点.语法和c语言比 ...
- python学习第十三天 -模块和包
模块和包 大家都知道,在计算机程序开发的过程中,随着程序代码越写越多,这样代码就会越不容易维护. 有时候为了好维护代码,把不同功能的函数放到不同的xx.py文件中. 在python中,一个.py文件就 ...
- 一个图片上传的servlet,传到本地磁盘,要传到服务器请修改
本来想写个controller,结果拦截器把图片拦住了,那就直接servlet public class UploadEamge extends HttpServlet{ /** * */ priva ...