国产探花免费观看_亚洲丰满少妇自慰呻吟_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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 连南| 云林县| 永登县| 宁河县| 彩票| 丹巴县| 襄垣县| 竹山县| 嘉禾县| 南漳县| 伊吾县| 南安市| 华池县| 哈巴河县| 台北县| 威远县| 扎兰屯市| 闽侯县| 广丰县| 延寿县| 博野县| 安仁县| 托克托县| 文安县| 石柱| 丹江口市| 桑日县| 黔西| 尼勒克县| 湟中县| 莱阳市| 吉安县| 楚雄市| 遵义市| 上林县| 华坪县| 全南县| 龙游县| 泽普县| 同德县| 晋中市|