Poj OpenJudge 百练 1573 Robot Motion

时间:2022-09-04 15:13:57

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

Poj OpenJudge 百练 1573 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.

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)

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

  1. Poj OpenJudge 百练 2632 Crashing Robots

    1.Link: http://poj.org/problem?id=2632 http://bailian.openjudge.cn/practice/2632/ 2.Content: Crashin ...

  2. Poj OpenJudge 百练 1062 昂贵的聘礼

    1.Link: http://poj.org/problem?id=1062 http://bailian.openjudge.cn/practice/1062/ 2.Content: 昂贵的聘礼 T ...

  3. Poj OpenJudge 百练 1860 Currency Exchang

    1.Link: http://poj.org/problem?id=1860 http://bailian.openjudge.cn/practice/1860 2.Content: Currency ...

  4. Poj OpenJudge 百练 2602 Superlong sums

    1.Link: http://poj.org/problem?id=2602 http://bailian.openjudge.cn/practice/2602/ 2.Content: Superlo ...

  5. Poj OpenJudge 百练 2389 Bull Math

    1.Link: http://poj.org/problem?id=2389 http://bailian.openjudge.cn/practice/2389/ 2.Content: Bull Ma ...

  6. Poj OpenJudge 百练 Bailian 1008 Maya Calendar

    1.Link: http://poj.org/problem?id=1008 http://bailian.openjudge.cn/practice/1008/ 2.content: Maya Ca ...

  7. 模拟 POJ 1573 Robot Motion

    题目地址:http://poj.org/problem?id=1573 /* 题意:给定地图和起始位置,robot(上下左右)一步一步去走,问走出地图的步数 如果是死循环,输出走进死循环之前的步数和死 ...

  8. POJ 1573 Robot Motion(BFS)

    Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12856   Accepted: 6240 Des ...

  9. POJ 1573 Robot Motion(模拟)

    题目代号:POJ 1573 题目链接:http://poj.org/problem?id=1573 Language: Default Robot Motion Time Limit: 1000MS ...

随机推荐

  1. 【BZOJ-3306】树 线段树 &plus; DFS序

    3306: 树 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 792  Solved: 262[Submit][Status][Discuss] De ...

  2. 使用gdb调试多线程程序总结

    转:使用gdb调试多线程程序总结 一直对GDB多线程调试接触不多,最近因为工作有了一些接触,简单作点记录吧. 先介绍一下GDB多线程调试的基本命令. info threads 显示当前可调试的所有线程 ...

  3. include 模板标签

    {%load staticfiles %}就能使用include标签了 {% include %}该标签允许在(模板中)包含其他的模板的内容,标签的参数是所要包含的模板名称,可以是一个变量,也可以是用 ...

  4. 安恒7月赛wp

    1.[order]   这道题,发现order参数处有注入点,于是就使用sqlmap盲注,emmmm,学到了sqlmap的一些小窍门.   首先,解题的语句是: sqlmap -u "htt ...

  5. Go语言之进阶篇请求报文格式分析

    1. 请求报文格式分析 示例: package main import ( "fmt" "net" ) func main() { //监听 listener, ...

  6. Python之路----内置函数补充与匿名函数

    内置函数补充:reversed()保留原列表,返回一个反向的迭代器 l = [1,2,3,4,5] l.reverse() print(l) l = [1,2,3,4,5] l2 = reversed ...

  7. android自定义控件实例

    很多时候android常用的控件不能满足我们的需求,那么我们就需要自定义一个控件了.今天做了一个自定义控件的实例,来分享下. 首先定义一个layout实现按钮内部布局: 01 <?xml ver ...

  8. Java中break、continue及标签等跳转语句的使用&lbrack;下&rsqb;

    作为上一篇使用for循环演示的跳转,这一篇将使用while.相比较来说,while比for循环更简单.代码如下: public class LabeledWhile { public static v ...

  9. hdu1243 最长公共子序列(LCS)

    原题地址 题目分析 这道题基本上是在普通LCS问题上的一点小小的变形,由求LCS的长度,改为求LCS的权值.架构还是不变的.可作为LCS问题的模板题.时间复杂度O(N^2). 注意 题目中的字母都是小 ...

  10. 倍福TwinCAT&lpar;贝福Beckhoff&rpar;应用教程12&period;1 TwinCAT控制松下伺服 连接和试运行

    首先是用松下伺服自带的软件可以测试运行(驱动器,电机都连接好,然后用USB线连接到松下伺服驱动器的X1口),打开调试软件会自动提示连接到伺服   一般需要对驱动器清除绝对值编码器数据(驱动器可能报错4 ...