hdu1035 2010.3.5

来源:互联网 发布:网络写手怎么赚钱 编辑:程序博客网 时间:2024/06/14 03:27

hdu1035 2010.3.5

Robot Motion

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 1149    Accepted Submission(s): 532

 

 

Problem Description


 

A robot has been programmed to follow theinstructions in its path. Instructions for the next direction the robot is tomove 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 onthe north (top) side of Grid 1 and starts south (down). The path the robotfollows is shown. The robot goes through 10 instructions in the grid beforeleaving the grid.

 

Compare what happens in Grid 2: the robotgoes through 3 instructions only once, and then starts a loop through 8instructions, and never exits.

 

You are to write a program that determineshow 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 robotsto navigate. The data for each is in the following form. On the first line arethree integers separated by blanks: the number of rows in the grid, the numberof columns in the grid, and the number of the column in which the robot entersfrom the north. The possible entry columns are numbered starting with one atthe left. Then come the rows of the direction instructions. Each grid will haveat least one and at most 10 rows and columns of instructions. The lines ofinstructions contain only the characters N, S, E, or W with no blanks. The endof input is indicated by a row containing 0 0 0.

 

 

 

Output

For each grid in the input there is oneline of output. Either the robot follows a certain number of instructions andexits the grid on any one the four sides or else the robot follows theinstructions on a certain number of locations once, and then the instructionson some number of locations repeatedly. The sample input below corresponds tothe two grids above and illustrates the two forms of output. The word"step" is always immediately followed by "(s)" whether ornot the number before it is 1.

 

 

 

Sample Input

3 6 5

NEESWE

WWWESS

SNWWWW

4 5 1

SESWE

EESNW

NWEEN

EWSEN

0 0


#include <stdio.h>#include <string.h>#define MAXN 10+5int hash[MAXN][MAXN],num[MAXN][MAXN];char map[MAXN][MAXN];int n,m,ans,st,i,j;void printsteps(){/*    if (ans==1)        printf("1 step to exit\n");    else*/        printf("%d step(s) to exit\n",ans);}void loop(int x,int y){    int tt;    tt=ans+1-hash[x][y];    /*if ((hash[x][y]==1)&&(tt==1))        printf("1 step before a loop of 1 step\n");    else    {        if (hash[x][y]==1)            printf("1 step before a loop of %d step(s)\n",tt);        else            if (tt==1)                printf("%d step(s) before a loop of 1 step\n",hash[x][y]-1);            else*/                printf("%d step(s) before a loop of %d step(s)\n",hash[x][y]-1,tt);}void deal(int x,int y){    if (hash[x][y]>0)    {        loop(x,y);    }    else    {        if ((y==0)||(x==0)||(x==n+1)||(y==m+1))        {            printsteps();        }        else        {            ans++;            hash[x][y]=ans;            if (map[x][y]=='N')                deal(x-1,y);            else                if (map[x][y]=='S')                    deal(x+1,y);                else                     if (map[x][y]=='W')                        deal(x,y-1);                    else deal(x,y+1);        }    }}void main(){    while (scanf("%d %d",&n,&m),n)    {        scanf("%d",&st);        memset(hash,0,sizeof(hash));        ans=0;        getchar();        for(i=1;i<=n;i++)        {            for(j=1;j<=m;j++)                map[i][j]=getchar();            getchar();        }        deal(1,st);    }}


0 0
原创粉丝点击