学习视频:16-抽象深度优先搜索练习_哔哩哔哩_bilibili
Q:方程的解数
#include<iostream>
using namespace std;
int k[5];
int p[5];
int n;
int m;
int ans = 0;
void dfs(int x,long long s) {
if (x == n) {
if (s == 0) {
ans++;
return;
}
return;
}
for (int i = 1; i <= m; i++) {
long long xp = 1;
for (int j = 0; j < p[x]; j++)
xp *= i;
dfs(x + 1, s + k[x] * xp);
}
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> k[i] >> p[i];
}
dfs(0, 0);
cout << ans;
return 0;
}
/*
3
100
1 2
-1 2
1 2
*/
Q:数独
#include<iostream>
using namespace std;
char ch[10][10];
bool visx[10][10];
bool visy[10][10];
bool vis[10][10];
bool f;
void dfs(int x, int y) {
if (f)
return;
if (x == 9) {
f = true;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
cout << ch[i][j] << " ";
}
cout << endl;
}
return;
}
if (y == 9) {
dfs(x + 1, 0);
return;
}
if (ch[x][y] != '*') {
dfs(x, y + 1);
return;
}
for (int i = 1; i <= 9; i++) {
if (!visx[x][i] && !visy[y][i] && !vis[x / 3 * 3 + y / 3][i]) {
visx[x][i] = true;
visy[y][i] = true;
vis[x / 3 * 3 + y / 3][i] = true;
ch[x][y] = i + '0'; //!!!
dfs(x, y + 1);
visx[x][i] = visy[y][i] = vis[x / 3 * 3 + y / 3][i] = false;
ch[x][y] = '*';
}
}
}
int main() {
int c;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
cin >> ch[i][j];
if (ch[i][j] != '*') {
c = int(ch[i][j] - '0');
visx[i][c] = true;
visy[j][c] = true;
vis[i / 3 * 3 + j / 3][c] = true;
}
}
}
cout << '\n';
dfs(0, 0);
return 0;
}
/*
* 2 6 * * * * * *
* * * 5 * 2 * * 4
* * * 1 * * * * 7
* 3 * * 2 * 1 8 *
* * * 3 * 9 * * *
* 5 4 * 1 * * 7 *
5 * * * * 1 * * *
6 * * 9 * 7 * * *
* * * * * * 7 5 *
*/
Q:2n皇后问题
#include<iostream>
using namespace std;
int n;
int ans = 0;
int ans_temp = 0;
bool yb[9], yw[9];
bool pb[17], pw[17];//+
bool nb[17], nw[17];//-
int map[9][9] = { 0 };
void dfsw(int x) { //放白人
if (x == n) {
ans_temp++;
return;
}
for (int i = 0; i < n; i++) {
if (!yw[i] && !pw[x + i] && !nw[x - i + n] && map[x][i] == 1) {
yw[i] = true;
pw[x + i] = true;
nw[x - i + n] = true;
map[x][i] = 3;
dfsw(x + 1);
yw[i] = false;
pw[x + i] = false;
nw[x - i + n] = false;
map[x][i] = 1;
}
}
}
void dfsb(int x) { //先放黑人,再放白人
if (x == n) {
dfsw(0);
ans += ans_temp;
ans_temp = 0;
return;
}
for (int i = 0; i < n; i++) {
if (!yb[i] && !pb[x + i] && !nb[x - i + n] && map[x][i] == 1) {
yb[i] = true;
pb[x + i] = true;
nb[x - i + n] = true;
map[x][i] = 2;
dfsb(x + 1);
yb[i] = false;
pb[x + i] = false;
nb[x - i + n] = false;
map[x][i] = 1;
}
}
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> map[i][j];
}
}
cout << '\n';
dfsb(0);
cout << ans << endl;
return 0;
}
/*
4
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
*/
Q:引爆炸弹
#include<iostream>
#include<cstring>
using namespace std;
int n, m;
int num = 0;
string map[1003];
bool vx[1002];
bool vy[1002];
void dfs(int x, int y) {
map[x][y] = '0';
vx[x] = vy[y] = true;
//遍历所在列
for (int i = 0; i < n; i++) {
if (map[i][y]=='1') {
dfs(i, y);
}
}
for (int j = 0; j < m; j++) {
if (map[x][j]=='1') {
dfs(x, j);
}
}
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> map[i];
//scanf_s("%s",map[i]);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (map[i][j] == '1') {
dfs(i, j);
num++;
}
}
}
cout << num << '\n';
return 0;
}
/*
5 5
00010
00010
01001
10001
01000
*/