poj1573Robot Motion

背景:一次ac,但做这种没有中文的题做多了,已经严重让我怀疑我自己了,毕竟英语太烂,读懂题意就要读好久。

思路:主要用两个数组模拟整个path,一个用字符串数组来模拟出整个命令,然后用一个整型数组初始化为0,每次走一步就标记一下,这样就能很快的找到结束条件了。

学习:懂得简化代码,把各种情况相同部分的代码写到一起。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
char path[12][12];
void judge(int r,int c,int x,int y)
{
    int path2[12][12],k=2;
    memset(path2,0,sizeof(path2));
    path2[y][x]=1;
    while(1)
    {
        char ch=path[y][x];
        int x1=x,y1=y;
        if(ch=='W') x1=x-1;
        if(ch=='E') x1=x+1;
        if(ch=='N') y1=y-1;
        if(ch=='S') y1=y+1;
        if(x1>=0&&x1<r&&y1>=0&&y1<c)
        {
            if(!path2[y1][x1])
            {
                path2[y1][x1]=k;k++;
                x=x1;y=y1;
            }
            else
            {
                cout<<path2[y1][x1]-1<<" step(s) before a loop of ";
                cout<<path2[y][x]-path2[y1][x1]+1<<" step(s)"<<endl;
                return;
            }
        }
        if(x1<0||y1<0||x1>=r||y1>=c)
        {
            cout<<path2[y][x]<<" step(s) to exit"<<endl;
            return;
        }
    }
}
int main()
{
    int r,c,qishi;
    while(cin>>c>>r>>qishi&&r&&c&&qishi)
    {
        memset(path,'A',sizeof(path));
        for(int i=0;i<c;i++)
            scanf("%s",path[i]);
        judge(r,c,qishi-1,0);
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值