日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

*【ZOJ - 3781】Paint the Grid Reloaded(dfs求连通块缩点,bfs求最短路,建图技巧)

發布時間:2023/12/10 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 *【ZOJ - 3781】Paint the Grid Reloaded(dfs求连通块缩点,bfs求最短路,建图技巧) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

Leo has a grid with?N?rows and?M?columns. All cells are painted with either black or white initially.

Two cells?A?and?B?are called?connected?if they share an edge and they are in the same color, or there exists a cell?C?connected to both?A?and?B.

Leo wants to paint the grid with the same color. He can make it done in multiple steps. At each step Leo can choose a cell and flip the color (from black to white or from white to black) of all cells connected to it. Leo wants to know the minimum number of steps he needs to make all cells in the same color.

Input

There are multiple test cases. The first line of input contains an integer?Tindicating the number of test cases. For each test case:

The first line contains two integers?N?and?M?(1 <=?N,?M?<= 40). Then?N?lines follow. Each line contains a string with?N?characters. Each character is either 'X' (black) or 'O' (white) indicates the initial color of the cells.

Output

For each test case, output the minimum steps needed to make all cells in the same color.

Sample Input

2 2 2 OX OX 3 3 XOX OXO XOX

Sample Output

1 2

Hint

For the second sample, one optimal solution is:

Step 1. flip (2, 2)

XOX OOO XOX

Step 2. flip (1, 2)

XXX XXX XXX

題目大意:

字符一樣并且相鄰的即為連通。每次可翻轉一個連通塊X(O)的顏色,問至少改變幾次使得圖上所有字符都相等

解題報告:

思路1:直接枚舉起點,然后暴力搜。這個步驟搜的O那下一個步驟就搜X。。

思路2:dfs連通塊縮點,枚舉起點bfs求最短路,注意要用連通塊來求bfs,,不然會T。

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 55; char s[MAX][MAX]; int bk[MAX][MAX]; int nx[4] = {1,0,-1,0}; int ny[4] = {0,1,0,-1}; int tot,n,m; int vis[MAX*MAX]; vector<int> vv[MAX*MAX]; struct Node {int id;int t;Node(){}Node(int id,int t):id(id),t(t){} }; bool ok(int x,int y) {if(x>=1&&x<=n&&y>=1&&y<=m) return 1 ;else return 0 ; } void dfs(int x,int y,char tar) {bk[x][y] = tot; for(int k = 0; k<4; k++) {int tx = x + nx[k];int ty = y + ny[k];if(ok(tx,ty) == 0) continue;if(s[tx][ty] == tar) {if(bk[tx][ty] == -1) dfs(tx,ty,tar);}else if(bk[tx][ty] != -1) {vv[tot].pb(bk[tx][ty]);vv[bk[tx][ty]].pb(tot);}} }int bfs(int x) {memset(vis,0,sizeof vis);queue<Node> q;int res = 0;q.push(Node(x,0));vis[x]=1;while(q.size()) {Node cur = q.front();q.pop();int col = cur.id;res = cur.t;int up = vv[col].size();for(int i = 0; i<up; i++) {int v = vv[col][i];if(vis[v]) continue;vis[v]=1;q.push(Node(v,cur.t+1));} } return res; } int main() {int t;cin>>t;while(t--) {tot=0;memset(bk,-1,sizeof bk);scanf("%d%d",&n,&m);for(int i = 1; i<=n; i++) {scanf("%s",s[i]+1);}for(int i = 1; i<=n*m; i++) vv[i].clear();for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {if(bk[i][j]== -1) {tot++;dfs(i,j,s[i][j]);}}}int ans = 9999999;for(int i = 1; i<=tot; i++) ans = min(ans,bfs(i));printf("%d\n",ans);}return 0 ; }

?

總結

以上是生活随笔為你收集整理的*【ZOJ - 3781】Paint the Grid Reloaded(dfs求连通块缩点,bfs求最短路,建图技巧)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。