首页 > 其他分享 >POJ 1573 Robot Motion(BFS)

POJ 1573 Robot Motion(BFS)

时间:2022-11-20 14:31:39浏览次数:64  
标签:robot number Motion step POJ 1573 grid div instructions


Robot Motion



Description




POJ 1573 Robot Motion(BFS)_#include


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)


Source


​Mid-Central USA 1999​

Time Limit: 1000MS

 

Memory Limit: 10000K

Total Submissions: 13107

 

Accepted: 6353

题意:输入三个数n,m,c,然后是n行m列,从第一行的第c个开始走,如上图所示;

解析:非常简单,一边走一边标记是否走过就可以了;

#include <iostream>
#include <string.h>
using namespace std;
char str[15][15];
int div[15][15];
int main()
{
int n, m, c, step, restep, flag, j, i;
while(cin>>n>>m>>c)
{
if(n==0&&m==0&&c==0)
return 0;
memset(div, 0, sizeof(div));
for(i=0; i<n; i++)
cin>>str[i];
i=0;
j=c-1;
step=0;
flag=1;
while(i>=0&&i<n&&j>=0&&j<m)
{
step++;
if(div[i][j]==0)
{
div[i][j]=step;
}
else
{
flag=0;
restep=div[i][j];
break;
}
if(str[i][j]=='E')
j++;
else if(str[i][j]=='W')
j--;
else if(str[i][j]=='N')
i--;
else if(str[i][j]=='S')
i++;
}
if(flag==0)
{
cout<<restep-1<<" step(s) before a loop of "<<step-restep<<" step(s)"<<endl;
}
else
{
cout<<step<<" step(s) to exit"<<endl;
}
}
return 0;
}



标签:robot,number,Motion,step,POJ,1573,grid,div,instructions
From: https://blog.51cto.com/u_15879559/5871394

相关文章

  • POJ 1845Sumdiv(数论)
    SumdivTimeLimit:1000MS MemoryLimit:30000KTotalSubmissions:20041 Accepted:5060DescriptionConsidertwonaturalnumbersAandB.LetSbethesumof......
  • Feign 实现 GET 方法传递 POJO
    Feign实现GET方法传递POJO作者:Grey原文地址:博客园:Feign实现GET方法传递POJOCSDN:Feign实现GET方法传递POJO需求SpringMVC支持GET方法直接绑定POJO......
  • Mohamed Hassan-2021-StochasticSceneAwareMotionPrediction-ICCV
    #StochasticScene-AwareMotionPrediction#paper1.paper-info1.1MetadataAuthor::[[MohamedHassan]],[[DuyguCeylan]],[[RubenVillegas]],[[JunSaito]......
  • POJ-1417(带权并查集+01背包+回溯)
    TrueLiars题目描述:Peter有一个王国。在这个王国里,一共有2种人,即诚实人和撒谎人。诚实人永远说真话,撒谎人永远说假话。可惜的是,Peter只记得诚实人的数量和撒谎人的数量......
  • POJ 1952 BUY LOW, BUY LOWER
    DescriptionTheadviceto"buylow"ishalftheformulatosuccessinthebovinestockmarket.Tobeconsideredagreatinvestoryoumustalsofollowthi......
  • SPOJ PHRASES Relevant Phrases of Annihilation
    DescriptionYouaretheKingofByteland.Youragentshavejustinterceptedabatchofencryptedenemymessagesconcerningthedateoftheplannedattackon......
  • POJ 1035 Spell checker
    DescriptionYou,asamemberofadevelopmentteamforanewspellcheckingprogram,aretowriteamodulethatwillcheckthecorrectnessofgivenword......
  • POJ 2774 Long Long Message
    DescriptionThelittlecatismajoringinphysicsinthecapitalofByterland.Apieceofsadnewscomestohimthesedays:hismotherisgettin......
  • POJ 1226 Substrings
    DescriptionYouaregivenanumberofcase-sensitivestringsofalphabeticcharacters,findthelargeststringX,suchthateitherX,oritsinversecan......
  • POJ 3420 Quad Tiling
    DescriptionTiredofthe TriTiling gamefinally,Michaelturnstoamorechallengeablegame,QuadTiling:Inhowmanywayscanyoutilea4× N (1≤......