2638: 黑白染色
来源:互联网 发布:英语四级真题推荐知乎 编辑:程序博客网 时间:2024/05/16 13:59
题目链接
题目大意:黑白矩阵,每次可以选择一个四联通块染色,求最少操作次数
题解:Orz cls,ccz大爷题解
我的收获:bfs树
#include <queue>#include <cstdio>#include <vector>#include <algorithm>using namespace std;const int MAXN = 54;const int dx[] = { 1, 0, -1, 0 };const int dy[] = { 0, 1, 0, -1 };char color[MAXN * MAXN];vector<int> e[MAXN * MAXN];char buf[MAXN][MAXN];int tag[MAXN][MAXN];void floodfill(int x, int y, int z, char c) { if (buf[x][y] != c) { return; } buf[x][y] = '\0'; tag[x][y] = z; for (int i = 0; i < 4; ++i) { floodfill(x + dx[i], y + dy[i], z, c); }}int gao(int n, int s) { int ret = -1; queue<int> q; vector<int> d(n, -1); d[s] = 0; q.push(s); while (!q.empty()) { s = q.front(); q.pop(); ret=d[s]; if (color[s] == 'W') ret--; for (vector<int>::iterator it = e[s].begin(); it != e[s].end(); ++it) { if (d[*it] == -1) { d[*it] = d[s] + 1; q.push(*it); } } } return ret;}int main() { int n, r, c, ans; scanf("%d%d", &r, &c); for (int i = 1; i <= r; ++i) { scanf("%s", buf[i] + 1); } n = 0; for (int i = 1; i <= r; ++i) { for (int j = 1; j <= c; ++j) { if (buf[i][j] != '\0') { color[n] = buf[i][j]; floodfill(i, j, n, buf[i][j]); ++n; } } } for (int i = 1; i <= r; ++i) { for (int j = 1; j <= c; ++j) { for (int k = 0; k < 4; ++k) { int x = i + dx[k]; int y = j + dy[k]; if (1 <= x && x <= r && 1 <= y && y <= c && tag[i][j] != tag[x][y]) { e[tag[i][j]].push_back(tag[x][y]); } } } } for (int i = 0; i < n; ++i) {//去重 sort(e[i].begin(), e[i].end()); e[i].erase(unique(e[i].begin(), e[i].end()), e[i].end()); } ans = n; for (int i = 0; i < n; ++i) { ans = min(ans, gao(n, i)); } printf("%d\n", ans + 1); return 0;}
阅读全文
0 0
- 2638: 黑白染色
- 黑白染色(钟沛林)
- 【TsinsenA1309】黑白染色题解
- 黑白染色棋子
- DFS黑白染色
- BFS黑白染色
- HDOJ 4751 黑白染色问题
- acdream 1056 (黑白染色)
- uva10004 Bicoloring 黑白染色问题,DFS
- HDU 1569 黑白染色+最小割
- 【HDU】4859 海岸线 黑白染色+最小割
- hdu5285黑白染色二分最大匹配
- uva 10237 Bishops 黑白染色处理
- luoguP1330 封锁阳光大学 黑白染色
- 【AGC018F】Two Trees 构造 黑白染色
- HDU OJ 4185 Oil Skimming【二分图之黑白染色】
- [POJ 2492] A Bug's Life [黑白染色]
- HDU 1565 黑白染色 最大点权独立集
- CentOS系统rsync文件同步安装配置
- org.apache.ibatis.binding.BindingException【原因汇总】
- 解决VPS经SS访问Google Schoolar受限
- VM中的最小化安装的Centos7下安装vmware-tools实现宿主机和物理机之间文件传输
- android 自定义折线图
- 2638: 黑白染色
- 正则表达式Regex
- windows下cmd命令行,path搜索路径
- vi编辑器常用命令大全
- python基础入门(4)
- 记一次require+闭包的bug检查
- 感知器算法——C++实现
- Android中拍照和录像功能
- maven常用cmd命令