hrbust 哈理工oj 1020 Robot Motion【简单模拟】
2016-03-10 10:27
337 查看
Robot Motion | ||||||
| ||||||
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 0 | ||||||
Sample Output | ||||||
10 step(s) to exit 3 step(s) before a loop of 8 step(s) |
思路:简单模拟,牢记初始化,注意起点的标记。
AC代码:
#include<stdio.h>
#include<string.h>
using namespace std;
int output[1050][1050];
char a[1050][1050];
//n-up 1 13
//s-down 2 18
//e-right 3 4
//w-left 4 22
int n,m,t;
void solve()
{
memset(output,0,sizeof(output));
output[0][t]=1;
int flag=0;
int ans=0;
int step=1;
int x=0;
int y=t;
while(1)
{
//printf("%d %d %d\n",x,y,output[x][y]);
int cur=a[x][y]-'A';
if(cur==13)
{
x-=1;
}
if(cur==18)
{
x+=1;
}
if(cur==4)
{
y+=1;
}
if(cur==22)
{
y-=1;
}
if(x<0||y<0||x>=n||y>=m)
break;
step++;
if(output[x][y]==0)
output[x][y]=step;
else
{
//printf("%d %d\n",x,y);
ans=output[x][y];
flag=step-output[x][y];
break;
}
}
if(flag==0)
printf("%d step(s) to exit\n",step);
else
printf("%d step(s) before a loop of %d step(s)\n",ans-1,flag);
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&t))
{
if(n==0&&m==0&&t==0)break;
for(int i=0;i<n;i++)
{
scanf("%s",a[i]);
}
t--;
solve();
}
}
相关文章推荐
- React-Native自学资料汇总
- ORA-1652 临时表空间满了导致新会话数据不能入库诊断案例
- javascript中快速求数组的全部元素的相加之和
- MySql ibdata1文件太大 如何缩小
- mac下python2和python3的共存
- A bug‘s life
- Android设计模式系列-组合模式
- Referenced file contains errors (http://www.springframework.org/schema/beans/spring-beans-3.0.xsd).
- Swift 基础学习(实例方法)
- Swift 基础学习(实例方法)
- 11gR2/Aix - Dedicated Server Processes Have Large Usla Heap Segment Compared To Older Versions (文档 I
- ArcGIS runtime sdk for android 结合mpchartlib进行离线数据统计分析
- Xcode中git的使用方法介绍与"Please tell me who you are"问题的解决方案
- object类型强制转换成Int类型的方法
- Web服务器(Apache)虚拟主机的配置
- JS的Object类的属性、方法及如何创建对象
- Graph Reconstruction
- fatal error C1047: 对象或库文件“..\..\xxx.obj”是使用比创建其他对象所用编译器旧的编译器创建的;请重新生成旧的对象和库
- android获取设备屏幕大小的方法
- C#中数组、ArrayList和List三者的区别