Poj OpenJudge 百练 1573 Robot Motion
1.Link:
http://poj.org/problem?id=1573
http://bailian.openjudge.cn/practice/1573/
2.Content:
Robot Motion
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10856 Accepted: 5260 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 areN 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 0Sample Output
10 step(s) to exit
3 step(s) before a loop of 8 step(s)Source
3.Method:
模拟题,使用arr_mark保存行走路径,走到重复的路则为loop,出边界则为exit
特别注意 0 step(s) before a loop of 4 step(s) 的情况
如:
2 2 1
SW
EN
4.Code:
#include <iostream>
#include <cstring> using namespace std; //N,E,S,W
const int idx_x[] = {,,,-};
const int idx_y[] = {-,,,};
const char idx_ch[] = {'N','E','S','W'};
const int num_d = ; int main()
{
//freopen("D://input.txt","r",stdin); int y,x;
int i; int w,h,s;
cin >> h >> w >> s; while(h != || w != || s != )
{
int **arr_d = new int*[h];
for(y = ; y < h; ++y) arr_d[y] = new int[w]; char ch;
for(y = ; y < h; ++y)
{
for(x = ; x < w; ++x)
{
cin >> ch;
for(i = ; i < num_d; ++i) if(idx_ch[i] == ch) break;
arr_d[y][x] = i;
}
} //for(y = 0; y < h; ++y)
//{
// for(x = 0; x < w; ++x)
// {
// cout << arr_d[y][x] << " ";
// }
// cout << endl;
//} int **arr_mark = new int*[h];
for(y = ; y < h; ++y)
{
arr_mark[y] = new int[w];
memset(arr_mark[y],,sizeof(int) * w);
} y = ;
x = s - ;
int path = ;
int nx,ny;
while(!arr_mark[y][x])//loop
{
nx = x;
ny = y;
arr_mark[y][x] = ++path; x = nx + idx_x[arr_d[ny][nx]];
y = ny + idx_y[arr_d[ny][nx]]; if(y < || y >= h || x < || x >= w) break;//exit
} if(y < || y >= h || x < || x >=w)
{
cout << path << " step(s) to exit" << endl;
}
else
{
cout << (arr_mark[y][x] - ) << " step(s) before a loop of " << (arr_mark[ny][nx] - arr_mark[y][x] + ) << " step(s)" << endl;
} //for(y = 0; y < h; ++y)
//{
// for(x = 0; x < w; ++x) cout << arr_mark[y][x] << " ";
// cout << endl;
//} for(y = ; y < h; ++y) delete [] arr_mark[y];
delete [] arr_mark; for(y = ; y < h; ++y) delete [] arr_d[y];
delete [] arr_d; cin >> h >> w >> s;
} //fclose(stdin); return ;
}
5.Reference:
http://poj.org/showmessage?message_id=123463
Poj OpenJudge 百练 1573 Robot Motion的更多相关文章
- Poj OpenJudge 百练 2632 Crashing Robots
1.Link: http://poj.org/problem?id=2632 http://bailian.openjudge.cn/practice/2632/ 2.Content: Crashin ...
- Poj OpenJudge 百练 1062 昂贵的聘礼
1.Link: http://poj.org/problem?id=1062 http://bailian.openjudge.cn/practice/1062/ 2.Content: 昂贵的聘礼 T ...
- Poj OpenJudge 百练 1860 Currency Exchang
1.Link: http://poj.org/problem?id=1860 http://bailian.openjudge.cn/practice/1860 2.Content: Currency ...
- Poj OpenJudge 百练 2602 Superlong sums
1.Link: http://poj.org/problem?id=2602 http://bailian.openjudge.cn/practice/2602/ 2.Content: Superlo ...
- Poj OpenJudge 百练 2389 Bull Math
1.Link: http://poj.org/problem?id=2389 http://bailian.openjudge.cn/practice/2389/ 2.Content: Bull Ma ...
- Poj OpenJudge 百练 Bailian 1008 Maya Calendar
1.Link: http://poj.org/problem?id=1008 http://bailian.openjudge.cn/practice/1008/ 2.content: Maya Ca ...
- 模拟 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 ...
- POJ 1573 Robot Motion(模拟)
题目代号:POJ 1573 题目链接:http://poj.org/problem?id=1573 Language: Default Robot Motion Time Limit: 1000MS ...
随机推荐
- delphi 数组类型
数组类型 数组类型定义了一组指定类型的元素序列,在方括号中填入下标值就可访问数组中的元素.定义数组时,方括号也用来指定可能的下标值.例如,下面的代码中定义了一个有 24 个整数的数组:type ...
- ListView返回选中的多项目
ListView返回选中的多项目 procedure TForm1.Button3Click(Sender: TObject);var s: string; I: Integer;begi ...
- 基于Linux的owncloud搭建
为了保证一个纯净的环境,我重新安装了一台centos系统 [root@localhost httpd-2.2.23]# lsb_release -a LSB Version: :base-4.0 ...
- int *(*a[5])(int, char*)
int* 表示是一个int型指针;(*a[5])(int, char*)中的a[5]表示是一个有5个元素的数组,而(*)(int, char*)则表示指向一个函数的指针,该函数有两个参数,第一个参数为 ...
- Sales_item例子
Sales_item.h #ifndef SALES_ITEM_H #define SALES_ITEM_H #include<iostream> #include<string&g ...
- debian下软件包管理方式总结
linux最流行的包管理方式除了rpm之外就是debian的deb格式了.目前采用deb管理方式的主流操作系统主要有debian和ubuntu系列.和rpm包管理方式不同的是,虽然debian也有包含 ...
- 内核工具 – Sparse 简介
转载:http://www.cnblogs.com/wang_yb/p/3575039.html Sparse是内核代码静态分析工具, 能够帮助我们找出代码中的隐患. 主要内容: Sparse 介绍 ...
- java_小技巧
看很多人说,在Eclipse里面,输入Syso然后按 ALT+/不起作用. 正确的用法如下,先输入一行 System.out.println(); 然后连按5次以上shift键,其实就是粘滞的功能.接 ...
- Android基本控件之ListView(一)
我们在使用手机的时候,通常看到,像通讯录,QQ列表样式的东西,这里来解释一下,其实那些都是一个ListView 今天,我们就来详细的讲解一下ListView这个控件 ListView中每条显示的数据都 ...
- ArcMap运行时出现Runtime Error错误的解决方案
运行ArcMap时弹出错误提示:“Microsoft Visual C++ Runtime Library. Runtime 1.开始->运行->regsvr32 "C:\Pro ...
