P1141 01迷宫 (bfs)

该博客讨论了一个由0和1组成的迷宫问题,其中0可以移动到1,1可以移动到0。任务是计算从特定格子出发能到达的格子数量。通过使用宽度优先搜索(BFS)策略,可以避免对每个点进行搜索,只需记录每个可到达点的集合。给出了输入输出格式、样例及100%数据规模限制,并提供了相应的解题思路和代码片段。
摘要由CSDN通过智能技术生成

题目描述

有一个仅由数字00与11组成的n \times nn×n格迷宫。若你位于一格0上,那么你可以移动到相邻44格中的某一格11上,同样若你位于一格1上,那么你可以移动到相邻44格中的某一格00上。

你的任务是:对于给定的迷宫,询问从某一格开始能移动到多少个格子(包含自身)。

输入输出格式

输入格式:

 

第11行为两个正整数n,mn,m。

下面nn行,每行nn个字符,字符只可能是00或者11,字符之间没有空格。

接下来mm行,每行22个用空格分隔的正整数i,ji,j,对应了迷宫中第ii行第jj列的一个格子,询问从这一格开始能移动到多少格。

 

输出格式:

 

mm行,对于每个询问输出相应答案。

 

输入输出样例

输入样例#1: 复制

2 2
01
10
1 1
2 2

输出样例#1: 复制

4
4

说明

所有格子互相可达。

对于20\%20%的数据,n≤10n≤10;

对于40\%40%的数据,n≤50n≤50;

对于50\%50%的数据,m≤5m≤5;

对于60\%60%的数据,n≤100,m≤100n≤100,m≤100;

对于100\%100%的数据,n≤1000,m≤100000n≤1000,m≤100000。</

好的,下面是基于 BFS 算法实现迷宫求解的 C 语言代码: ```c #include <stdio.h> #include <stdlib.h> #define MAX_ROW 5 #define MAX_COL 5 struct Point { int row, col; } queue[MAX_ROW * MAX_COL]; int maze[MAX_ROW][MAX_COL] = { {0, 1, 0, 0, 0}, {0, 1, 0, 1, 0}, {0, 0, 0, 0, 0}, {0, 1, 1, 1, 0}, {0, 0, 0, 1, 0}, }; int visited[MAX_ROW][MAX_COL]; int front = 0, rear = 0; void enqueue(struct Point p) { queue[rear++] = p; } struct Point dequeue() { return queue[front++]; } int is_empty() { return front == rear; } int bfs() { struct Point p = {0, 0}; maze[p.row][p.col] = 2; visited[p.row][p.col] = 1; enqueue(p); while (!is_empty()) { struct Point current = dequeue(); if (current.row == MAX_ROW - 1 && current.col == MAX_COL - 1) { return 1; } if (current.col+1 < MAX_COL && maze[current.row][current.col+1] == 0 && !visited[current.row][current.col+1]) { struct Point next = {current.row, current.col+1}; maze[next.row][next.col] = 2; visited[next.row][next.col] = 1; enqueue(next); } if (current.row+1 < MAX_ROW && maze[current.row+1][current.col] == 0 && !visited[current.row+1][current.col]) { struct Point next = {current.row+1, current.col}; maze[next.row][next.col] = 2; visited[next.row][next.col] = 1; enqueue(next); } if (current.col-1 >= 0 && maze[current.row][current.col-1] == 0 && !visited[current.row][current.col-1]) { struct Point next = {current.row, current.col-1}; maze[next.row][next.col] = 2; visited[next.row][next.col] = 1; enqueue(next); } if (current.row-1 >= 0 && maze[current.row-1][current.col] == 0 && !visited[current.row-1][current.col]) { struct Point next = {current.row-1, current.col}; maze[next.row][next.col] = 2; visited[next.row][next.col] = 1; enqueue(next); } } return 0; } void print_maze() { int i, j; printf("Maze:\n"); for (i = 0; i < MAX_ROW; i++) { for (j = 0; j < MAX_COL; j++) { printf("%d ", maze[i][j]); } printf("\n"); } printf("\n"); } int main() { if (bfs()) { printf("Path found!\n"); } else { printf("Path not found!\n"); } print_maze(); return 0; } ``` 这段代码中,我们使用了一个结构体 `Point` 来表示坐标,使用一个数组 `queue` 来实现队列,使用 `visited` 数组来记录已经访问过的位置。 在 `bfs` 函数中,我们首先将起点入队,并将其标记为已访问过。然后在一个循环中,不断从队列中取出元素,分别向四个方向扩展,如果扩展出了终点,就返回 1 表示找到了路径。如果队列为空,说明已经搜索完所有可达的位置,但是没有找到终点,返回 0 表示没有路径。 注意,在扩展一个位置时,需要判断扩展后的位置是否越界、是否是障碍物、是否已经访问过。如果符合要求,将其入队,并将其标记为已访问过,同时在地图上标记为已访问过的位置。 最后,我们输出地图的状态,可以看到所有已访问过的位置都被标记为 2,起点和终点的位置分别为 2 和 0。如果找到了路径,输出 "Path found!",否则输出 "Path not found!"。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值