(联通快缩点加bfs)F - Paint the Grid Reloaded

本文通过一个具体的编程竞赛题目,深入解析了图论的基本概念及其应用,展示了深度优先搜索(DFS)与广度优先搜索(BFS)算法在解决图论问题中的实际运用。通过对代码的详细解读,读者可以了解到如何构建图数据结构,如何利用DFS进行图的遍历,以及如何使用BFS寻找最短路径。此外,还介绍了图的连通性和图着色等概念。

https://vjudge.net/contest/294469#problem/F

#pragma comment (linker,"/STACK:1024000000,1024000000")
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<cstdlib>
#include<cmath>
#include<stack>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
#define ll long long
#define lb long double
#define INF 0x3f3f3f3f
const int maxn = 45;
const ll mod = 1e9 + 7;
int t, n, m, cnt, num, mini = INF;
char s[maxn][maxn];
int head[maxn*maxn], col[maxn][maxn];
bool judge[maxn*maxn][maxn*maxn], vis[maxn*maxn];
int dir[4][2] = {1, 0, 0, -1, -1, 0, 0, 1};
struct node{
	int to;
	int next;
}edge[maxn*maxn*maxn*maxn];
struct nod{
	int u;
	int d;
};
void add(int u, int v){
	edge[cnt].to = v;
	edge[cnt].next = head[u];
	head[u] = cnt ++;
}
void init(){
	scanf("%d %d", &n, &m);
	getchar();
	for(int i = 1 ; i <= n ; ++ i){
		scanf("%s", s[i] + 1);
	}
	cnt = 0;
	num = 0;
	mini = INF;
	//memset(edge, 0, sizeof(edge));
	memset(judge, 0, sizeof(judge));
	memset(col, -1, sizeof(col));
	memset(head, -1, sizeof(head));
}
void dfs(int x, int y, int c){
	for(int i = 0 ; i < 4 ; ++ i){
		int tx = x + dir[i][0];
		int ty = y + dir[i][1];
		if(tx < 1 || tx > n || ty < 1 || ty > m) continue;
		if(s[x][y] == s[tx][ty]){
			if(col[tx][ty] == -1){
				col[tx][ty] = c;
				dfs(tx, ty, c);
			}
		}
		else{
			if(col[tx][ty] != -1){
				int u = col[x][y], v = col[tx][ty];
				if(!judge[u][v] && u != v){
					add(u, v); add(v, u);
					judge[u][v] = 1; judge[v][u] = 1;
				}
			}
		}
	}
}
int bfs(int s){
	int maxi = 0;
	memset(vis, 0, sizeof(vis));
	queue<nod> que;
	vis[s] = 1;
	nod temp;
	temp.u = s; temp.d = 0;
	que.push(temp);
	while(!que.empty()){
		nod cur = que.front();
		que.pop();
		if(cur.d > mini) return INF;
		for(int i = head[cur.u] ; i != -1 ; i = edge[i].next){
			int tt = edge[i].to;
			if(tt == cur.u) continue;
			if(!vis[tt]){
				vis[tt] = 1;
				nod temp;
				temp.u = tt; temp.d = cur.d + 1;
				que.push(temp);
				maxi = max(maxi, cur.d + 1);
			}
		}
	}
	return maxi;
}
int main()
{
	scanf("%d", &t);
	while(t--){
		init();
		for(int i = 1 ; i <= n ; ++ i){
			for(int j = 1 ; j <= m ; ++ j){
				if(col[i][j] == -1){
					col[i][j] = ++ num;
					dfs(i, j, num);
				}
			}
		}
		for(int i = 1 ; i <= num ; ++ i){
			mini = min(mini, bfs(i));
		}
		printf("%d\n", mini);
	}
    return 0;
}


乐播投屏是一款简单好用、功能强大的专业投屏软件,支持手机投屏电视、手机投电脑、电脑投电视等多种投屏方式。 多端兼容与跨网投屏:支持手机、平板、电脑等多种设备之间的自由组合投屏,且无需连接 WiFi,通过跨屏技术打破网络限制,扫一扫即可投屏。 广泛的应用支持:支持 10000+APP 投屏,包括综合视频、网盘与浏览器、美韩剧、斗鱼、虎牙等直播平台,还能将央视、湖南卫视等各大卫视的直播内容一键投屏。 高清流畅投屏体验:腾讯独家智能音画调校技术,支持 4K 高清画质、240Hz 超高帧率,低延迟不卡顿,能为用户提供更高清、流畅的视觉享受。 会议办公功能强大:拥有全球唯一的 “超级投屏空间”,扫码即投,无需安装。支持多人共享投屏、远程协作批注,PPT、Excel、视频等文件都能流畅展示,还具备企业级安全密,保障会议资料不泄露。 多人互动功能:支持多人投屏,邀请好友入投屏互动,远程也可入。同时具备一屏多显、语音互动功能,支持多人连麦,实时语音交流。 文件支持全面:支持 PPT、PDF、Word、Excel 等办公文件,以及视频、图片等多种类型文件的投屏,还支持网盘直投,无需下载和转格式。 特色功能丰富:投屏时可同步录制投屏画面,部分版本还支持通过触控屏或电视端外接鼠标反控电脑,以及在投屏过程中用画笔实时标注等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值