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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

【HDU - 4185】Oil Skimming (二分图,建图,匈牙利算法)

發布時間:2023/12/10 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【HDU - 4185】Oil Skimming (二分图,建图,匈牙利算法) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

Thanks to a certain "green" resources company, there is a new profitable industry of oil skimming. There are large slicks of crude oil floating in the Gulf of Mexico just waiting to be scooped up by enterprising oil barons. One such oil baron has a special plane that can skim the surface of the water collecting oil on the water's surface. However, each scoop covers a 10m by 20m rectangle (going either east/west or north/south). It also requires that the rectangle be completely covered in oil, otherwise the product is contaminated by pure ocean water and thus unprofitable! Given a map of an oil slick, the oil baron would like you to compute the maximum number of scoops that may be extracted. The map is an NxN grid where each cell represents a 10m square of water, and each cell is marked as either being covered in oil or pure water.

Input

The input starts with an integer K (1 <= K <= 100) indicating the number of cases. Each case starts with an integer N (1 <= N <= 600) indicating the size of the square grid. Each of the following N lines contains N characters that represent the cells of a row in the grid. A character of '#' represents an oily cell, and a character of '.' represents a pure water cell.

Output

For each case, one line should be produced, formatted exactly as follows: "Case X: M" where X is the case number (starting from 1) and M is the maximum number of scoops of oil that may be extracted.

Sample Input

1 6 ...... .##... .##... ....#. ....## ......

Sample Output

Case 1: 3

題目大意:

有一個地圖,' . '代表水,' # ' 代表油。每個單元格是10*10的,現用一個'.'或'#'表示,現有10*20的勺子可以提取出水上漂浮的油,問最多可以提取幾勺的油; 每次提取的時候勺子放的位置都要是油,不然就被海水污染而沒有價值了。

一句話題意:每次恰好覆蓋相鄰的兩個#,不能重復,求最大覆蓋次數。

解題報告:

? ?建模:用模板每次恰好覆蓋相鄰的兩個#,同一個點不能重復覆蓋,求最大覆蓋次數。

? 建圖:這題有兩種建圖方式。一種是奇偶建圖,即(i+j)的奇偶數來建圖,vis1表示左側集合,vis2表示右側。因為木板能且僅能只能放在(i+j)%2==0 和 (i+j)%2==1這兩個相鄰的位置上,所以我們這么去分集合。然后找匹配就可以了。

? ? ? ? ? ? ?第二種建圖方式就是如果是'#'那就++cnt,就是都用同一套體系來給‘#’標號,這樣最后匈牙利搞完之后答案/2即可。

下面分別給出代碼。

建圖方式1的AC代碼:

#include<bits/stdc++.h> using namespace std; int n,ans; int vis1[605][605],vis2[605][605],line[605][605]; bool used[605*605]; char maze[605][605]; int tot1,tot2; int nxt[605*605]; bool find(int x) {for(int i = 1; i<=tot2; i++) {if(line[x][i] && !used[i]) {used[i]=1;if(nxt[i] == 0 || find(nxt[i])) {nxt[i] = x;return 1;}}}return 0 ; } int match() {int sum = 0;memset(nxt,0,sizeof nxt);for(int i = 1; i<=tot1; i++) {memset(used,0,sizeof used);if(find(i)) sum++;}return sum; } int main() {int iCase = 0,t=0;scanf("%d",&t);while(t--) {tot1 = tot2 = 0;scanf("%d",&n);memset(vis1,0,sizeof vis1);memset(vis2,0,sizeof vis2);memset(line,0,sizeof line);for(int i = 1; i<=n; i++) {scanf("%s",maze[i]+1);}for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {if(maze[i][j] == '#') {if((i+j)&1) vis2[i][j] = ++tot2;else vis1[i][j] = ++tot1;}}}for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {if((i+j)&1) continue;if(maze[i][j] == '#') {if(maze[i][j+1] == '#' && j+1 <= n) line[vis1[i][j]][vis2[i][j+1]]=1;if(maze[i][j-1] == '#' && j-1 >= 1) line[vis1[i][j]][vis2[i][j-1]]=1;if(maze[i+1][j] == '#' && i+1 <= n) line[vis1[i][j]][vis2[i+1][j]]=1;if(maze[i-1][j] == '#' && i-1 >= 1) line[vis1[i][j]][vis2[i-1][j]]=1;}}}printf("Case %d: %d\n",++iCase,match());}return 0 ; }

建圖方式2的AC代碼:

#include<bits/stdc++.h> using namespace std; int n,ans; int vis[605][605],line[605][605]; bool used[605*605]; char maze[605][605]; int tot; int nxt[605*605]; bool find(int x) {for(int i = 1; i<=tot; i++) {if(line[x][i] && !used[i]) {used[i]=1;if(nxt[i] == 0 || find(nxt[i])) {nxt[i] = x;return 1;}}}return 0 ; } int match() {int sum = 0;memset(nxt,0,sizeof nxt);for(int i = 1; i<=tot; i++) {memset(used,0,sizeof used);if(find(i)) sum++;}return sum; } int main() {int iCase = 0,t=0;scanf("%d",&t);while(t--) { // tot1 = tot2 = 0;tot=0;scanf("%d",&n);memset(vis,0,sizeof vis);memset(line,0,sizeof line);for(int i = 1; i<=n; i++) {scanf("%s",maze[i]+1);}for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {if(maze[i][j] == '#') {vis[i][j] = ++tot;}}}for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {if(maze[i][j] == '#') {if(maze[i][j+1] == '#' && j+1 <= n) line[vis[i][j]][vis[i][j+1]]=1;if(maze[i][j-1] == '#' && j-1 >= 1) line[vis[i][j]][vis[i][j-1]]=1;if(maze[i+1][j] == '#' && i+1 <= n) line[vis[i][j]][vis[i+1][j]]=1;if(maze[i-1][j] == '#' && i-1 >= 1) line[vis[i][j]][vis[i-1][j]]=1;}}}printf("Case %d: %d\n",++iCase,match()/2);}return 0 ; }

總結:

? ?再好好想想建圖!和POJ - 2226這個最小點覆蓋的題比較一下,也是寫過博客的。這倆題還是有共同之處的。

總結

以上是生活随笔為你收集整理的【HDU - 4185】Oil Skimming (二分图,建图,匈牙利算法)的全部內容,希望文章能夠幫你解決所遇到的問題。

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