zoj 3781 Paint the Grid Reloaded (bfs)
来源:互联网 发布:网大播出数据 编辑:程序博客网 时间:2024/06/05 09:25
Paint the Grid Reloaded
此题首先应该将问题抽象,通过dfs将原背景转化为图,即缩点连边,这样问题处理就变的很方便了。
缩点之后,问题变成一个二分图。可以在任意一时刻,选择任意一点,翻转该点,和周围的点变成同一种点,则又可以缩点。缩点后再进行操作。问最少的翻转次数,使得所有的点变成同一种点。
将问题中的翻转一次转化为从该点bfs的一步。
此题最后的解决方法是:枚举任意一点为起点,bfs,取所有最大深度的最小值,即为答案。
个人对此bfs法的正确性的理解:
如果,从2个点作为出发点bfs,深度分别为d1、d2,最后结果为d1+d2,则2个点的bfs必有交叉点。而若从交叉点开始bfs,则此时的答案为max(d1, d2)<= d1+d2;
类推多个点。则对于多个起点的bfs的结果,可以找到一个不比其差的从一个点开始bfs的结果。枚举任意一点为起点(寻找较为平衡的交叉点),bfs,取所有最大深度的最小值,即为答案。
另思路:每次贪心翻转度最大的点。待验证???
int n, m;char s[44][44];int id[44][44];vector<int>G[1700];set<int> S[1700];int sz;int dir_i[] = {0, -1, 0, 1};int dir_j[] = {-1, 0, 1, 0};bool check(int i, int j){ if (i >= 0 && i < n && j >= 0 && j < m) return true; return false;}void dfs(int ui, int uj){ id[ui][uj] = sz; REP(r, 4) { int vi = ui + dir_i[r]; int vj = uj + dir_j[r]; if (check(vi, vj)) { if (s[vi][vj] == s[ui][uj] && !id[vi][vj]) { dfs(vi, vj); } else if (s[vi][vj] != s[ui][uj] && id[vi][vj]) { int x = id[vi][vj]; if (!S[sz].count(x)) { S[sz].insert(x); G[x].push_back(sz); G[sz].push_back(x); } } } }}int vis[1700];int when = 0;int bfs(int st){ queue<int>qd; queue<int>q; int ret = 0; when++; vis[st] = when; q.push(st); qd.push(0); while (!q.empty()) { int u = q.front(); q.pop(); int ud = qd.front(); qd.pop(); ret = max(ret, ud); REP(i, G[u].size()) { int r = G[u][i]; if (vis[r] != when) { vis[r] = when; q.push(r); qd.push(ud + 1); } } } return ret;}int main (){ int T; RI(T); while (T--) { memset(id, 0, sizeof(id)); RII(n, m); REP(i, n) RS(s[i]); sz = 0; REP(i, n) REP(j, m) { if (!id[i][j]) { ++sz; S[sz].clear(); G[sz].clear(); dfs(i, j); } } int ans = n * m; for (int i = 1; i <= sz; i++) { ans = min(ans, bfs(i)); } cout << ans << endl; } return 0;}/**232 2XOOX*/
0 0
- ZOJ 3781 Paint the Grid Reloaded BFS
- zoj 3781 Paint the Grid Reloaded (bfs)
- ZOJ 3781 - Paint the Grid Reloaded(缩点dfs+bfs)
- ZOJ 3781Paint the Grid Reloaded 缩点+bfs
- ZOJ 3781 Paint the Grid Reloaded 图论 bfs
- ZOJ 3781 Paint the Grid Reloaded(缩点+bfs)
- ZOJ 3781 Paint the Grid Reloaded (缩点,bfs)
- zoj 3781 Paint the Grid Reloaded
- zoj 3781 Paint the Grid Reloaded
- ZOJ 3781 Paint the Grid Reloaded
- Paint the Grid Reloaded ZOJ - 3781
- ZOJ Paint the Grid Reloaded
- ZOJ 3781 Paint the Grid Reloaded ( BFS(重点是对问题的分析) )
- ZOJ 3781 Paint the Grid Reloaded 题解 (dfs缩点+BFS)
- 3781 -- Paint the Grid Reloaded
- ZOJ Problem Set - 3781 Paint the Grid Reloaded
- zoj 3781 Paint the Grid Reloaded 最短路变形
- zoj 3781 Paint the Grid Reloaded (最短路,连通块缩点)
- 你如何评价Android系统?优缺点
- 动态规划
- 数据解析(一)解析XML之系统自带NSXMLParse类
- 弟弟
- SharePoint 2010 At Work--Taming the Elusive Calculated Column:Logic Functions
- zoj 3781 Paint the Grid Reloaded (bfs)
- jsp获与action数据
- 数塔
- PHP中根基标识表记标帜及操纵要收
- Common Subsequence
- poj3122 Pie
- Super Jumping! Jumping! Jumping!
- 练习使用wireshark
- 最少拦截系统