1010 Robot Motion
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.
题意:题目给出一个矩阵,让机器人按照规定行走;如果最终走入一个循环中,则输出进入循环前的的步数和循环的步数,如果最终走出矩阵范围也是输行走的步数;
规则如下:
E:向右走一步;W:向左走一步;N:向上走一步;S向下走一步;
AC代码:
#include<iostream>
#include<cstring>
#include<cstdio> using namespace std; int dp[][]={};
char ch[][];
int x,y,s;
int em()
{
int number=;
int a=,b=s;
while(){
if(dp[a][b]){//循环情况的处理;
cout<<dp[a][b]-<<" step(s) before a loop of "<<number-dp[a][b]<<" step(s)"<<endl;
return ;
}
if(a==||a==x+||b==||b==y+){cout<<number-<<" step(s) to exit"<<endl;return ;}
dp[a][b]=number;
switch(ch[a][b])
{
case 'E':b++;break;
case 'W':b--;break;
case 'S':a++;break;
case 'N':a--;break;
}
number++;
}
} int main()
{
freopen("1.txt","r",stdin);
int i,j;
while(){
memset(dp,,sizeof(dp));
cin>>x>>y>>s;
if(x==y&&y==s&&s==)return ;
for(i=;i<=x;i++)
for(j=;j<=y;j++)cin>>ch[i][j];
em();
}
}
1010 Robot Motion的更多相关文章
- HDOJ(HDU).1035 Robot Motion (DFS)
		
HDOJ(HDU).1035 Robot Motion [从零开始DFS(4)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DF ...
 - hdu1035 Robot Motion (DFS)
		
Robot Motion Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
 - poj1573 Robot Motion
		
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12507 Accepted: 6070 Des ...
 - Robot Motion(imitate)
		
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11065 Accepted: 5378 Des ...
 - 模拟 POJ 1573 Robot Motion
		
题目地址:http://poj.org/problem?id=1573 /* 题意:给定地图和起始位置,robot(上下左右)一步一步去走,问走出地图的步数 如果是死循环,输出走进死循环之前的步数和死 ...
 - POJ 1573 Robot Motion(BFS)
		
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12856 Accepted: 6240 Des ...
 - Robot Motion                                                       分类:            POJ             2015-06-29 13:45    11人阅读    评论(0)    收藏
		
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11262 Accepted: 5482 Descrip ...
 - POJ 1573 Robot Motion
		
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12978 Accepted: 6290 Des ...
 - Poj OpenJudge 百练 1573 Robot Motion
		
1.Link: http://poj.org/problem?id=1573 http://bailian.openjudge.cn/practice/1573/ 2.Content: Robot M ...
 
随机推荐
- spring+springmvc+mybatis整合框架搭建
			
由于例子是基于Maven搭建的,所以首先是pom.xml文件的依赖信息: <project xmlns="http://maven.apache.org/POM/4.0.0" ...
 - js-数据转换
			
<script type="text/javascript"> var msg = '{"code": 0, "data": 2 ...
 - Chapter 3 Phenomenon——1
			
When I opened my eyes in the morning, something was different. 这天早上当我睁开眼睛的时候,一些事变得不同了. It was the li ...
 - MVC之联动学习
			
一,数据库表设计 CREATE TABLE [dbo].[HY_Province]( [id] [INT] NOT NULL, [province] [NVARCHAR]() NOT NULL, CO ...
 - Openjudge-NOI题库-垂直直方图
			
题目描述 Description 写一个程序从输入文件中去读取四行大写字母(全都是大写的,每行不超过72个字符),然后用柱状图输出每个字符在输入文件中出现的次数.严格地按照输出样例来安排你的输出格式. ...
 - robotium如何定位控件?
			
search类获取当前所有的view,然后根据类型或者文本去筛选,找到view后获取坐标,然后点击坐标.本质都是通过坐标点击.solo.clickonScreen方法,底层调用MotionEvent类 ...
 - Edward's Cola Plan
			
Edward's Cola Plan Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu S ...
 - H5移动端页面设计心得分享(转载)
			
去年JDC出了不少优秀的武媚娘…不,H5呢,大家都很拼,同时当然也积累了一些经验和教训,今天结合咱们的实战案例,从字体,排版,动效,音效,适配性,想法这几个方面好好聊一聊关于H5的设计,希望对同学们有 ...
 - java变量和数据类型总结
 - js时间戳格式化成日期格式
			
原文:http://www.sufeinet.com/thread-1500-1-1.htmljs需要把时间戳转为为普通格式,一般的情况下可能用不到的,下面先来看第一种吧 function getLo ...