学习视频:13-深度优先搜索视频讲解_哔哩哔哩_bilibili
Q: 迷宫
#include<iostream>
#include<string>
using namespace std;
int n, m;
string maze[110];
bool vis[110][110];
bool in(int x, int y) {
return 0 <= x && x < n && y < m && y >= 0;
}
bool dfs(int x, int y) {
if (maze[x][y] == 'T') {
return true;
}
vis[x][y] = 1; //表示已经访问过
maze[x][y] = 'm'; //走过的点用m标记
int tx = x - 1, ty = y; //往上走
if (in(tx, ty) && maze[tx][ty] != '*' && !vis[tx][ty]) { //不在边界且未访问
if (dfs(tx, ty)) {
return true;
}
}
tx = x, ty = y - 1;
if (in(tx, ty) && maze[tx][ty] != '*' && !vis[tx][ty]) {
if (dfs(tx, ty)) {
return true;
}
}
tx = x+1, ty = y;
if (in(tx, ty) && maze[tx][ty] != '*' && !vis[tx][ty]) {
if (dfs(tx, ty)) {
return true;
}
}
tx = x, ty = y + 1;
if (in(tx, ty) && maze[tx][ty] != '*' && !vis[tx][ty]) {
if (dfs(tx, ty)) {
return true;
}
}
vis[x][y] = 0; //回溯
maze[x][y] = '.';
return false;
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> maze[i];
}
int x, y;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (maze[i][j] == 'S')
x = i, y = j;
}
}
cout << '\n';
if (dfs(x, y)) {
for (int i = 0; i < n; i++) {
cout << maze[i] << endl;
}
}
else {
cout << "No" << endl;
}
return 0;
}
改进dfs
int dir[4][2] = { {-1,0},{0,-1},{1,0},{0,1} };
bool dfs(int x, int y) {
if (maze[x][y] == 'T') {
return true;
}
vis[x][y] = 1; //表示已经访问过
maze[x][y] = 'm'; //走过的点用m标记
int fx, fy;
for (int i = 0; i < 4; i++) {
fx = x + dir[i][0];
fy = y + dir[i][1];
if (in(fx, fy) && !vis[fx][fy] && maze[fx][fy] != '*')
if (dfs(fx, fy))
return true;
}
vis[x][y] = 0; //回溯
maze[x][y] = '.';
return false;
}
Q:中国象棋
#include<iostream>
#include<string>
using namespace std;
int fx, fy;
char ch[10][9];
bool vis[10][9];
int dis[8][2] = { {-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1} };
bool in(int x, int y) {
return x >= 0 && x < 10 && y >= 0 && y < 9;
}
bool dfs(int x, int y) {
if (ch[x][y] == 'T') { //到达
return true;
}
vis[x][y] = 1; //已经访问过
//ch[x][y]='*'; //表示路径标记
for (int i = 0; i < 8; i++) {
fx = x + dis[i][0];
fy = y + dis[i][1];
if (in(fx, fy) && ch[fx][fy] != '#' && !vis[fx][fy]) {
if (dfs(fx, fy))
return true;
}
}
vis[x][y] = 0; //回溯
//ch[x][y]='.'; //表示路径标记
return false;
}
int main() {
int x;
int y;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 9; j++) {
cin >> ch[i][j];
if (ch[i][j] == 'S') { //起始位置
x = i;
y = j;
}
}
}
if (dfs(x, y))
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
Q:迷宫最短的路径
#include<iostream>
#include<string>
using namespace std;
int n, m;
string maze[110];
bool vis[110][110];
int dir[4][2] = { {-1,0},{0,-1},{1,0},{0,1} };
bool in(int x, int y) {
return 0 <= x && x < n && y < m && y >= 0;
}
int ans = 100000000;
void dfs(int x, int y, int step) {
if (maze[x][y] == 'T') {
if (step < ans)
ans = step;
return;
}
vis[x][y] = 1;
int fx, fy;
for (int i = 0; i < 4; i++) {
fx = x + dir[i][0];
fy = y + dir[i][1];
if (in(fx, fy) && !vis[fx][fy] && maze[fx][fy] != '*') {
dfs(fx, fy, step+1);
}
}
vis[x][y] = 0;
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> maze[i];
}
int x, y;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (maze[i][j] == 'S')
x = i, y = j;
}
}
cout << '\n';
dfs(x, y, 0);
cout << ans << endl;
return 0;
}
——————————————————————————————————
学习视频:14-深度优先搜索练习题_哔哩哔哩_bilibili
Q:踏青
#include<iostream>
#include<string>
using namespace std;
int n, m;
char ch[101][101];
int dir[4][2] = { {0,1},{-1,0},{0,-1},{1,0} };
bool in(int x, int y) {
return x >= 0 && x < n && y >= 0 && y < m;
}
void dfs(int x, int y) {
ch[x][y] = '.';
int fx, fy;
for (int i = 0; i < 4; i++) {
fx = x + dir[i][0];
fy = y + dir[i][1];
if (in(fx, fy) && ch[fx][fy] != '.') {
dfs(fx, fy);
}
}
return;
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> ch[i][j];
}
}
int num = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (ch[i][j] == '#') {
dfs(i, j);
num++;
}
}
}
cout << num;
return 0;
}
Q:迷宫解的方案数
#include<iostream>
#include<string>
using namespace std;
char ch[12][12];
bool vis[12][12];
int n, m;
int num = 0;
int dir[4][2] = { {0,1},{1,0},{-1,0},{0,-1} };
bool in(int x, int y) {
return x >= 0 && x < n && y >= 0 && y < m;
}
void dfs(int x, int y) {
if (ch[x][y] == 'e') {
num++;
return;
}
vis[x][y] = 1;
int fx, fy;
for (int i = 0; i < 4; i++) {
fx = x + dir[i][0];
fy = y + dir[i][1];
if (in(fx, fy) && !vis[fx][fy] && ch[fx][fy] != '#') {
dfs(fx, fy);
}
}
vis[x][y] = 0;
}
int main() {
cin >> n >> m;
int x, y;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> ch[i][j];
if (ch[i][j] == 'S') {
x = i;
y = j;
}
}
}
dfs(x, y);
cout << num << '\n';
return 0;
}
Q:最大的蛋糕快
#include<iostream>
#include<string>
using namespace std;
char ch[1002][1002];
int num = 0;
int n, m;
int dir[4][2] = { {1,0},{0,1},{-1,0},{0,-1} };
bool in(int x, int y) {
return x >= 0 && x < n && y >= 0 && y < m;
}
void dfs(int x, int y) {
ch[x][y] = '.';
num++;
int fx, fy;
for (int i = 0; i < 4; i++) {
fx = x + dir[i][0];
fy = y + dir[i][1];
if (in(fx, fy) && ch[fx][fy] != '.') {
dfs(fx, fy);
}
}
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> ch[i][j];
}
}
int maxn = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (ch[i][j] == '#') {
dfs(i, j);
maxn = max(num, maxn);
num = 0;
}
}
}
cout << maxn << '\n';
return 0;
}
Q:家谱
#include<iostream>
#include<vector>
using namespace std;
vector<int> vec[100005];
vector<int> ans(100005, 0);
bool f[100005];
int dfs(int u) {
int ret = 0;
for (int i = 0; i < vec[u].size(); i++) {
ret += dfs(vec[u][i]);
}
ans[u] = ret;
return ret + 1;
}
int main() {
int n;
cin >> n;
int x, y;
for (int i = 1; i < n; i++) {
cin >> x >> y;
vec[x].push_back(y);
f[y] = true;
}
int u;
for (int i = 1; i <= n; i++) {
if (!f[i]) {
u = i;
break;
}
}
dfs(u);
for (int i = 0; i < n; i++) {
cout << ans[i + 1] << '\n';
}
return 0;
}
Q;马的覆盖点
#include<iostream>
#include<vector>
using namespace std;
char ch[101][101];
int n, m;
int dir[8][2] = { {1,2},{-2,1},{1,-2},{-2,-1},{2,1},{-1,-2},{-1,2},{2,-1} };
//bool vis[101][101];
void dfs(int x, int y, int num) {
//vis[x][y] = true;
if (num == 3) {
ch[x][y] = '#';
return;
}
ch[x][y] = '#';
int fx, fy;
for (int i = 0; i < 8; i++) {
fx = x + dir[i][0];
fy = y + dir[i][1];
if (fx >= 0 && fx < n && fy >= 0 && fy < m) {
dfs(fx, fy, num + 1);
}
}
}
int main() {
cin >> n >> m;
int x, y;
cin >> x >> y;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ch[i][j] = '.';
}
}
dfs(x-1, y-1, 0);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << ch[i][j] << " ";
}
cout << endl;
}
return 0;
}
Q:王子救公主
#include<iostream>
#include<vector>
using namespace std;
char ch[101][101];
int n, m;
//!!!公主可以到的点+王子可以到的点
bool vis[101][101];
bool gong[101][101] = { 0 };
bool wang[101][101] = { 0 };
int go[4][2] = { {1,0},{0,1},{-1,0},{0,-1} };
int wa[4][2] = { {2,0},{0,2},{-2,0},{0,-2} };
void dfsw(int x, int y) {
vis[x][y] = true;
wang[x][y] = true;
int fx, fy;
for (int i = 0; i < 4; i++) {
fx = x + wa[i][0];
fy = y + wa[i][1];
if (fx >= 0 && fx < n && fy >= 0 && fy < m &&ch[fx][fy]!='#'&&!vis[fx][fy]) {
dfsw(fx, fy);
}
}
vis[x][y] = false;
}
void dfsg(int x, int y) {
vis[x][y] = true;
gong[x][y] = true;
int fx, fy;
for (int i = 0; i < 4; i++) {
fx = x + go[i][0];
fy = y + go[i][1];
if (fx >= 0 && fx < n && fy >= 0 && fy < m && ch[fx][fy] != '#' && !vis[fx][fy]) {
dfsg(fx, fy);
}
}
vis[x][y] = false;
}
int main() {
cin >> n >> m;
int wx, wy, gx, gy;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> ch[i][j];
if (ch[i][j] == 'w') {
wx = i;
wy = j;
}
else if (ch[i][j] == 'g') {
gx = i;
gy = j;
}
}
}
dfsw(wx, wy);
dfsg(gx, gy);
bool flag;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (gong[i][j] && wang[i][j]) {
cout << "Yes" << endl;
flag = true;
break;
}
}
if (flag)
break;
}
if (!flag)
cout << "no" << endl;
return 0;
}
Q:蒜头开公司
不会,不明白