POJ 1573 Robot Motion 模拟机器人行走

显然这是一道模拟题,但是对我来说还算是比较难的一道模拟题把。
模拟机器人走的方向,判断从起点到出去要多少步,如果出不去,则判断机器人在里面走了一个多少步的环路。
我采用的普通模拟,但是后面又写了一份递归模拟

传送门:POJ-1573-Robot Motion


// 普通模拟
#include <stdio.h>
#include <string.h>
int map[12][12];
int vis[12][12];
int step[12][12];
int fx[5][2]={{0,0},{-1,0},{0,1},{1,0},{0,-1}};
int main() {
    int i,j,k,n,m,tmp,flag;
    int x,y,rk,bf,loops;
    char s[12];
    while(scanf("%d%d%d",&n,&m,&rk) && (n || m || rk)) {
        memset(vis, 0, sizeof(vis));
        memset(step, 0, sizeof(step));
        for(i=1; i<=n; i++) {
            scanf("%s",s);
            for(j=1; j<=m; j++) {
                if(s[j-1] == 'N')
                    map[i][j] = 1;
                else if(s[j-1] == 'E')
                    map[i][j] = 2;
                else if(s[j-1] == 'S')
                    map[i][j] = 3;
                else
                    map[i][j] = 4;
            }
        }
        x = 1,y = rk,flag = 0;
        step[x][y]++;
        vis[x][y] = 1;
        while(x>0 && x<=n && y>0 && y<=m) {
            tmp = step[x][y];
            if(map[x][y] == 1) {
                x -= 1;
            } else if(map[x][y] == 2) {
                y += 1;
            } else if(map[x][y] == 3) {
                x += 1;
            } else {
                y -= 1;
            }
            if(!vis[x][y]) {
                step[x][y] = tmp + 1;
                vis[x][y] = 1;
            } else {
                flag = 1;
                bf = step[x][y] - 1;
                loops = tmp - step[x][y] + 1;
                break;
            }
        }
        if(!flag) {
            printf("%d step(s) to exit\n",step[x][y]-1);
        } else {
            printf("%d step(s) before a loop of %d step(s)\n",bf,loops);
        }
    }

    return 0;
}

// 递归模拟
#include <stdio.h>
#include <string.h>
int map[12][12];
int vis[12][12];
int step[12][12];
int fx[5][2]={{0,0},{-1,0},{0,1},{1,0},{0,-1}};
int i,j,k,n,m,flag;
int x,y,rk,bf,loops;
char s[12];
void go(int x,int y) {
    int tmp = step[x][y];
    if(map[x][y] == 1)
        x -= 1;
    else if(map[x][y] == 2)
        y += 1;
    else if(map[x][y] == 3)
        x += 1;
    else
        y -= 1;
    if(!(x>0 && x<=n && y>0 && y<=m)) {
        printf("%d step(s) to exit\n",tmp);
        return;
    }
    if(vis[x][y]) {
        printf("%d step(s) before a loop of %d step(s)\n",step[x][y]-1,tmp-step[x][y]+1);
        return;
    } else {
        vis[x][y] = 1;
        step[x][y] = tmp+1;
        go(x,y);
    }
}

int main() {
    while(scanf("%d%d%d",&n,&m,&rk) && (n || m || rk)) {
        memset(vis, 0, sizeof(vis));
        memset(step, 0, sizeof(step));
        for(i=1; i<=n; i++) {
            scanf("%s",s);
            for(j=1; j<=m; j++) {
                if(s[j-1] == 'N')
                    map[i][j] = 1;
                else if(s[j-1] == 'E')
                    map[i][j] = 2;
                else if(s[j-1] == 'S')
                    map[i][j] = 3;
                else
                    map[i][j] = 4;
            }
        }
        x = 1,y = rk,flag = 0;
        step[x][y] = 1;
        vis[x][y] = 1;
        go(x,y);
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值