A计划 HDU2102

9 篇文章 0 订阅

在开始的时候需要将map初始化,要不然会WA,还有就是#不会向四周扩展。 我这种是三维搜索。

 

应该还有一种方法是将两层合并为一层,就是#对应的那层是'.'就改为'.',否则不变。在合并的一层中‘#’不会向四周扩展。P直接改到对应的位置。然后二维搜索就可以了。


#include<iostream>
#include<cstdlib>
#include<queue>
#include<string.h>
#include<cstdio>

using namespace std;

int T, R, C, SL, SR, SC, EL, ER, EC;
char t;
const int N = 10;
char map[3][N+3][N+3];
bool pos[3][N+3][N+3];

int dir[6][3] = {{-1, 0, 0}, {1, 0, 0}, {0, -1, 0}, {0, 1, 0}, {0, 0, -1}, {0, 0, 1}};

struct Node
{
    int l, r, c; //position
    int step;
    char t;
    string path;
    // bool flag;
    Node(){
        l = r = c = 0;
        step = 0;
        t = '.';
        path = "";
//        flag = false;
    }
    Node(int q, int b, int d, int w, char m){
        l = q, r = b, c = d, step = w, t = m;
    }
};
Node tmp1;

int Judge(Node tmp2)
{
    // cout << "first judge 1" <<map[tmp2.l][tmp2.r][tmp2.c] << endl;

    if(map[tmp2.l][tmp2.r][tmp2.c] == '.'|| map[tmp2.l][tmp2.r][tmp2.c] == 'P'|| map[tmp2.l][tmp2.r][tmp2.c] == '#') {
        //   cout << "first judge i2" << endl;
        if (!pos[tmp2.l][tmp2.r][tmp2.c] && tmp2.l <= 2 && tmp2.r <= N && tmp2.c <= N && tmp2.l > 0 && tmp2.r > 0 && tmp2.c > 0) {
            return 1;
        }
    }
    return 0;
}


void bfs()
{
    queue<Node> q;
//    Node tmp(SL, SR, SC, 0, 'S');
    Node tmp;
    tmp.l = SL;
    tmp.r = SR;
    tmp.c = SC;
    tmp.step = 0;
    tmp.t = 'S';
    tmp.path = "S";
    //     cout << tmp.t << endl;
    q.push(tmp);
    pos[SL][SR][SC] = true;

    while(!q.empty()) {
        tmp1 = q.front();
//        cout << tmp1.l << " "<<tmp1.r << " " << tmp1.c << " " << map[tmp1.l][tmp1.r][tmp1.c] << endl;

        if (tmp1.t == 'P') {
            return;
        }
        q.pop();
        Node tmp2;
        //传递
        if (tmp1.t == '#') {
            tmp2.l = 3 - tmp1.l;
            tmp2.r = tmp1.r;
            tmp2.c = tmp1.c;
        //   cout << tmp2.l << " "<<tmp2.r << " " << tmp2.c << "  :" << map[tmp2.l][tmp2.r][tmp2.c] << "   " << pos[tmp2.l][tmp2.r][tmp2.c]  << "  " <<Judge(tmp2)<< endl;
        //judge
        if (Judge(tmp2)) {
//                tmp2.flag = true;
            //  cout << "in" << endl;
//                cout << tmp2.l << " "<<tmp2.r << " " << tmp2.c << "  :" << map[tmp2.l][tmp2.r][tmp2.c] << endl;

                tmp2.step = tmp1.step;


            tmp2.t = map[tmp2.l][tmp2.r][tmp2.c];

            tmp2.path = tmp1.path + tmp2.t;
//                cout << tmp2.t << " ";
            q.push(tmp2);


            pos[tmp2.l][tmp2.r][tmp2.c] = true;

            //  map[tmp2.l][tmp2.r][tmp2.c] = '*';
        }
            continue;
    }
        for(int i=0; i<6; i++){

            tmp2.l = tmp1.l;
            tmp2.r = tmp1.r+dir[i][1];
            tmp2.c = tmp1.c+dir[i][2];
         //   cout << tmp2.l << " "<<tmp2.r << " " << tmp2.c << "  :" << map[tmp2.l][tmp2.r][tmp2.c] << "   " << pos[tmp2.l][tmp2.r][tmp2.c]  << "  " <<Judge(tmp2)<< endl;
            //judge
            if(Judge(tmp2)){
//                tmp2.flag = true;
                //  cout << "in" << endl;
//                cout << tmp2.l << " "<<tmp2.r << " " << tmp2.c << "  :" << map[tmp2.l][tmp2.r][tmp2.c] << endl;
                if(map[tmp1.l][tmp1.r][tmp1.c] == '#')
                {tmp2.step = tmp1.step;

                    //                   cout << tmp1.l << " "<<tmp1.r << " " << tmp1.c << "  :" << '#';
                }
                else
                    tmp2.step = tmp1.step+1;

                tmp2.t = map[tmp2.l][tmp2.r][tmp2.c];

                tmp2.path = tmp1.path+tmp2.t;
//                cout << tmp2.t << " ";
                q.push(tmp2);


                pos[tmp2.l][tmp2.r][tmp2.c] = true;

                //  map[tmp2.l][tmp2.r][tmp2.c] = '*';
            }
        }
//        cout << endl;

    }

}

int main()
{
//
    int n;
    cin >> n;
    while(n--){
        cin >> R >> C >> T;
        memset(pos, false, sizeof(pos));
        memset(map, '*', sizeof(map));

//        int n = 0;

        for(int i=1; i<=2; i++){
            getchar();//层之间的换行符
            for(int j=1; j<=R; j++){
                for(int k=1; k<=C; k++){

                    cin >> map[i][j][k];

                    t = map[i][j][k];

                    //记录起始位置和结束位置
                    if(t == 'S'){
                        SL = i, SR = j, SC = k;
                    }
                    if(t == 'P'){
                        EL = i, ER = j, EC = k;
                    }

                }
                getchar();//行之间的换行符
            }

        }
/*
        for(int i=1; i<=2; i++){
            // getchar();//层之间的换行符
            for(int j=1; j<=R; j++){
                for(int k=1; k<=C; k++){
                    //cin >> map[i][j][k];
                    printf("%c", map[i][j][k]);
                    //记录起始位置和结束位置
                }
                printf("\n");
                // getchar();//行之间的换行符
            }
            printf("\n");
        }

        cout << SL << SR << SC << endl;
*/
        //开始走动
        bfs();

  //            cout << tmp1.path << endl;

        if(tmp1.t == 'P' && tmp1.step <= T){
            printf("YES\n");
    //        printf("Escaped in %d minute(s).\n", tmp1.step);
        }
        else
            printf("NO\n");

    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值