hdu1035 2010.3.5

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+5

int 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值