poj 1573 Robot Motion

action_map纪录经过每个点的次数

但其实只可能是0,1只要再次经过标记为1的点时就一定是走到圈里了

刚开始写的++,不是直接附值为1,就超时了……竟然超时了!!!

然后改了又wa……嗯……是没有初始化

哇靠其实应该不用也没事啊???

只能说c++是一个神奇的语言……

#include <iostream>
#include <cstdio>
using namespace std;

char map[12][12];
int action_map[12][12];
int row,width,spot;
int step;   //the number of the steps
int loop;   //counting the steps of the loop

int action_in_loop(int i,int j)
{
    action_map[i][j]=2;
    while (i>=1&&i<=row&&j>=1&&j<=width) {
        if (map[i][j]=='N') {
            if (2==action_map[i-1][j]) {
                return 0;
            }
            action_map[i-1][j]++;
            loop++;
            i--;
        }
        if (map[i][j]=='S') {
            if (2==action_map[i+1][j]) {
                return 0;
            }
            action_map[i+1][j]++;
            loop++;
            i++;
        }
        if (map[i][j]=='E') {
            if (2==action_map[i][j+1]) {
                return 0;
            }
            action_map[i][j+1]++;
            loop++;
            j++;
        }
        if (map[i][j]=='W') {
            if (2==action_map[i][j-1]) {
                return 0;
            }
            action_map[i][j-1]++;
            loop++;
            j--;
        }
    }
    return 1;
}

int action_in_map(int start)    //the situation that the robot move in the map
{
    action_map[1][start]=1;
    int i=1,j=start;
    while (i>=1&&i<=row&&j>=1&&j<=width) {
        if (map[i][j]=='N') {
            if (1==action_map[i-1][j]) {
                action_in_loop(i, j);
                return 0;
            }
            action_map[i-1][j]=1;
            step++;
            i--;
        }
        if (map[i][j]=='S') {
            if (1==action_map[i+1][j]) {
                action_in_loop(i, j);
                return 0;
            }
            action_map[i+1][j]=1;
            step++;
            i++;
        }
        if (map[i][j]=='E') {
            if (1==action_map[i][j+1]) {
                action_in_loop(i, j);
                return 0;
            }
            action_map[i][j+1]=1;
            step++;
            j++;
        }
        if (map[i][j]=='W') {
            if (1==action_map[i][j-1]) {
                action_in_loop(i, j);
                return 0;
            }
            action_map[i][j-1]=1;
            step++;
            j--;
        }
    }
    return 1;
}

int main(int argc, const char * argv[]) {
    int spot;
    char m;
    while (cin>>row>>width>>spot) {
        if (row==0&&width==0&&spot==0) {
            return 0;
        }
        memset(map,0,sizeof(map));
        memset(action_map,0,sizeof(map));
        int i,j;
        step=0,loop=0;
        getchar();
        for (i=1; i<=row; i++)
        {
            for (j=1; j<=width; j++)
            {
                m=getchar();
                map[i][j]=m;
                action_map[i][j]=0;
            }
            getchar();
        }
        /*for (i=1; i<=row; i++)
        {
            for (j=1; j<=width; j++)
            {
                cout<<action_map[i][j]<<" ";
            }
            cout<<endl;
        }*/
        if(action_in_map(spot)==1)
            cout<<step<<" step(s) to exit"<<endl;
        else
            cout<<step-loop<<" step(s) before a loop of "<<loop+1<<" step(s)"<<endl;
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值