文章目录
王道机试 第9章 搜索
广度优先搜索 BFS
- (1)状态——确定求解问题的状态,通过状态扩展,遍历所有状态,从中寻找需要的答案。
- (2)状态扩展方式——尽可能地扩展状态,并对先扩展得到的状态先进性下一次状态扩展。
- (3)有效状态——对于有些状态,并不需要再次扩展它,而是直接舍弃它。因为根据问题的分析可知,目标状态不可能有这些状态经过若干次状态得到,所以直接舍弃。
- (4)队列——为了使得先得出的状态能够先扩展,于是使用队列,将得到的状态一次放入队尾,每次取出队头元素进行扩展。
广度优先搜索模板
int bfs()
{
queue<struct> q;
memset(vis, false, sizeof(vis));
struct head, next;
初始化head;
vis[head] = true;
q.push(head);
while (!q.empty())
{
now = q.front(); q.pop();
for (int i = 0; i < cases; i++)
{
if (i == ?) 利用now扩展更新next;
else if (i == ??) 利用now更新next;
else 利用now更新next;
if (next不可达或者越界) continue;
if (!vis[next])
{
vis[next] = true;
更新next及其结构体内附加的值比如路径或者长度等;
q.push(next);
}
if (next到达终点) return 答案;
}
}
}
例题9.1 Catch That Cow
- 题目解析
(1)定状态:记(x, step) x为农夫当前坐标,step为总步数
(2)状态表示:利用结构体
(3)bfs细节:剪枝条件为将要扩展的next节点的坐标超出题目给定范围;利用vis数组记录每个坐标是否被访问,若被访问则不再从此节点进行扩展。
(4)坑:本代码C++能够通过POJ和HDU,但是G++无法通过。
C++代码如下:
// 坑:所有的bfs操作都在for循环里面!
#include <iostream>
#include <cstring>
#include <queue>
#include <string>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 100005;
bool vis[maxn];
// 问题:求起点n到终点k的最短耗时
// 定状态:(x, step) x为农夫当前坐标,step为总步数
struct node
{
int x; // position
int step; // 步数
node(int _x, int _step){ // 构造函数
x = _x;
step = _step;
}
};
int bfs(int n, int k){
memset(vis, false, sizeof(vis));
queue<node> q;
vis[n] = true;
q.push(node(n, 0));
while (!q.empty()){
node now = q.front(); // 取队首元素
q.pop(); // 出队列
if (now.x == k){ // 已到达终点
return now.step;
}
for (int i = 0; i < 3; i++) // 3种情况,bfs搜索
{
node next(now.x, now.step + 1);
if (i == 0) next.x -= 1;
else if (i == 1) next.x += 1;
else next.x *= 2;
if (next.x < 0 || next.x > 100000) continue; // 剪枝,超出边界条件
if (!vis[next.x]) // 未被访问
{
vis[next.x] = true; // 标记为已访问
q.push(next); // 新节点入队列
}
}
}
}
int main()
{
int n, k;
while (cin >> n >> k)
{
memset(vis, false, sizeof(vis));
int ans = bfs(n, k);
cout << ans << endl;
}
return 0;
}
例题9.2 Find The Multiple
- 题目解析
- 本题的关键是BFS的扩展方法。对于正整数x,由于其每一位不是1就是0.所以由此得到多出一个位数的下一个状态只能是x * 10或 x * 10 + 1,所以在BFS中遍历这两种状态即可。
- 由于每个扩展出的正整数必定不相等,故而不需要记录每个状态是否已经被搜索到。
C++代码如下:
#include <iostream>
#include <cstring>
#include <queue>
#include <string>
#include <algorithm>
#include <cstdio>
using namespace std;
void bfs(int n){
queue<long long> q;
q.push(1);
while (!q.empty()){
long long now = q.front();
q.pop();
if (now % n == 0){
cout << now << endl;
return;
}
q.push(now * 10);
q.push(now * 10 + 1);
// for (int i = 0; i < 2; i++){
// long long next;
// if (i == 0) next = now * 10;
// else next = now * 10 + 1;
// q.push(next);
// }
}
}
int main()
{
int n;
while (cin >> n && n){
bfs(n);
}
return 0;
}
习题9.1 玛雅人的密码-清华大学复试上机
- 题目解析
(1)利用string的find函数判断子串是否在字符串中:
if (s.find("2012") != string::npos) return true;
else return false;
(2)利用map<string , bool>映射记录当前字符串是否被考察过。
(3)状态转移思路:s[i]和s[i + 1]字符互换,即可得到一个新的字符串。对于该字符串,考察其两两相邻的字符,在bfs中,共遍历n=s.size()次。
C++代码如下:
#include <iostream>
#include <cstring>
#include <queue>
#include <string>
#include <algorithm>
#include <cstdio>
#include <map>
using namespace std;
map<string, bool> vis; // vis数组
bool can(string s){
if (s.find("2012") != string::npos){
return true;
}
return false;
}
struct node{
string str;
int step;
node(string _str, int _step){
str = _str;
step = _step;
}
};
void dfs(string s){
vis.clear();
queue<node> q;
while (!q.empty()) q.pop();
node head(s, 0);
q.push(head);
vis[s] = true;
while (!q.empty()){
node now = q.front();
q.pop();
if (can(now.str)){
// cout << "now.str = " << now.str << endl;
cout << now.step << endl;
return;
}
int n = s.size();
for (int i = 0; i < n - 1; i++){
node next = now;
swap((next.str)[i], (next.str)[i + 1]);
if (!vis[next.str]){
next.step++;
vis[next.str] = true;
q.push(next);
}
}
}
cout << -1 << endl;
}
int main()
{
string s; int n;
while (cin >> n){
cin >> s;
dfs(s);
}
return 0;
}
洛谷 P1135 奇怪的电梯
- 题目思路
- 第 i i i层有两个扩展状态,可上 c [ i ] c[i] c[i]可下 c [ i ] c[i] c[i]层,再根据抵达楼层的限制剪枝即可。
C++代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <string>
using namespace std;
struct node{
int x;
int step;
node(int x_, int step_){
x = x_;
step = step_;
}
};
const int maxn = 205;
bool vis[maxn];
int c[maxn];
int bfs(int a, int b, int N) // 从a楼到b楼
{
memset(vis, false, sizeof(vis));
node head(a, 0);
queue<node> q;
q.push(head);
vis[head.x] = true;
while (!q.empty()){
node now = q.front(); q.pop();
if (now.x == b){
return now.step;
}
for (int i = 0; i < 2; i++){
node next(now.x, now.step);
int pos = next.x;
if (i == 0) next.x += c[pos];
else next.x -= c[pos];
if (next.x < 0 || next.x > N) {
// cout << "c[" << pos << "] = " << c[pos] << endl;
// cout << "next.x = " << next.x << endl;
continue;}
if (!vis[next.x]){
// cout << "succeed, next.x = " << next.x << endl;
next.step++;
vis[next.x] = true;
q.push(next);
}
}
}
return -1;
}
int main()
{
int N, a, b;
while (cin >> N >> a >> b){
memset(c, 0, sizeof(c));
for (int i = 1; i <= N; i++){
cin >> c[i];
}
int ans = bfs(a, b, N);
cout << ans << endl;
}
return 0;
}
洛谷 P1443 马的遍历
- 注意
- 左对齐,宽5格的输出方法:
printf("%-5d", ans[i][j]);
- 跳马有8个方向!
C++代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <string>
using namespace std;
struct node{
int x; int y;
int step;
node(int x_, int y_, int step_){
x = x_;
y = y_;
step = step_;
}
};
const int maxn = 405;
bool vis[maxn][maxn];
int ans[maxn][maxn]; // 存储步数
void bfs(int a, int b, int n, int m) // 从a楼到b楼
{
memset(vis, false, sizeof(vis));
node head(a, b, 0);
queue<node> q;
q.push(head);
vis[head.x][head.y] = true;
while (!q.empty()){
node now = q.front(); q.pop();
for (int i = 0; i < 8; i++){ // 马走日,8个方向
node next(now.x, now.y, now.step);
if (i == 0) {next.x += 1; next.y += 2;}
else if (i == 1) {next.x += 2; next.y += 1;}
else if (i == 2) {next.x += 1; next.y -= 2;}
else if (i == 3) {next.x += 2; next.y -= 1;}
else if (i == 4) {next.x -= 1; next.y += 2;}
else if (i == 5) {next.x -= 2; next.y += 1;}
else if (i == 6) {next.x -= 1; next.y -= 2;}
else if (i == 7) {next.x -= 2; next.y -= 1;}
if (next.x <= 0 || next.y <= 0 || next.x > n || next.y > m) continue;
if (!vis[next.x][next.y]){
next.step++;
ans[next.x][next.y] = next.step;
vis[next.x][next.y] = true;
q.push(next);
}
}
}
return;
}
int main()
{
int n, m, a, b;
while (cin >> n >> m >> a >> b){
memset(ans, 0, sizeof(ans));
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++){
ans[i][j] = -1;
}
}
ans[a][b] = 0;
bfs(a, b, n, m);
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++){
printf("%-5d", ans[i][j]); // 左对齐,宽5格
}
cout << endl;
}
}
return 0;
}