HDU 1035 Robot Motion(大模拟)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1035

表示这个题目很简单,基本上没什么陷井,至于我wa了两次是因为起点没设置成1,也就是把回到起点的情况多加了1,有点不应该

#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
#define maxn 20
int map[maxn][maxn];
char rec[maxn][maxn];
int find_ans(int t)
{
    int i=1,j=t,step=1;
    if(rec[i][j]==0)
    {
        printf("%d step(s) to exit\n",0);
        return 0;
    }
    map[i][j]=1;
    while(1)
    {

        switch(rec[i][j])
        {
            case 'N':i--;break;
            case 'S':i++;break;
            case 'E':j++;break;
            case 'W':j--;break;
        }
        step++;
        if(map[i][j]>0)
        {
        printf("%d step(s) before a loop of %d step(s)\n",map[i][j]-1,step-map[i][j]);
        return 0;
        }
        if(rec[i][j]==0)
        {
            printf("%d step(s) to exit\n",step-1);
            return 0;
        }

        else
        map[i][j]=step;
    }
    return 0;
}
int main()
{
    int n,m,t,i;
    while(scanf("%d%d",&n,&m))
    {
        if(n==0 && m==0)
        return 0;
        scanf("%d",&t);
        memset(map,0,sizeof(map));
        memset(rec,0,sizeof(rec));
        for(i=1;i<=n;i++)
        scanf("%s",rec[i]+1);
        find_ans(t);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值