目录
1、DFS:
排列数字:
用 path 数组保存排列,当排列的长度为 n 时,是一种方案,输出。
用 str数组表示数字是否用过。当 str[i] 为 true 时:i 已经被用过,str[i] 为 false 时,i 没有被用过。
dfs(i) 表示的含义是:在 path[i] 处填写数字,然后递归的在下一个位置填写数字。
回溯:第 i 个位置填写某个数字的所有情况都遍历后, 第 i 个位置填写下一个数字。
#include <iostream>
using namespace std;
const int N = 7;
int n, path[N];
bool str[N]; //用来标记哪些数已经用过
void dfs(int u)
{
if (u == n)
{
for (int i = 0; i < n; i++)
cout << path[i] << " ";
cout << endl;
return;
}
//遍历每个数,找到没用过的第一个数
for (int i = 1; i <= n; i++)
{
if (!str[i])
{
path[u] = i;
str[i] = true;
dfs(u + 1);
//回溯
path[u] = 0;
str[i] = false;
}
}
}
int main()
{
cin >> n;
dfs(0);
return 0;
}
https://www.acwing.com/problem/content/844/
n-皇后问题:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 10;
// q[i]用来记录第i行的皇后在哪一列
int q[N], n;
void dfs(int u)
{
if (u == n)
{
for (int i = 0; i < n; i++)
{
int j = 1;
for (; j < q[i]; j++)
cout << ".";
cout << "Q";
j++;
for (; j <= n; j++)
cout << ".";
cout << endl;
}
cout << endl;
return;
}
for (int i = 1; i <= n; i++)
{
bool Flag = false;
for (int j = 0; j < u; j++)
{
//判断某一行的第i列是否有皇后或者所在对角线是否有皇后
if (q[j] == i || (u - j) == (i - q[j]) || (u + i) == (j + q[j]))
{
Flag = true;
break;
}
}
if (Flag)
continue;
q[u] = i;
dfs(u + 1);
q[u] = 0;
}
}
int main()
{
cin >> n;
dfs(0);
return 0;
}
https://www.acwing.com/problem/content/845/
2、BFS:
广度优先搜索:用一个队列存储可能出现的状态,进行状态转移(考虑怎么在队列中存储能够表示当前的状态的信息)。
走迷宫:
用一个二维数组来存储迷宫,另一个二维数组来表示每个点到起点的距离;
队列中存储每个点的横纵坐标。
#include <iostream>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 105;
int q[N][N], d[N][N];//q用来存迷宫 d用来存储每个点到起点的距离
int n, m;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int bfs()
{
queue<pair<int, int>> que;
que.push({0, 0});
memset(d, -1, sizeof d);
d[0][0] = 0;
while (!que.empty())
{
int x = que.front().first;
int y = que.front().second;
que.pop();
for (int i = 0; i < 4; i++)
{
int newx = x + dx[i];
int newy = y + dy[i];
if (newx >= 0 && newx < n && newy >= 0 && newy < m && q[newx][newy] == 0 && d[newx][newy] == -1)
{
d[newx][newy] = d[x][y] + 1;
que.push({newx, newy});
}
}
}
return d[n - 1][m - 1];
}
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> q[i][j];
cout << bfs() << endl;
return 0;
}
https://www.acwing.com/problem/content/846/
八数码:
将状态存储成一个字符串,用一个哈希表存储每个状态和起点之间的“距离”;
找到字符串中x字符的索引,将一维索引转换到二维,再做BFS。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <unordered_map>
using namespace std;
string start = "", endstr = "12345678x";
unordered_map<string, int> d; //记录每个状态到起点的距离
queue<string> que;
int bfs()
{
que.push(start);
d[start] = 0;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
while (!que.empty())
{
auto t = que.front();
int distance = d[t];
que.pop();
if (t == endstr)
return distance;
int k = t.find('x');
int x = k / 3, y = k % 3;
for (int i = 0; i < 4; i++)
{
int newx = x + dx[i], newy = y + dy[i];
if (newx >= 0 && newx < 3 && newy >= 0 && newy < 3)
{
swap(t[k], t[newx * 3 + newy]);
if (!d.count(t))
{ //如果t为没有出现过的新状态,则加入队列
d[t] = distance + 1;
que.push(t);
}
swap(t[k], t[newx * 3 + newy]); //还原到交换前的状态
}
}
}
return -1;
}
int main()
{
for (int i = 1; i <= 9; i++)
{
char c;
cin >> c;
start += c;
}
cout << bfs() << endl;
return 0;
}