zoukankan      html  css  js  c++  java
  • HDU 2102 A计划(三维BFS)

    这题太欢乐了......虽然wa了几次,但是想到骑士在两幅图的传送门中传来传去就觉得这骑士太坑了大笑

    #include <cstdio>
    #include <iostream>
    #include <cmath>
    #include <cstring>
    using namespace std;
    int n,m,cost,head,tail,ans;
    char map[2][15][15];
    int sum[2][15][15];
    struct node {
        int z,x,y;
    } q[11111];
    node st, end;
    
    int dirx[4] = {1,-1,0,0};
    int diry[4] = {0,0,1,-1};
    void init() {
        memset(sum,-1,sizeof(sum));
        ans = 0;
    }
    
    bool go(int z,int x,int y) {
        if(x < 0 || x >= n || y < 0 || y >= m) return false;
        if(map[z][x][y] == '*') return false;
        if(sum[z][x][y] != -1) return false;   //
        return true;
    }
    
    int bfs() {
        head = 0; tail = 0;
        q[head++] = st;
        sum[st.z][st.x][st.y] = 0;
        while(head != tail) {
            node t = q[tail++];
            node tt;
            if(end.z == t.z && end.x == t.x && end.y == t.y) {
                if(cost >= sum[t.z][t.x][t.y]) {
                    return sum[t.z][t.x][t.y];
                }
                else return -1;
            }
            for(int i=0; i<4; i++) {
                tt.z = t.z; tt.x = t.x + dirx[i]; tt.y = t.y + diry[i];
                if(go(tt.z,tt.x,tt.y)) {
                    //cout << tt.z << ' ' << tt.x << ' ' << tt.y << endl;
                    if(map[tt.z][tt.x][tt.y] == '.' || map[tt.z][tt.x][tt.y] == 'P') {
                        sum[tt.z][tt.x][tt.y] = sum[t.z][t.x][t.y] + 1;
                        q[head++] = tt;
                    }
                    if(map[tt.z][tt.x][tt.y] == '#' && map[1 - tt.z][tt.x][tt.y] != '*' && map[1 - tt.z][tt.x][tt.y] != '#'){
                        sum[tt.z][tt.x][tt.y] = sum[t.z][t.x][t.y] + 1;
                        sum[1 - tt.z][tt.x][tt.y] = sum[tt.z][tt.x][tt.y];
                        tt.z = 1 - tt.z;
                        q[head++] = tt;
                    }
                }
            }
        }
        return -1;
    }
    
    int main() {
        int T;
        cin >> T;
        while(T --) {
            init();
            cin >> n >> m >> cost;
            for(int z=0; z<2; z++)
                for(int i=0; i<n; i++)
                    for(int j=0; j<m; j++) {
                        cin >> map[z][i][j];
                        if(map[z][i][j] == 'S') {
                            st.z = z;
                            st.x = i;
                            st.y = j;
                        }
                        if(map[z][i][j] == 'P') {
                            end.z = z;
                            end.x = i;
                            end.y = j;
                        }
                    }
            if(bfs() == -1) printf("NO
    ");
            else printf("YES
    ");
        }
        return 0;
    }
    


  • 相关阅读:
    解决命令行乱码问题(中文乱码)
    Outlook 修改归档文件顶层目录名
    (职员)2015-11-11 星期三 日志
    职员)2015-11-10 星期二 日志
    职员)2015-11-09 星期一 日志
    (职员)2015-11-05 星期四 日志
    (职员)2015-11-04 星期三 日志
    (职员)2015-11-03 星期二 日志
    (职员)2015-11-02 星期一 日志
    (职员)2015-10-31 星期六 周志
  • 原文地址:https://www.cnblogs.com/riskyer/p/3233687.html
Copyright © 2011-2022 走看看