首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

HDU 1035Robot Motion(不带来溯的DFS)

2013-10-17 
HDU 1035Robot Motion(不带回溯的DFS)Robot MotionTime Limit: 2000/1000 MS (Java/Others)Memory Limit:

HDU 1035Robot Motion(不带回溯的DFS)

Robot MotionTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5394    Accepted Submission(s): 2511


Problem DescriptionHDU 1035Robot Motion(不带来溯的DFS)

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.
 
InputThere 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.
 
OutputFor 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 
 
Sample Output
10 step(s) to exit3 step(s) before a loop of 8 step(s)
 

题目大意:题目意思很简单,当时主要是输出看麻烦了,The word "step" is always immediately followed by "(s)" whether or not the number before it is 1.他说的意思是不管前面是多大,都加s,自己却在判断是不是大于1之类的,不能主观臆断啊,这题直接DFS即可!
      题目地址:Robot Motion
AC代码:
#include<iostream>#include<cstring>#include<string>#include<cmath>#include<cstdio>#include<map>using namespace std;map <int,int> mq;int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};  //定义四个方向char a[4]={'E','S','W','N'};   //分别与四个方向对应char p[15];int map1[15][15];  //存储每个位置的方向int visi[15][15];  //存储每个点的访问状态int flag;   //flag表示有无出现环的情况int tx,ty;  //循环的起始坐标int r,c;int res1,res2;void dfs(int x,int y,int step){    if(x<0||x>=r||y<0||y>=c)    {        res1=step;        return ;    }    if(visi[x][y]&&!flag)    {        tx=x,ty=y;        res1=step;        flag=1;        return ;    }    else if(visi[x][y]&&flag)    {        res2=step;        return ;    }    visi[x][y]=1;    int xx,yy;    xx=dir[map1[x][y]][0],yy=dir[map1[x][y]][1];    x=xx+x,y=yy+y;    dfs(x,y,step+1);}int main(){    for(int i=0;i<4;i++)        mq[a[i]]=i;    int sx,sy,i,j;    while(scanf("%d%d",&r,&c))    {        flag=0;        if(r==0&&c==0) break;        scanf("%d",&sy);        sy--;        sx=0;        for(i=0;i<r;i++)        {            scanf("%s",p);            for(j=0;j<c;j++)                map1[i][j]=mq[p[j]];        }        /*for(i=0;i<r;i++)        {            for(j=0;j<c;j++)                cout<<map1[i][j]<<" ";            cout<<endl;        }*/        memset(visi,0,sizeof(visi));        dfs(sx,sy,0);        if(flag)        {            memset(visi,0,sizeof(visi));            dfs(tx,ty,0);        }        if(!flag)  //没有loop循环            printf("%d step(s) to exit\n",res1);        else            printf("%d step(s) before a loop of %d step(s)\n",res1-res2,res2);    }    return 0;}


热点排行