• 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;
    }
    


  • 相关阅读:
    Qt 交叉编译经典错误——头文件包含
    Linux-Qt使用QThread多线程isRunning标志量问题
    个人总结——C、C++指针传参和初始化字符空间
    ARM板设置开机自启动应用程序
    python--ModuleFoundError
    php输出错误屏蔽的函数
    类QQ账号生成阐述
    Python基础(四)—日期类型
    Python基础(三)—字典和集合
    Python基础(二)—列表和元组
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3235520.html
Copyright © 2020-2023  润新知