第二十三天:广度优先搜索 ( BFS )

T1

胜利大逃亡
时间限制:2 秒
内存限制:32 兆
特殊判题:否

题目描述: Ignatius 被魔王抓走了,有一天魔王出差去了,这可是 Ignatius 逃亡的好机会.
魔王住在一个城堡里,城堡是一个 ABC 的立方体,可以被表示成 A 个 B*C 的矩阵,刚开始 Ignatius 被关在(0,0,0)的位置,离开城堡的门在(A-1,B-1,C-1)的位置,现在知道魔王将在 T 分钟后回到城堡,Ignatius 每分钟能从一个坐标走到相邻的六个坐标中的其中一个.现在给你城堡的地图,请你计算出 Ignatius 能否在魔王回来前离开城堡(只要走到出口就算离开城堡,如果走到出口的时候魔王刚好回来也算逃亡成功),如果可以请输出需要多少分钟才能离开,如果不能则输出-1。

输入: 输入数据的第一行是一个正整数 K,表明测试数据的数量.每组测试数据的第一行是四个正整数 A,B,C 和 T(1<=A,B,C<=50,1<=T<=1000),它们分别代表城堡的大小和魔王回来的时间.然后是 A 块输入数据(先是第 0 块,然后是第 1 块,第 2 块…),每块输入数据有 B 行,每行有 C 个正整数,代表迷宫的布局,其中 0 代表路,1 代表墙。

输出: 对于每组测试数据,如果 Ignatius 能够在魔王回来前离开城堡,那么请输出他最少需要多少分钟,否则输出-1.

样例输入:
1
3 3 4 20
0 1 1 1
0 0 1 1
0 1 1 1
1 1 1 1
1 0 0 1
0 1 1 1
0 0 0 0
0 1 1 0
0 1 1 0

样例输出:
11

//
//  main.cpp
//  Escape
//
//  Created by Apple on 2019/8/27.
//  Copyright © 2019 Apple_Lance. All rights reserved.
//

#include <iostream>
#include <stdio.h>
#include <queue>
using namespace std;

bool mark[51][51][51];
int maze[51][51][51];

struct N{
    int x, y, z;
    int t;
};

queue<N> Q;

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


int BFS(int a, int b, int c){
    while(!Q.empty()){
        N now = Q.front();
        Q.pop();
        for(int i = 0;i<6;i++){
            int nx = now.x + go[i][0];
            int ny = now.y + go[i][1];
            int nz = now.z + go[i][2];
            if(nx < 0 || nx >= a || ny < 0 || ny >= b || nz < 0 || nz >= c)
                continue;
            if(mark[nx][ny][nz] == true)
                continue;
            if(maze[nx][ny][nz] == 1)
                continue;
            N tmp;
            tmp.x = nx;
            tmp.y = ny;
            tmp.z = nz;
            tmp.t = now.t + 1;
            Q.push(tmp);
            mark[nx][ny][nz] = true;
            if(nx == a - 1 && ny == b - 1 && nz == c - 1)
                return tmp.t;
        }
    }
    return -1;
}


int main(int argc, const char * argv[]) {
    // insert code here...
    int T;
    scanf("%d", &T);
    while(T --){
        int a, b, c, t;
        scanf("%d%d%d%d", &a, &b, &c, &t);
        for(int i = 0;i < a;i++)
            for(int j = 0;j < b;j++)
                for(int k = 0;k < c;k++){
                    scanf("%d", &maze[i][j][k]);
                    mark[i][j][k] = false;
                }
        while(!Q.empty())
            Q.pop();
        N tmp;
        tmp.x = tmp.y = tmp.z = tmp.t = 0;
        Q.push(tmp);
        mark[0][0][0] = true;
        int ans = BFS(a, b, c);
        if(ans <= t)
            printf("%d\n", ans);
        else
            printf("-1\n");
    }
    return 0;
}
/*
 1
 3 3 4 20
 0 1 1 1
 0 0 1 1
 0 1 1 1
 1 1 1 1
 1 0 0 1
 0 1 1 1
 0 0 0 0
 0 1 1 0
 0 1 1 0
 
 */

T2

非常可乐
时间限制:1 秒
内存限制:32 兆
特殊判题:否

题目描述: 大家一定觉的运动以后喝可乐是一件很惬意的事情,但是 seeyou 却不这么认为。因为每次当 seeyou 买了可乐以后,阿牛就要求和 seeyou 一起分享这一瓶可乐,而且一定要喝的和 seeyou 一样多。但 seeyou 的手中只有两个杯子,它们的容量分别是 N 毫升和 M 毫升 可乐的体积为 S (S<101)毫升(正好装满一瓶) ,它们三个之间可以相互倒可乐 (都是没有刻度的,且 S==N+M,101>S >0,N>0,M>0) 。聪明的 ACMER 你们说他们能平分吗?如果能请输出倒可乐的最少的次数,如果不能输出"NO"。

输入: 三个整数 : S 可乐的体积 , N 和 M 是两个杯子的容量,以"0 0 0"结束。

