国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

POJ 1573 Robot Motion (模擬)

2019-11-08 02:23:28
字體:
來源:轉載
供稿:網友

Description

img

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 5NEESWEWWWESSSNWWWW4 5 1SESWEEESNWNWEENEWSEN0 0 0

Sample Output

10 step(s) to exit3 step(s) before a loop of 8 step(s)

題意

給出一個迷宮與人物的起始位置,人物沿地圖的標記行走,輸出他在第多少步時走出迷宮或者在第多少步時陷入循環以及循環的長度。

思路

迷宮可以用二維數組來存儲,每一個點存儲的是它的移動方向。

然后從初始位置模擬行走,如果某一次脫離地圖邊緣,即成功離開;

走過的點都標記為第一次走到這里所需要的步數,若在行走過程中遇到已經走過的點,則說明當前陷入循環,循環的長度即兩個步數之差。

AC 代碼

#include <iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<map>#include<set>#include<algorithm>using namespace std;int mv[4][2] = {{-1,0},{1,0},{0,-1},{0,1}}; //移動方向int mapp[15][15];int x,y,s;void solve(){ int vis[15][15]= {0}; int mx=0,my=s-1,step=0,sx,sy; while(true) { if(mx<0||mx>=x||my<0||my>=y) //離開地圖邊緣 { printf("%d step(s) to exit/n",step); break; } if(vis[mx][my]) //走到一個已經訪問過的點 { printf("%d step(s) before a loop of %d step(s)/n",vis[mx][my]-1,step-vis[mx][my]+1); break; } vis[mx][my]=++step; sx=mx; sy=my; mx+=mv[mapp[sx][sy]][0]; my+=mv[mapp[sx][sy]][1]; }}int main(){ while(~scanf("%d%d%d%*c",&x,&y,&s)&&(x||y||s)) { char str[15]; for(int i=0; i<x; i++) { gets(str); for(int j=0; j<y; j++) if(str[j]=='N')mapp[i][j]=0; //這里的方向與mv數組相對應 else if(str[j]=='S')mapp[i][j]=1; else if(str[j]=='W')mapp[i][j]=2; else if(str[j]=='E')mapp[i][j]=3; } solve(); } return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 丹阳市| 西城区| 开江县| 岫岩| 五寨县| 孝义市| 沂南县| 瓮安县| 舒兰市| 河源市| 柘荣县| 苍山县| 丹江口市| 横山县| 桓台县| 蕲春县| 武宁县| 黑山县| 融水| 新营市| 南岸区| 潢川县| 广南县| 游戏| 陵川县| 绍兴市| 花垣县| 静安区| 富阳市| 星子县| 平和县| 板桥市| 金湖县| 衡山县| 昌宁县| 叙永县| 九龙城区| 大埔区| 庄河市| 上虞市| 拉孜县|