BFS- 迷宫中的英雄

BFS- 迷宫中的英雄


500年前,Jesse是我国最卓越的剑客。他英俊潇洒,而且机智过人^_^。

突然有一天,Jesse心爱的公主被魔王困在了一个巨大的迷宫中。Jesse听说这个消息已经是两天以后了,他知道公主在迷宫中还能坚持T天,他急忙赶到迷宫,开始到处寻找公主的下落。 时间一点一点的过去,Jesse还是无法找到公主。最后当他找到公主的时候,美丽的公主已经死了。从此Jesse郁郁寡欢,茶饭不思,一年后追随公主而去了。T_T 500年后的今天,Jesse托梦给你,希望你帮他判断一下当年他是否有机会在给定的时间内找到公主。
他会为你提供迷宫的地图以及所剩的时间T。请你判断他是否能救出心爱的公主。

题目包括多组测试数据。 每组测试数据以三个整数N,M,T(00)开头,分别代表迷宫的长和高,以及公主能坚持的天数。 紧接着有M行,N列字符,由”.”,”“,”P”,”S”组成。其中 “.” 代表能够行走的空地。 “” 代表墙壁,Jesse不能从此通过。 “P” 是公主所在的位置。 “S” 是Jesse的起始位置。 每个时间段里Jesse只能选择“上、下、左、右”任意一方向走一步。 输入以0 0 0结束。

如果能在规定时间内救出公主输出“YES”,否则输出“NO”。

样例1

输入:

4 4 10
….
….
….
S**P
0 0 0
输出:

YES


蛋疼,确实是简单的bfs,不过这里有个坑,就是M和N的输入顺序。。。。
题目链接

#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int sx, sy, gx, gy;
int N, M, T;
int book[222][222];
char s[222][222];
int next1[4][2] = {
    {0, 1},
    {1, 0},
    {0, -1},
    {-1, 0}
};
struct node {
    int x;int y;int T;
};
int bfs() {
    int res = 9999999;
    bool f = false;
    memset(book, 0, sizeof(book));
    queue<node> q;
    node n1;
    n1.x = sx;n1.y = sy;n1.T = 0;
    q.push(n1);
    book[sx][sy] = 1;
    while(q.size()) {
        node st = q.front();q.pop();
        if (st.x == gx && st.y == gy && st.T <= T) {
            puts("YES");
            f = true;
            break;
        }
        for (int i = 0; i < 4; i++) {
            int tx = st.x + next1[i][0], ty = st.y + next1[i][1];
            if (tx >= 0 && tx < N && ty >= 0 && ty < M && s[tx][ty] != '*' && book[tx][ty] == 0) {
                node n2;n2.x = tx;n2.y = ty;n2.T = st.T+1;
                book[tx][ty] = 1;
                q.push(n2);
            }
        }
    }
    if (!f) puts("NO");
    return res;
}
int main() {
    while(true) {
        scanf("%d%d%d", &M, &N, &T);getchar();
        if (N == 0 && M == 0 && T == 0) break;
        for(int i=0; i<N; i++){
            for(int j=0; j<M;j++){
                scanf("%c", &s[i][j]);
                if(s[i][j]=='S'){
                    sx = i; sy = j;
                }
                else if(s[i][j]=='P'){
                    gx = i; gy = j;
                }
            }
            getchar();
       }
      bfs();
   }
    return 0;
}

第二种方法:

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int N, M, T, sx, sy, gx, gy;
struct node {
    int x;
    int y;
    int t;
};
char s[999][999];
int book[999][999] = {0};
int next1[4][2] = {{1,0}, {0,1}, {-1,0}, {0,-1}};
void bfs() {
    struct node que[333*333];
    int tx, ty, head, tail, flag = 0;
    memset(book, 0, sizeof(book));
    head = 1, tail = 1;
    que[tail].x = sx;
    que[tail].y = sy;
    que[tail].t = 0;
    book[sx][sy] = 1;
    tail++;
    while(head < tail) {
        for (int i = 0; i < 4; i++) {
            tx = que[head].x + next1[i][0];
            ty = que[head].y + next1[i][1];
            if (tx < 0 || tx >= N || ty < 0 || ty >= M) continue;
            if (book[tx][ty] == 0 && s[tx][ty] != '*') {
                book[tx][ty] = 1;
                que[tail].x = tx;
                que[tail].y = ty;
                que[tail].t = que[head].t+1;
                tail++;
            }
            if (que[head].x == gx && que[head].y == gy && que[head].t <= T) {
                flag = 1;break;
            }
        }
        if (flag) break;
        head++;
    }
    if (flag) puts("YES");
    else puts("NO");
}
int main() {
    //freopen("in.txt", "r", stdin);
    while(true) {
        scanf("%d%d%d", &M, &N, &T);
        getchar();
        if (N == 0 && M == 0 && T == 0) break;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++){
                scanf("%c", &s[i][j]);
                if (s[i][j] == 'S') {
                    sx = i; sy = j;
                } else if (s[i][j] == 'P') {
                    gx = i; gy = j;
                }
            }
            getchar();
        }
        bfs();
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值