搜索与图论
DFS
842排列数字
-
最简单的DFS
-
#include <iostream> #include <cstring> #include <algorithm> using namespace std; const int N = 10; int path[N]; //记录路径 bool flag[N]; //记录是否进入path int n; void dfs(int u) { if(u == n) { for (int i = 0; i < n; i ++ ) cout << path[i] << " "; cout << endl; } for (int i = 1; i <= n; i ++ ) { if(!flag[i]) { path[u] = i; flag[i] = true; dfs(u+1); flag[i] = false; // 回溯将状态归正 } } } int main() { cin >> n; dfs(0); return 0; }
843 n皇后问题
-
首先确定每一行只有一个皇后。
-
然后设置是否冲突的标志数组,斜边用在y轴上的截距来表示
-
#include <iostream> #include <cstring> #include <algorithm> using namespace std; const int N = 20; char g[N][N]; bool row[N], col[N], dg[N], udg[N]; //row,col, dg和udg分别代表行、列、对角线和反对角线是否冲突 int n; void dfs(int u) // 每一行只能有一个皇后,u表示第u行 { if(u == n) { for (int i = 0; i < n; i ++ ) cout << g[i] << endl; cout << endl; } for(int i = 0; i < n; i++) { if(!row[i] && !col[i] && !dg[u - i + n] && !udg[u + i]) { g[u][i] = 'Q'; row[i] = true; col[i] = true; dg[u - i + n] = true; udg[u + i] = true; dfs(u + 1); g[u][i] = '.'; //回溯 row[i] = false; col[i] = false; dg[u - i + n] = false; udg[u + i] = false; } } } int main() { cin >> n; for(int i = 0; i < n; i++) { for (int j = 0; j < n; j ++ ) g[i][j] = '.'; } dfs(0); return 0; }
BFS
844.走迷宫
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 110;
int g[N][N]; // g表示地图
int d[N][N]; //d表示从(1,1)处走到这个地图元素的距离
typedef pair<int, int> PII;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}; // dx和dy是方向偏移量
int n, m;
int bfs() // bfs 模板
{
queue<PII> q;
memset(d, -1, sizeof d); //-1 表示没有到过这一点
d[0][0] = 0;
q.push({0, 0});
while(!q.empty())
{
auto t = q.front();
q.pop();
for (int i = 0; i < 4; i ++ )
{
int x = t.first + dx[i];
int y = t.second + dy[i];
if(x >= 0 && x < n && y >= 0 && y < m && d[x][y] == -1 && g[x][y] == 0)
{
d[x][y] = d[t.first][t.second] + 1;
q.push({x,y});
}
}
}
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 >> g[i][j];
cout << bfs();
return 0;
}
845.八数码
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <queue>
using namespace std;
const int N = 10;
string e = "12345678x";
unordered_map<string, int> d; //d表示转移到string时需要移动多少步 采用hash表的方法来记录
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int bfs(string s)
{
queue<string> q;
q.push(s);
while(q.size())
{
auto t = q.front();
q.pop();
int distance = d[t];
if(t == e) return distance; // 如果有答案直接返回,否则视为没有方法变为end,返回-1
int idx = t.find('x'); //查找x的下标
int t_x = idx / 3; //查找x在3*3数组中的横坐标
int t_y = idx % 3; //查找x在3*3数组中的纵坐标
for (int i = 0; i < 4; i ++ )
{
int x = t_x + dx[i];
int y = t_y + dy[i];
swap(t[t_x * 3 + t_y], t[x * 3 + y]); // 现将两个位置上的字符进行交换,判断t的状态
if(x >= 0 && x < 3 && y >= 0 && y < 3 && d[t] == 0) //d[t] == 0 代表没有经过转移的状态
{
q.push(t);
d[t] = distance + 1;
}
swap(t[t_x * 3 + t_y], t[x * 3 + y]); // 将状态返回
}
}
return -1;
}
int main()
{
string start = "";
char c;
for (int i = 0; i < 9; i ++ )
{
cin >> c;
start += c;
}
d[start] = 0;
cout << bfs(start);
return 0;
}
树与图的深度优先遍历
846.树的重心
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
int e[2 * N], ne[2 * N], idx, h[N]; // e 表示插入节点值,h 表示队列头结点需要N个队列来表示N个连通块,idx下标
bool st[N]; //记录当前点有没有被遍历到
int ans = N; // 最大连通数
int n; //总节点数
void add(int a, int b) //将b点连接到a连通块中去
{
e[idx] = b;
ne[idx] = h[a];
h[a] = idx++;
}
int dfs(int u)
{
st[u] = true;
int res = 0; //存储删掉某个节点之后,最大的连通子图节点数
int sum = 1; //表示以u为根的树的节点数
for(int i = h[u]; i != -1; i = ne[i])
{
int j = e[i]; // j为节点值
if(!st[j]) // 不用让状态恢复,因为st[N]开辟了N个大小的数组
{
int s = dfs(j); //j节点的单棵子树节点数 -- 即去掉u的子树的节点数
res = max(res, s); //记录最大联通子图的节点数
sum += s;
}
}
res = max(n - sum, res); // 比较以删掉u为根的树的节点的其余节点 和 删掉u的子节点
ans = min(ans, res);
return sum;
}
int main()
{
cin >> n;
memset(h, -1, sizeof h);
for (int i = 0; i < n - 1; i ++ )
{
int a, b;
cin >> a >> b;
add(a, b), add(b, a);
}
dfs(1);
cout << ans;
return 0;
}
847. 图中点的层次
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 1e5 + 10;
int e[N], ne[N], idx, h[N];
int st[N]; // 表示1到N的距离
int n, m;
void add(int a, int b)
{
e[idx] = b;
ne[idx] = h[a];
h[a] = idx++;
}
int bfs()
{
queue<int> q;
memset(st, -1, sizeof st);
q.push(1);
st[1] = 0;
while(q.size())
{
int t = q.front();
q.pop();
for(int i = h[t]; i != -1; i = ne[i])
{
int k = e[i];
if(st[k] == -1)
{
q.push(k);
st[k] = st[t] + 1;
}
// cout << k << ":" << st[k] << endl;
}
}
return st[n];
}
int main()
{
cin >> n >> m;
memset(h, -1, sizeof h);
while (m -- )
{
int a, b;
cin >> a >> b;
add(a, b);
}
cout << bfs();
return 0;
}