2020寒假训练第一周(搜索)解题报告

这篇博客详细介绍了2020年寒假第一周编程训练的六道题目,包括A - Maze、B - Lakes in Berland、C - Valid BFS?等,涉及迷宫连通性、湖泊数量减少、BFS序列验证等算法问题。作者通过DFS和BFS解决这些问题,并在解题过程中进行了反思和总结。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

训练链接:https://vjudge.net/contest/350133#overview

A - MazeCodeForces 377A

B - Lakes in BerlandCodeForces 723D

C - Valid BFS?CodeForces 1037D

D - CheckPostsCodeForces 427C

E - Ice CaveCodeForces 540C

F - 0-1MSTCodeForces 1242B


A - Maze

题意:给出一个n * m大小的迷宫,“#”表示墙,“. ”表示路,保证给定的迷宫是连通的,即任意的一对“ . ”之间可以相互到达,再给定一个数k,要求将迷宫的 k 块路改为墙后,迷宫仍然连通,更改的地方用“ X ”表示,输出更改之后的迷宫。Special Judge

思路:直接DFS跑迷宫,回溯的时候将当前点改为“ X ”,直到k为0,因为回溯时的当前点必定为此次DFS最深的点,将其变为墙不会对连通性有影响。

AC代码

#include<set>
#include<iostream>
#include<cmath>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<string>
#include<vector>
typedef long long LL;
#define PII pair<int, int>
#define pb push_back
const int maxn = 500 + 5;
const int inf = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const double EPS = 1e-15;
using namespace std;
 
int n, m, k;
char a[maxn][maxn];
bool vis[maxn][maxn];
int dx[] = {1, 0, 0, -1};
int dy[] = {0, 1, -1, 0};
 
void dfs(int x, int y){
	vis[x][y] = 1;
	for(int i = 0; i < 4; ++i){
		int nx = x + dx[i];
		int ny = y + dy[i];
		if(nx < 1 || ny < 1 || nx > n || ny > m) continue;
		if(a[nx][ny] == '#') continue;
		if(vis[nx][ny]) continue;
		dfs(nx, ny);
	}
	if(k) k--, a[x][y] = 'X';
}
 
int main(){
	scanf("%d %d %d", &n, &m, &k);
	for(int i = 1; i <= n; ++i) scanf(" %s", a[i] + 1);
	bool flag = 0;
	for(int i = 1; i <= n; ++i){
		for(int j = 1; j <= m; ++j){
			if(a[i][j] == '.') {
				dfs(i, j);
				flag = 1;
				break;
			}
		}
		if(flag) break;
	}
	for(int i = 1; i <= n; ++i) printf("%s\n", a[i] + 1);
	return 0;
}

B - Lakes in Berland

题意:给出一个n * m大小的大陆的俯视图,用“*”表示陆地,用“.”表示水。n * m外的世界为海洋,当水与海洋相接时,此连通水域不称为湖泊,当一块连通水域不与海洋相接,被陆地包围时,称为湖泊。现在要将湖泊的数量减至k个,问需要填多少湖泊中的水块才能使的湖泊剩下k个,要求填的水块最少。

思路:先将与海洋相连的水域标记,标记完后剩下来的水块必定就是属于湖泊的水块,对每个连通水域跑bfs或者dfs,记录下当前湖泊的水块数量,并且标记当前是第几个湖泊,跑完bfs或dfs之后对水块数量升序排序,将需要填成陆地的湖泊的水块数量相加即为答案。

反思:做的时候一直卡在如果当前的“湖泊”与海洋相连该怎么在遍历的时候跳过这个连通水域,出现了奇奇怪怪的bug,怎么改也改不对,应该一开始就直接将非湖泊的水域标记完了再去跑剩下来的水域。

AC代码:

#include<set>
#include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
typedef long long LL;
#define PII pair<int, int>
#define pb push_back
const int maxn = 55;
const int inf = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const double EPS = 1e-15;
using namespace std;
 
int num;//联通块数量
int n, m, k;
char a[maxn][maxn];
int b[maxn][maxn];
int vis[maxn][maxn];
int dx[] = {1, 0, 0, -1};
int dy[] = {0, 1, -1, 0};
struct NODE{
	int num, pos;
	friend bool operator < (NODE x, NODE y){
		return x.num < y.num;
	}
}ga[maxn * maxn];
 
void dfs1(int x, int y){
	vis[x][y] = -1;
	for(int i = 0; i < 4; ++i){
		int nx, ny;
		nx = x + dx[i], ny = y + dy[i];
		if(nx < 1 || nx > n || ny < 1 || ny > m) continue;
		if(a[nx][ny] == '*') continue;
		if(vis[nx][ny] == -1) continue;
		dfs1(nx, ny);
	}
}
 
int dfs(int x, int y){
	int cnt = 1;
	vis[x][y] = 1;
	b[x][y] = num;
	for(int i = 0; i < 4; ++i){
		int nx, ny;
		nx = x + dx[i], ny = y + dy[i];
		if(nx < 1 || ny < 1 || nx > n || ny > m) continue;
		if(a[nx][ny] == '*') continue;
		if(vis[nx][ny] == 1 || vis[nx][ny] == -1) continue;
		cnt += dfs(nx, ny);
	}
	return cnt;
}
 
