zoukankan      html  css  js  c++  java
  • HDU 4166 & BNU 32715 Robot Navigation (记忆化bfs)


    题意:给一个二维地图,每个点为障碍或者空地,有一个机器人有三种操作:1、向前走;2、左转90度;3、右转90度。现给定起点和终点,问到达终点最短路的条数。

    思路:一般的题目只是求最短路的长度,但本题还要求出相应的条数。比赛时只记录最少的步数,却没有记录以最少步数到达该点的的条数,让他们一直入队.......铁定tle.......

    只要记录好到达该点最少步数的条数,减少了很多重复入队..........

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <string>
    #include <algorithm>
    #include <queue>
    #define MAX 1111
    #define INF 0x7FFFFFFF
    using namespace std;
    
    int n,m,mod;
    struct node {
        int x,y,dir;
    } st,end;
    
    queue <node> q;
    char map[MAX][MAX];
    int num[MAX][MAX][4]; //步数
    int cnt[MAX][MAX][4]; //多少种路径以该方向到达该点
    int dx[] = {-1,0,1,0};
    int dy[] = {0,1,0,-1};
    
    void init() {
        while(!q.empty()) q.pop();
        memset(num,-1,sizeof(num));
        memset(cnt,0,sizeof(cnt));
    }
    
    int getdir(char c) {
        if(c == 'N') return 0;
        if(c == 'E') return 1;
        if(c == 'S') return 2;
        if(c == 'W') return 3;
    }
    
    bool ok(int x,int y) {
        if(x >= 0 && x < n && y >= 0 && y < m && map[x][y] == '.') return true;
        return false;
    }
    
    void bfs() {
        num[st.x][st.y][st.dir] = 0;
        cnt[st.x][st.y][st.dir] = 1;
        q.push(st);
        while(! q.empty()) {
            node t = q.front();
            q.pop();
            node tt;
            //cout << t.x << ' ' << t.y << ' ' << num[t.x][t.y][t.dir] << endl;
            for(int i=0; i<4; i++) { //转方向
                if(abs(t.dir - i) == 1 || abs(t.dir - i) == 3) {
                    tt = t;
                    if(num[t.x][t.y][i] == -1) {
                        num[t.x][t.y][i] = num[t.x][t.y][t.dir] + 1;
                        cnt[t.x][t.y][i] = cnt[t.x][t.y][t.dir];
                        tt.dir = i;
                        q.push(tt);
                    } else if(num[t.x][t.y][i] == num[t.x][t.y][t.dir] + 1) {
                        cnt[t.x][t.y][i] = (cnt[t.x][t.y][i] + cnt[t.x][t.y][t.dir]) % mod;
                    }
                }
            }
            tt = t;
            tt.x += dx[tt.dir];
            tt.y += dy[tt.dir];
            if(ok(tt.x,tt.y)) {
                if(num[tt.x][tt.y][tt.dir] == -1) { //移动
                    num[tt.x][tt.y][tt.dir] = num[t.x][t.y][t.dir] + 1;
                    cnt[tt.x][tt.y][tt.dir] = cnt[t.x][t.y][t.dir];
                    q.push(tt);
                } else if(num[tt.x][tt.y][tt.dir] == num[t.x][t.y][t.dir] + 1) {
                    cnt[tt.x][tt.y][tt.dir] = (cnt[tt.x][tt.y][tt.dir] + cnt[t.x][t.y][t.dir]) % mod;
                }
            }
        }
    }
    
    void solve() {
        int ans = INF;
        for(int i=0; i<4; i++) {
            if(num[end.x][end.y][i] != -1) {
                ans = min(ans,num[end.x][end.y][i]);
            }
        }
        if(ans == INF) {
            printf("%d -1
    ",mod);
            return ;
        }
        int sum = 0;
        for(int i=0; i<4; i++) {
            if(num[end.x][end.y][i] == ans) sum = (sum + cnt[end.x][end.y][i]) % mod;
        }
        printf("%d %d
    ",mod,sum);
    }
    int main() {
        char c;
        int ca = 1;
        while(scanf("%d%d%d",&n,&m,&mod) != EOF) {
            if(mod == 0) break;
            init();
            for(int i=0; i<n; i++) scanf("%s",map[i]);
            scanf("%d%d%d%d",&st.x,&st.y,&end.x,&end.y);
            scanf(" %c",&c);
            st.dir = getdir(c);
            bfs();
            printf("Case %d: ",ca++);
            solve();
        }
        return 0;
    }
    





  • 相关阅读:
    A*算法在栅格地图上的路径搜索(python实现)_1.1
    python基础
    Celery ---异步任务,定时任务,周期任务
    Flask-Script
    Flask-SQLAlchemy
    SQLAlchemy的增删改查 一对多 多对多
    Django Rest framework
    django之forms组件
    缓存, 队列(Redis,RabbitMQ)
    django框架(2)
  • 原文地址:https://www.cnblogs.com/james1207/p/3304132.html
Copyright © 2011-2022 走看看