每日OJ题_牛客_kotori和迷宫_BFS_C++_Java

目录

牛客_kotori和迷宫_BFS

题目解析

C++代码

Java代码


牛客_kotori和迷宫_BFS

kotori和迷宫

描述:

        kotori在一个n*m迷宫里,迷宫的最外层被岩浆淹没,无法涉足,迷宫内有k个出口。kotori只能上下左右四个方向移动。她想知道有多少出口是她能到达的,最近的出口离她有多远?

输入描述:

第一行为两个整数n和m,代表迷宫的行和列数 (1≤n,m≤30)
        后面紧跟着n行长度为m的字符串来描述迷宫。'k'代表kotori开始的位置,'.'代表道路,'*'代表墙壁,'e'代表出口。保证输入合法。

输出描述:

        若有出口可以抵达,则输出2个整数,第一个代表kotori可选择的出口的数量,第二个代表kotori到最近的出口的步数。(注意,kotori到达出口一定会离开迷宫)

若没有出口可以抵达,则输出-1。


题目解析

BFS迷宫问题小拓展,注意下面的数组的使用。

C++代码

#include <climits>
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;

int m = 0, n = 0;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};
int dist[31][31];

void bfs(vector<vector<char>>& arr, int sr, int sc)
{
    queue<pair<int, int>> q;
    q.push({sr, sc});
    while(q.size())
    {
        auto [a, b] = q.front();
        q.pop();
        for(int i = 0; i < 4; ++i)
        {
            int x = a + dx[i], y = b + dy[i];
            if(x >= 1 && x <= m && y >= 1 && y <= n && dist[x][y] == -1 && arr[x][y] != '*')
            {
                dist[x][y] = dist[a][b] + 1;
                if(arr[x][y] != 'e')
                    q.push({x, y});
            }
        }
    }
}

int main()
{
    memset(dist, -1, sizeof(dist));
    cin >> m >> n;
    vector<vector<char>> arr(m + 1, vector<char>(n + 1));
    int sr = 0, sc = 0;
    for(int i = 1; i <= m; ++i)
    {
        for(int j = 1; j <= n; ++j)
        {
            cin >> arr[i][j];
            if(arr[i][j] == 'k')
                sr = i, sc = j;
        }
    }
    dist[sr][sc] = 0;
    bfs(arr, sr, sc);
    
    int cnt = 0, minStep = INT_MAX;
    for(int i = 1; i <= m; ++i)
    {
        for(int j = 1; j <= n; ++j)
        {
            if(arr[i][j] == 'e' && dist[i][j] != -1)
            {
                ++cnt;
                minStep = min(minStep, dist[i][j]);
            }
        }
    }
    if(cnt == 0)
        cout << -1 << endl;
    else
        cout << cnt << " " << minStep << endl;
    return 0;
}

/*
6 8
e . * . * e . *
. * * . * . * e
. . * k * * . .
* * * . * . e *
. * * . * . * *
* . . . . . . e
*/

Java代码

import java.util.*;
public class Main
{
    public static int N = 35;
    public static int x1, y1; // 记录起始位置
    public static int n, m;
    public static char[][] arr = new char[N][N];
    public static int[][] dist = new int[N][N];

    public static int[] dx = {0, 0, 1, -1};
    public static int[] dy = {1, -1, 0, 0};

    public static void bfs()
    {
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                dist[i][j] = -1;
            }
        }

        Queue<int[]> q = new LinkedList<>();
        q.add(new int[]{x1, y1});
        dist[x1][y1] = 0;

        while(!q.isEmpty())
        {
            int[] tmp = q.poll();
            int a = tmp[0], b = tmp[1];
            for(int i = 0; i < 4; i++)
            {
                int x = a + dx[i], y = b + dy[i];
                if(x >= 0 && x < n && y >= 0 && y < m && arr[x][y] != '*' && dist[x][y] == -1)
                {
                    dist[x][y] = dist[a][b] + 1;
                    if(arr[x][y] != 'e')
                    {
                        q.add(new int[]{x, y});
                    }
                }
            }
        }
    }

    public static void main(String[] s)
    {
        Scanner in = new Scanner(System.in);
        n = in.nextInt(); m = in.nextInt();
        for(int i = 0; i < n; i++)
        {
            char[] tmp = in.next().toCharArray();
            for(int j = 0; j < m; j++)
            {
                arr[i][j] = tmp[j];
                if(arr[i][j] == 'k')
                {
                    x1 = i; y1 = j;
                }
            }
        }

        bfs();

        // 更新结果
        int count = 0, ret = 1000;
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                if(arr[i][j] == 'e' && dist[i][j] != -1)
                {
                    count++;
                    ret = Math.min(ret, dist[i][j]);
                }
            }
        }

        if(count == 0) 
            System.out.println(-1);
        else 
            System.out.println(count + " " + ret);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GR鲸鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值