[POJ2157]Maze

本文介绍了一款迷宫寻宝游戏的算法实现,玩家需要找到所有钥匙打开门并最终到达宝藏位置。采用广度优先搜索策略,确保能够正确判断是否可以成功获取宝藏。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Time Limit: 2000MS
Memory Limit: 65536K

Description
Acm, a treasure-explorer, is exploring again. This time he is in a special maze, in which there are some doors (at most 5 doors, represented by ‘A’, ‘B’, ‘C’, ‘D’, ‘E’ respectively). In order to find the treasure, Acm may need to open doors. However, to open a door he needs to find all the door’s keys (at least one) in the maze first. For example, if there are 3 keys of Door A, to open the door he should find all the 3 keys first (that’s three ‘a’s which denote the keys of ‘A’ in the maze). Now make a program to tell Acm whether he can find the treasure or not. Notice that Acm can only go up, down, left and right in the maze.

Input
The input consists of multiple test cases. The first line of each test case contains two integers M and N (1 < N, M < 20), which denote the size of the maze. The next M lines give the maze layout, with each line containing N characters. A character is one of the following: ‘X’ (a block of wall, which the explorer cannot enter), ‘.’ (an empty block), ‘S’ (the start point of Acm), ‘G’ (the position of treasure), ‘A’, ‘B’, ‘C’, ‘D’, ‘E’ (the doors), ‘a’, ‘b’, ‘c’, ‘d’, ‘e’ (the keys of the doors). The input is terminated with two 0’s. This test case should not be processed.

Output
For each test case, in one line output “YES” if Acm can find the treasure, or “NO” otherwise.

Sample Input

4 4 
S.X. 
a.X. 
..XG 
.... 
3 4 
S.Xa 
.aXB 
b.AG 
0 0

Sample Output

YES 
NO

题意:
给一张n*m的图,S表示起点,X表示障碍,不能走,A,B,C,D,E表示门,只有拿到所有对应的钥匙才能打开相应的门(比如,要打开A,必须先拿到地图上所有a,其他门也一样。)询问能否拿到宝藏(G处)。能则输出YES,不能则输出NO

题解:
从起点开始bfs,如果这遍能得到钥匙,则再从起点开始再次遍历,直到一次遍历找不到钥匙为止。

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cmath>
#define LiangJiaJun main
#define pa pair<int,int>
using namespace std;
int n,m;
bool iskey(char c){
     if(c=='a'||c=='b'||c=='c'||c=='d'||c=='e')return 1;
     return 0;
}
bool isdoor(char c){
     if(c=='A'||c=='B'||c=='C'||c=='D'||c=='E')return 1;
     return 0;
}
char mp[24][24];
int hav[14],g[14];
int dx[4]={0,1,0,-1},
    dy[4]={1,0,-1,0};
pa st;
queue<pa>q;
bool vis[24][24];
bool pd;
int bfs(pa be){
    int get=0;
    q.push(be);
    memset(vis,0,sizeof(vis));
    vis[be.first][be.second]=1;
    while(!q.empty()){
        pa now=q.front();q.pop();
        for(int i=0;i<4;i++){
            int nowx=now.first+dx[i],nowy=now.second+dy[i];
            if(nowx<=n&&nowx>0&&nowy<=m&&nowy>0&&mp[nowx][nowy]!='X'&&!vis[nowx][nowy]){
                if(isdoor(mp[nowx][nowy])){
                    if(hav[mp[nowx][nowy]-'A']<=g[mp[nowx][nowy]-'A']){
                        vis[nowx][nowy]=1;
                        q.push(make_pair(nowx,nowy));
                        mp[nowx][nowy]='.';
                    }
                }
                else{
                    if(mp[nowx][nowy]=='G'){pd=1;break;}
                    vis[nowx][nowy]=1;
                    if(iskey(mp[nowx][nowy])){
                        ++g[mp[nowx][nowy]-'a'];
                        mp[nowx][nowy]='.';
                        ++get;
                    }
                    q.push(make_pair(nowx,nowy));
                }
            }
        }
    }
    return get;
}
int w33ha(){
    for(int i=1;i<=n;i++)scanf("%s",mp[i]+1);
    memset(hav,0,sizeof(hav));
    memset(g,0,sizeof(g));
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(iskey(mp[i][j]))hav[mp[i][j]-'a']++;
            if(mp[i][j]=='S')st=make_pair(i,j);
        }
    }
    pd=0;
    while(bfs(st));
    if(!pd)puts("NO");
    else puts("YES");
    return 0;
}
int LiangJiaJun(){
    while(scanf("%d%d",&n,&m)!=EOF){
        if(n==0&&m==0)break;
        w33ha();
    }
    return 0;
}
根据提供的引用内容,可以得知这是一道关于迷宫问题的题目,需要使用Java语言进行编写。具体来说,这道题目需要实现一个迷宫的搜索算法,找到从起点到终点的最短路径。可以使用广度优先搜索或者深度优先搜索算法来解决这个问题。 下面是一个使用广度优先搜索算法的Java代码示例: ```java import java.util.*; public class Main { static int[][] maze = new int[5][5]; // 迷宫地图 static int[][] dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; // 方向数组 static boolean[][] vis = new boolean[5][5]; // 标记数组 static int[][] pre = new int[5][5]; // 记录路径 public static void main(String[] args) { Scanner sc = new Scanner(System.in); for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { maze[i][j] = sc.nextInt(); } } bfs(0, 0); Stack<Integer> stack = new Stack<>(); int x = 4, y = 4; while (x != 0 || y != 0) { stack.push(x * 5 + y); int t = pre[x][y]; x = t / 5; y = t % 5; } stack.push(0); while (!stack.empty()) { System.out.print(stack.pop() + " "); } } static void bfs(int x, int y) { Queue<Integer> qx = new LinkedList<>(); Queue<Integer> qy = new LinkedList<>(); qx.offer(x); qy.offer(y); vis[x][y] = true; while (!qx.isEmpty()) { int tx = qx.poll(); int ty = qy.poll(); if (tx == 4 && ty == 4) { return; } for (int i = 0; i < 4; i++) { int nx = tx + dir[i][0]; int ny = ty + dir[i][1]; if (nx >= 0 && nx < 5 && ny >= 0 && ny < 5 && maze[nx][ny] == 0 && !vis[nx][ny]) { vis[nx][ny] = true; pre[nx][ny] = tx * 5 + ty; qx.offer(nx); qy.offer(ny); } } } } } ``` 该代码使用了广度优先搜索算法,首先读入迷宫地图,然后从起点开始进行搜索,直到找到终点为止。在搜索的过程中,使用标记数组记录已经访问过的位置,使用路径数组记录路径。最后,使用栈来输出路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值