输出: 如果能平分的话请输出最少要倒的次数,否则输出"NO"。

样例输入:
7 4 3
4 1 3
0 0 0

样例输出:
NO
3

//
//  main.cpp
//  DrinkCoca
//
//  Created by Apple on 2019/8/27.
//  Copyright © 2019 Apple_Lance. All rights reserved.
//

#include <iostream>
#include <stdio.h>
#include <queue>
using namespace std;

struct N{
    int a, b, c;
    int t;
};

queue<N> Q;
bool mark[101][101][101];

void AToB(int &a, int sa, int &b, int sb){
    if(sb - b >= a){
        b += a;
        a = 0;
    }
    else{
        a -= sb - b;
        b = sb;
    }
}

int BFS(int s, int n, int m){
    while(!Q.empty()){
        N now = Q.front();
        Q.pop();
        int a, b, c;
        a = now.a;
        b = now.b;
        c = now.c;
        AToB(a, s, b, n);
        if(!mark[a][b][c]){
            mark[a][b][c] = true;
            N tmp;
            tmp.a = a;
            tmp.b = b;
            tmp.c = c;
            tmp.t = now.t + 1;
            if(a == s / 2 && b == s / 2)
                return tmp.t;
            if(a == s / 2 && c == s / 2)
                return tmp.t;
            if(b == s / 2 && c == s / 2)
                return tmp.t;
            Q.push(tmp);
        }
        a = now.a;
        b = now.b;
        c = now.c;
        AToB(b, n, a, s);
        if(!mark[a][b][c]){
            mark[a][b][c] = true;
            N tmp;
            tmp.a = a;
            tmp.b = b;
            tmp.c = c;
            tmp.t = now.t + 1;
            if(a == s / 2 && b == s / 2)
                return tmp.t;
            if(a == s / 2 && c == s / 2)
                return tmp.t;
            if(b == s / 2 && c == s / 2)
                return tmp.t;
            Q.push(tmp);
        }
        a = now.a;
        b = now.b;
        c = now.c;
        AToB(a, s, c, m);
        if(!mark[a][b][c]){
            mark[a][b][c] = true;
            N tmp;
            tmp.a = a;
            tmp.b = b;
            tmp.c = c;
            tmp.t = now.t + 1;
            if(a == s / 2 && b == s / 2)
                return tmp.t;
            if(a == s / 2 && c == s / 2)
                return tmp.t;
            if(b == s / 2 && c == s / 2)
                return tmp.t;
            Q.push(tmp);
        }
        a = now.a;
        b = now.b;
        c = now.c;
        AToB(c, m, a, s);
        if(!mark[a][b][c]){
            mark[a][b][c] = true;
            N tmp;
            tmp.a = a;
            tmp.b = b;
            tmp.c = c;
            tmp.t = now.t + 1;
            if(a == s / 2 && b == s / 2)
                return tmp.t;
            if(a == s / 2 && c == s / 2)
                return tmp.t;
            if(b == s / 2 && c == s / 2)
                return tmp.t;
            Q.push(tmp);
        }
        a = now.a;
        b = now.b;
        c = now.c;
        AToB(b, n, c, m);
        if(!mark[a][b][c]){
            mark[a][b][c] = true;
            N tmp;
            tmp.a = a;
            tmp.b = b;
            tmp.c = c;
            tmp.t = now.t + 1;
            if(a == s / 2 && b == s / 2)
                return tmp.t;
            if(a == s / 2 && c == s / 2)
                return tmp.t;
            if(b == s / 2 && c == s / 2)
                return tmp.t;
            Q.push(tmp);
        }
        a = now.a;
        b = now.b;
        c = now.c;
        AToB(c, m, b, n);
        if(!mark[a][b][c]){
            mark[a][b][c] = true;
            N tmp;
            tmp.a = a;
            tmp.b = b;
            tmp.c = c;
            tmp.t = now.t + 1;
            if(a == s / 2 && b == s / 2)
                return tmp.t;
            if(a == s / 2 && c == s / 2)
                return tmp.t;
            if(b == s / 2 && c == s / 2)
                return tmp.t;
            Q.push(tmp);
        }
        
    }
    return -1;
}


int main(int argc, const char * argv[]) {
    int s, n, m;
    while(scanf("%d%d%d", &s, &n, &m)){
        if(s == 0)
            break;
        if(s % 2 == 1){
            puts("No");
            continue;
        }
        for(int i = 0;i < s;i++)
            for(int j = 0;j < n;j++)
                for(int k = 0;k < m;k++)
                    mark[i][j][k] = false;
        N tmp;
        tmp.a = s;
        tmp.b = tmp.c = tmp.t = 0;
        while(!Q.empty())
            Q.pop();
        Q.push(tmp);
        mark[s][0][0] = true;
        int rec = BFS(s, n, m);
        if(rec == -1)
            printf("NO\n");
        else
            printf("%d\n", rec);
    }
    return 0;
}
发布了182 篇原创文章 · 获赞 101 · 访问量 20万+

猜你喜欢

转载自blog.csdn.net/lancecrazy/article/details/100093659