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 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. Android系统Recovery工作原理之使用update.zip升级过程分析(一)

    通过分析update.zip包在具体Android系统升级的过程,来理解Android系统中Recovery模式服务的工作原理.我们先从update.zip包的制作开始,然后是Android系统的启动 ...

  2. 小米2在Eclipse 调试,要注意下列步骤。

    小米2在Eclipse 调试,要注意下列步骤.1.连接线,打开设置:USB线连接小米2,在设置-->开发者选项->USB 调是打开.如果这一步,就业在Eclipse中真机调试,下面的步骤不 ...

  3. ckfinder动态指定上传路径

    默认情况下无法用代码修改config.ascx中的BaseUrl设置,因为其后端代码ConfigFile中并没有提供修改BaseUrl的方法,这里我借用了fckeditor以前的用法:利用sessio ...

  4. mysql数据库问答

    一. 问:如果有一张表,里面有个字段为id的自增主键,当已经向表里面插入了10条数据之后,删除了id为8,9,10的数据,再把mysql重启,之后再插入一条数据,那么这条数据的id值应该是多少,是8, ...

  5. lisener在web.xml中设置

    /* * servlet监听器开发步骤: * 1.写一个类实现XXXListener接口(6个=3个容器+3个对容器中属性进行操作) * 2.在web.xml中配置<listener> - ...

  6. cocos2d-x中使用Http

    一.如何使用 //发送接口 void CmdHelper::postRequest(const char* cmdTag, const char* url, const char* postData, ...

  7. 引用自定义的framework

    关于静态库引用文件 如果希望你的工程能在未来能导出成静态库,那么在你编写的时候要遵循静态库引用原则,使用这种方式. 注意:这种引用方式必须在你的Products下静态库成黑色时候,才能编译通过. 使用 ...

  8. Mac上mariadb的启动与关闭

    1. 启动 launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mariadb.plist 2. 关闭 launchctl unload ~/Lib ...

  9. Angular 2.0 从0到1 (四)

    第一节:Angular 2.0 从0到1 (一)第二节:Angular 2.0 从0到1 (二)第三节:Angular 2.0 从0到1 (三)第四节:Angular 2.0 从0到1 (四)第五节: ...

  10. freeCodeCamp:GO BYBY GO!

    千里之行,始于足下. 今天是2016年10月26日, 写前端代码有一年多了, 渐渐找到了一些方向, 知道该往哪里努力, 也慢慢开始总结自己的工作和学习. 希望在这里记录下的点点滴滴, 成为我日后学习的 ...