int main(){
	scanf("%d %d %d", &n, &m, &k);
	for(int i = 1; i <= n; ++i) scanf(" %s", a[i] + 1);
	for(int i = 1; i <= n; ++i) {
		if(vis[i][1] == 0 && a[i][1] == '.') dfs1(i, 1);
		if(vis[i][m] == 0 && a[i][m] == '.') dfs1(i, m);
	}
	for(int i = 1; i <= m; ++i){
		if(vis[1][i] == 0 && a[1][i] == '.') dfs1(1, i);
		if(vis[n][i] == 0 && a[n][i] == '.') dfs1(n, i);
	}
	for(int i = 1; i <= n; ++i){
		for(int j = 1; j <= m; ++j){
			if(a[i][j] == '.' && vis[i][j] == 0){
				num++;
				ga[num].num = dfs(i, j);
				ga[num].pos = num;
			}
		}
	}
 
	//for(int i = 1; i <= num; ++i) printf("%d %d\n", ga[i].num, ga[i].pos);
//	printf("\n");
//	for(int i = 1; i <= n; ++i){
//		for(int j = 1; j <= m; ++j) printf("%d", vis[i][j]);
//		printf("\n");
//	}
  //  printf("\n");
	
	sort(ga + 1, ga + 1 + num);
	int cnt = num - k;
	set<int> se;
	int ans = 0;
	for(int i = 1; i <= cnt; ++i) se.insert(ga[i].pos);
	for(int i = 1; i <= n; ++i){
		for(int j = 1; j <= m; ++j){
			if(se.find(b[i][j]) != se.end()){
				a[i][j] = '*';
				ans++;
			}
		}
	}
	printf("%d\n", ans);
	for(int i = 1; i <= n; ++i) printf("%s\n", a[i] + 1);
	return 0;
}

C - Valid BFS?

题意:给定一课树,再给定一个序列,问给定的序列是否是一个有效的BFS序列。

思路:根据队列的性质,先进入队列的就会被先遍历到,那么遍历到的点所连的点也会先被遍历到,因此输入图之后,根据给定的序列中节点编号的位置从小到大排序,先按照给定的序列跑BFS,如果跑出来的序列与给定的序列不一致,则此BFS序列是无效的。

反思:做题的时候被题目绕进去了,没想到直接按照位置排序就可以先按照给定序列跑BFS。

AC代码:

#include<set>
#include<iostream>
#include<cmath>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<string>
#include<vector>
#include<cstring>
typedef long long LL;
#define PII pair<int, int>
#define pb push_back
const int maxn = 2e5 + 5;
const LL mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
using namespace std;

int pos[maxn];
vector<int> a, b, G[maxn];
bool vis[maxn];

bool cmp(int x, int y){
	return pos[x] < pos[y];
}

void Bfs(){
	queue<int> q;
	q.push(1);
	vis[1] = 1;
	while(!q.empty()){
		//printf("!\n");
		int cur = q.front();
		q.pop();
		b.pb(cur);
		int len = G[cur].size();
		for(int i = 0; i < len; ++i){
			int v = G[cur][i];
			if(vis[v]) continue;
			vis[v] = 1;
			q.push(v);
		}
	}
	return;
}

int main(){
	int n;
	scanf("%d", &n);
	for(int i = 1; i < n; ++i){
		int u, v;
		scanf("%d %d", &u, &v);
		G[u].pb(v);
		G[v].pb(u);
	}
	a.resize(n);
	for(int i = 0; i < n; ++i){
		scanf("%d", &a[i]);
		pos[a[i]] = i;
	}
	for(int i = 1; i <= n; ++i) sort(G[i].begin(), G[i].end(), cmp);
	Bfs();
	if(a == b) printf("Yes\n");
	else printf("No\n");
	return 0;
}

D - CheckPosts

传送门

E - Ice Cave

题意:给出一张n * m大小的冰面图,有的冰面已经破碎了,一踩就破,有的冰面暂时还是完好的,但是被踩了一次之后就会破掉,完整的冰面用“ . ”表示,破碎的冰面用“ X ”表示,现在给定起点和终点,要求从起点走到终点并且踩碎终点的冰块落下去,不能在原地跳,问能否从终点的冰面处落下去。

思路:BFS找最近路线,到达终点时判断能否从终点落下,如果不能直接落下,判断终点周围是否还有完整的冰面,有的话则可以先走到完整冰面上再走回终点。

AC代码:

#include<set>
#include<iostream>
#include<cmath>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<string>
#include<vector>
typedef long long LL;
#define PII pair<int, int>
#define pb push_back
const int maxn = 500 + 5;
using namespace std;
 
int n, m, k;
int sx, sy, ex, ey;
char a[maxn][maxn];
bool vis[maxn][maxn];
int dx[] = {1, 0, 0, -1};
int dy[] = {0, 1, -1, 0};
struct NODE{
	int x, y;
	NODE(){}
	NODE(int x, int y):x(x), y(y){}
};
 
