DFS
AcWing 842. 排列数字
#include <iostream>
using namespace std;
const int N = 10;
int n;
int path[N];
bool st[N];
void dfs(int u)
{
if (u == n)
{
for (int i = 0; i < n; i++) cout << path[i] << ' ';
puts("");
return;
}
for (int i = 1; i <= n; i++)
{
if (!st[i])
{
path[u] = i;
st[i] = true;
dfs(u + 1);
st[i] = false;
}
}
}
int main()
{
cin >> n;
dfs(0);
return 0;
}
AcWing 843. n-皇后问题
第一种搜索顺序
#include <iostream>
using namespace std;
const int N = 20;
int n;
char map[N][N];
bool col[N], dg[N * 2], udg[N * 2];
void dfs(int u)
{
if (u == n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++) cout << map[i][j];
puts("");
}
puts("");
return;
}
for (int i = 0; i < n; i++)
if (!col[i] && !dg[u + i] && !udg[n - u + i])
{
map[u][i] = 'Q';
col[i] = dg[u + i] = udg[n - u + i] = true;
dfs(u + 1);
col[i] = dg[u + i] = udg[n - u + i] = false;
map[u][i] = '.';
}
}
int main()
{
cin >> n;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
map[i][j] = '.';
dfs(0);
return 0;
}
第二种搜索顺序
#include <iostream>
using namespace std;
const int N = 20;
int n;
char map[N][N];
bool row[N], col[N], dg[N * 2], udg[N * 2];
void dfs(int x, int y, int s)
{
if (y == n) y = 0, x++;
if (x == n)
{
if (s == n)
{
for (int i = 0; i < n; i++) puts(map[i]);
puts("");
}
return;
}
dfs(x, y + 1, s);
if (!row[x] && !col[y] && !dg[x + y] && !udg[x - y + n])
{
map[x][y] = 'Q';
row[x] = col[y] = dg[x + y] = udg[x - y + n] = true;
dfs(x, y + 1, s + 1);
row[x] = col[y] = dg[x + y] = udg[x - y + n] = false;
map[x][y] = '.';
}
}
int main()
{
cin >> n;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
map[i][j] = '.';
dfs(0, 0, 0);
return 0;
}
AcWing 1112. 迷宫
#include <iostream>
#include <cstring>
using namespace std;
const int N = 110;
char g[N][N];
bool st[N][N];
int xa, ya, xb, yb;
int n;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
bool dfs(int x, int y)
{
if (g[x][y] == '#') return false;
if (x == ya && y == yb) return true;
st[x][y] = true;
for (int i = 0; i < 4; i++)
{
int a = x + dx[i], b = y + dy[i];
if (a < 0 || a >= n || b < 0 || b >= n) continue;
if (st[a][b]) continue;
st[a][b] = true;
if (dfs(a, b)) return true;
}
return false;
}
int main()
{
int M;
cin >> M;
while (M--)
{
cin >> n;
for (int i = 0; i < n; i++) cin >> g[i];
memset(st, false, sizeof st);
cin >> xa >> xb >> ya >> yb;
if (dfs(xa, xb)) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}
AcWing 1113. 红与黑
#include <iostream>
#include <cstring>
using namespace std;
const int N = 25;
char g[N][N];
bool st[N][N];
int n, m;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int dfs(int x, int y)
{
int cnt = 1;
st[x][y] = true;
for (int i = 0; i < 4; i++)
{
int a = x + dx[i], b = y + dy[i];
if (a < 0 || a >= n || b < 0 || b >= m) continue;
if (g[a][b] != '.') continue;
if (st[a][b]) continue;
cnt += dfs(a, b);
}
return cnt;
}
int main()
{
while (cin >> m >> n, n || m)
{
for (int i = 0; i < n; i++) cin >> g[i];
memset(st, 0, sizeof st);
int x, y;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (g[i][j] == '@')
x = i, y = j;
cout << dfs(x, y) << endl;
}
return 0;
}
AcWing 1116. 马走日
#include <iostream>
#include <cstring>
using namespace std;
const int N = 15;
int g[N][N];
bool st[N][N];
int ans;
int n, m, x, y;
int dx[] = {-2, -1, 1, 2, 2, 1, -1, -2};
int dy[] = {1, 2, 2, 1, -1, -2, -2, -1};
void dfs(int x, int y, int cnt)
{
if (cnt == n * m)
{
ans++;
return ;
}
st[x][y] = 1;
for (int i = 0; i < 8; i++)
{
int a = x + dx[i], b = y + dy[i];
if (a < 0 || a >= n || b < 0 || b >= m) continue;
if (st[a][b]) continue;
st[a][b] = 1;
dfs(a, b, cnt + 1);
st[a][b] = 0;
}
}
int main()
{
int M;
cin >> M;
while (M--)
{
memset(st, 0, sizeof st);
cin >> n >> m >> x >> y;
ans = 0;
dfs(x, y, 1);
cout << ans << endl;
}
return 0;
}
AcWing 1117. 单词接龙
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 21;
string word[N];
int g[N][N];
int used[N];
int n;
int ans;
void dfs(string dragon, int last)
{
ans = max((int)dragon.size(), ans);
used[last]++;
for (int i = 0; i < n; i++)
if (g[last][i] && used[i] < 2)
dfs(dragon + word[i].substr(g[last][i]), i);
used[last]--;
}
int main()
{
cin >> n;
for (int i = 0; i < n; i++) cin >> word[i];
char start;
cin >> start;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
string a = word[i], b = word[j];
for (int k = 1; k < min(a.size(), b.size()); k++)
if (a.substr(a.size()-k, k) == b.substr(0, k))
{
g[i][j] = k;
break;
}
}
for (int i = 0; i < n; i++)
if (word[i][0] == start)
dfs(word[i], i);
cout << ans << endl;
return 0;
}