蓝桥杯——13

学习视频: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

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:蒜头开公司

不会,不明白

  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晓晓hh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值