bool Bfs(){
	queue<NODE> q;
	q.push(NODE(sx,sy));
	vis[sx][sy] = 1;
	while(!q.empty()){
		NODE cur = q.front(); q.pop();
		a[cur.x][cur.y] = 'X';
		vis[cur.x][cur.y] = 1;
		for(int i = 0; i < 4; ++i){
			NODE nxt = NODE(cur.x + dx[i], cur.y + dy[i]);
			if(nxt.x < 1 || nxt.y < 1 || nxt.x > n || nxt.y > m) continue;
			if(nxt.x == ex && nxt.y == ey) return 1;
			if(a[nxt.x][nxt.y] == 'X') continue;
			if(vis[nxt.x][nxt.y]) continue;
                        vis[nxt.x][nxt.y] = 1;
			q.push(nxt);
		}
	}
	return 0;
}
 
bool check(){
	if(a[ex][ey] == 'X') return 1;
	for(int i = 0; i < 4; ++i){
		int nx, ny;
		nx = ex + dx[i];
		ny = ey + dy[i];
		if(a[nx][ny] == '.') return 1;
	}
	return 0;
}
int main(){
	scanf("%d %d", &n, &m);
	for(int i = 1; i <= n; ++i) scanf(" %s", a[i] + 1);
	scanf("%d %d %d %d", &sx, &sy, &ex, &ey);
	bool flag = 0;
	if(Bfs()){
		if(check()) printf("YES\n");
		else printf("NO\n");
	}
	else printf("NO\n");
	//for(int i = 1; i <= n; ++i) printf("%s\n", a[i] + 1);
	return 0;
}

F - 0-1MST

题意:给出一张图,边的边权只为1或0,求最小生成树的权值。

思路:类似并查集的思想,最终有多少个集合,最小生成树的权值就为集合数量 - 1。

AC代码:

#include<set>
#include<iostream>
#include<cmath>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<string>
#include<vector>
#include<cstring>
typedef long long LL;
#define PII pair<int, int>
#define pb push_back
const int maxn = 2e5 + 5;
const LL mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
using namespace std;

int n, m;
set<int> G[maxn], U;
bool vis[maxn];

void Bfs(int x){
	queue<int> q;
	q.push(x);
	vis[x] = 1;
	U.erase(x);
	while(!q.empty()){
		int cur = q.front(); q.pop();
		set<int> :: iterator i;
		vector<int> tmp;
		for(i = U.begin(); i != U.end(); ++i) tmp.pb(*i);
		int len = tmp.size();
		for(int i = 0; i < len; ++i){
			if(G[cur].find(tmp[i]) == G[cur].end()){
				vis[tmp[i]] = 1;
				U.erase(tmp[i]);
				q.push(tmp[i]);
			}
		}
	}
}

int main(){
	scanf("%d %d", &n, &m);
	for(int i = 1; i <= m; ++i){
		int u, v;
		scanf("%d %d", &u, &v);
		G[u].insert(v);
		G[v].insert(u);
	}
	for(int i = 1; i <= n; ++i){
		U.insert(i);
	}
	int cnt = 0;
	for(int i = 1; i <= n; ++i){
		if(!vis[i]){
			cnt++;
			Bfs(i);
		}
	}
	printf("%d\n", cnt - 1);
	return 0;
}

 

一、综合实战—使用极轴追踪方式绘制信号灯 实战目标:利用对象捕捉追踪和极轴追踪功能创建信号灯图形 技术要点:结合两种追踪方式实现精确绘图,适用于工程制图中需要精确定位的场景 1. 切换至AutoCAD 操作步骤: 启动AutoCAD 2016软件 打开随书光盘中的素材文件 确认工作空间为"草图与注释"模式 2. 绘图设置 1)草图设置对话框 打开方式:通过"工具→绘图设置"菜单命令 功能定位:该对话框包含捕捉、追踪等核心绘图辅助功能设置 2)对象捕捉设置 关键配置: 启用对象捕捉(F3快捷键) 启用对象捕捉追踪(F11快捷键) 勾选端点、中心、圆心、象限点等常用捕捉模式 追踪原理:命令执行时悬停光标可显示追踪矢量,再次悬停可停止追踪 3)极轴追踪设置 参数设置: 启用极轴追踪功能 设置角度增量为45度 确认后退出对话框 3. 绘制信号灯 1)绘制圆形 执行命令:"绘图→圆→圆心、半径"命令 绘制过程: 使用对象捕捉追踪定位矩形中心作为圆心 输入半径值30并按Enter确认 通过象限点捕捉确保圆形位置准确 2)绘制直线 操作要点: 选择"绘图→直线"命令 捕捉矩形上边中点作为起点 捕捉圆的上象限点作为终点 按Enter结束当前直线命令 重复技巧: 按Enter可重复最近使用的直线命令 通过圆心捕捉和极轴追踪绘制放射状直线 最终形成完整的信号灯指示图案 3)完成绘制 验证要点: 检查所有直线是否准确连接圆心和象限点 确认极轴追踪的45度增量是否体现 保存绘图文件(快捷键Ctrl+S)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值