一、[P1141] 01 迷宫
1. 审题
题目描述
有一个仅由数字 0 0 0 与 1 1 1 组成的 n × n n \times n n×n 格迷宫。若你位于一格 0 0 0 上,那么你可以移动到相邻 4 4 4 格中的某一格 1 1 1 上,同样若你位于一格 1 1 1 上,那么你可以移动到相邻 4 4 4 格中的某一格 0 0 0 上。
你的任务是:对于给定的迷宫,询问从某一格开始能移动到多少个格子(包含自身)。
输入格式
第一行为两个正整数 n , m n,m n,m。
下面 n n n 行,每行 n n n 个字符,字符只可能是 0 0 0 或者 1 1 1,字符之间没有空格。
接下来 m m m 行,每行两个用空格分隔的正整数 i , j i,j i,j,对应了迷宫中第 i i i 行第 j j j 列的一个格子,询问从这一格开始能移动到多少格。
输出格式
m m m 行,对于每个询问输出相应答案。
样例 #1
输入
2 2 01 10 1 1 2 2
输出
4 4
提示
对于样例,所有格子互相可达。
对于 20 % 20\% 20% 的数据, n ≤ 10 n \leq 10 n≤10;
对于 50 % 50\% 50% 的数据, m ≤ 5 m \leq 5 m≤5;
对于 100 % 100\% 100% 的数据, 1 ≤ n ≤ 1000 1\le n \leq 1000 1≤n≤1000, 1 ≤ m ≤ 100000 1\le m \leq 100000 1≤m≤100000。
2. 思路
这道题目是一个典型的泛洪问题,求在一个具有特定规则 n × n n \times n n×n 的正方形迷宫中,指定某个位置根据规则能够行进的所有房间数。当你看到可爱的 100000 100000 100000 的时候,……。
所以我们可以进行记忆化搜索,将每一个点可走的房间数进行记录(以下标为路径 id
,存值为可走的房间数)。因为不难发现,一条路径的每个点可走的房间数就是这条路径其他点的可走房间数。因此这也有点像一种映射。
3. 参考答案
#include <iostream>
#include <queue>
using namespace std;
struct Node
{
int x, y;
};
int n; // 迷宫大小
int m; // 起点数量
int num; // 起点id
int x, y; // 起点坐标
char a[1005][1005]; // 迷宫
int id[1005][1005]; // 每个点的id
int cnt[1000005]; // 每个id可走的房间数
int dx[5] = {-1, 0, 0, 1}; // x偏移量
int dy[5] = {0, -1, 1, 0}; // y偏移量
queue <Node> q; // 结构体队列
// bfs(): 找新的一条路径
void bfs(int x, int y)
{
id[x][y] = ++num; // 标记起点id
q.push({x, y}); // 起点入队
cnt[num] = 1; // 起点也算一个房间
while (!q.empty())
{
// 获取队首元素并出队
auto [fx, fy] = q.front();
q.pop();
// 遍历四个方向
for (int i = 0; i <= 3; i++)
{
int tx = fx+dx[i];
int ty = fy+dy[i];
// 非边界、未标记、颜色不同
if (tx>=1 && tx<=n && ty>=1 && ty<=n && id[tx][ty]==0 && a[fx][fy]!=a[tx][ty])
{
q.push({tx, ty}); // 入队
id[tx][ty] = num; // 标记id
cnt[num]++; // 增加房间数
}
}
}
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
cin >> a[i][j];
while (m--)
{
cin >> x >> y;
if (id[x][y] == 0) bfs(x, y);
cout << cnt[id[x][y]] << endl;
}
return 0;
}
二、1-9 路径
1. 审题
题目描述
现在有一个 n × n n \times n n×n 的迷宫,在迷宫中的数字是 0 − 9 0-9 0−9 的任意一个( 0 0 0 表示不可通过的格子)。现在求从 (1,1) \text{(1,1)} (1,1) 到 (n,n) \text{(n,n)} (n,n),按照 1 − 9 1-9 1−9 的顺序循环的一条最短路径。可以向八个方向移动,如果不能到达输出
-1
。
输入描述
第一行有一个正整数 n n n 表示迷宫的大小。
接下来 n n n 行,每行 n n n 个用空格隔开的整数,数字范围在 0 − 9 0-9 0−9 之间。
输出描述
只有一行,表示最短路径的长度。
样例 #1
输入
5 1 2 0 4 5 2 3 4 5 6 0 0 0 0 7 8 2 0 8 0 0 0 9 1 2
输出
11
提示
1 ≤ n ≤ 100 1 \le n \le 100 1≤n≤100
2. 思路
这道题需要用广搜,因为要找最短路径,直接套上模板。
注意有一个
1
−
9
1-9
1−9 顺序循环,因此我们可以用一个 Next[]
数组,用下标来表示现在的数字,存值表示下一个数字,这样直接判断 a[tx][ty]==Next[a[fx][fy]]
就可以了。
3. 参考答案
#include <iostream>
#include <queue>
using namespace std;
struct Node
{
int x, y;
int step;
};
int n; // 迷宫大小
int a[105][105]; // 迷宫
int Next[15] = {0, 2, 3, 4, 5, 6, 7, 8, 9, 1}; // 下一个数字
int dx[10] = {-1, -1, -1, 0, 0, 1, 1, 1}; // x偏移量
int dy[10] = {-1, 0, 1, -1, 1, -1, 0, 1}; // y偏移量
queue <Node> q; // 结构体队列
void bfs()
{
q.push({1, 1, 1}); // 起点入队
while (!q.empty())
{
// 获取队首并出队
auto [fx, fy, fstep] = q.front();
q.pop();
// 终点
if (fx==n && fy==n)
{
cout << fstep;
return;
}
// 遍历八个方向
for (int i = 0; i <= 7; i++)
{
int tx = fx+dx[i];
int ty = fy+dy[i];
// 边界、不可通过
if (tx<1 || tx>n || ty<1 || ty>n || a[tx][ty]==0)
continue;
// 是否为下一个数字
if (a[tx][ty] == Next[a[fx][fy]])
q.push({tx, ty, fstep+1}); // 入队
}
}
cout << -1;
return;
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
cin >> a[i][j];
bfs();
return 0;
}
三、分割回文串
1. 审题
题目描述
给你一个只包含小写字母的字符串
s
,请你将s
分割成一些子串,使每个子串都是回文串。按照子串切割长度从小到大输出s
所有可能的分割方案。
回文串是正着读和反着读都一样的字符串。
输入描述
一行,字符串
s
。
输出描述
所有可能的分割方案。
样例 #1
输入
aab
输出
a a b aa b
提示
1 ≤ 1≤ 1≤ 字符串长度 ≤ 20 ≤20 ≤20
2. 思路
用深搜(虽然有一股刺鼻的尺取味儿)。关于 dfs()
:
- 参数:分割位置
- 终止条件:分割位置到尾了
- 单层递归逻辑:
遍历可能可能的分割末尾 如果分割出来的部分是回文 搜索下一个位置 回溯
3. 参考答案
#include <iostream>
#include <string>
using namespace std;
string s;
int len;
int pos = 1;
string a[25];
bool isPalin(int l, int r)
{
while (l <= r)
{
if (s[l] != s[r])
return false;
l++; r--;
}
return true;
}
void dfs(int l)
{
if (l == len)
{
for (int i = 1; i < pos; i++)
cout << a[i] << " ";
cout << endl;
return;
}
for (int r = l; r < len; r++)
if (isPalin(l, r))
{
a[pos] = s.substr(l, r-l+1);
pos++;
dfs(r+1);
pos--;
}
}
int main()
{
cin >> s;
len = s.length();
dfs(0);
return 0;
}