程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> ZOJ 1654 Place the Robots(超牛的建圖思路) - from lanshui_Yang

ZOJ 1654 Place the Robots(超牛的建圖思路) - from lanshui_Yang

編輯:C++入門知識

Place the Robots

--------------------------------------------------------------------------------

Time Limit: 5 Seconds Memory Limit: 32768 KB

--------------------------------------------------------------------------------

Robert is a famous engineer. One day he was given a task by his boss. The background of the task was the following:

Given a map consisting of square blocks. There were three kinds of blocks: Wall, Grass, and Empty. His boss wanted to place as many robots as possible in the map. Each robot held a laser weapon which could shoot to four directions (north, east, south, west) simultaneously. A robot had to stay at the block where it was initially placed all the time and to keep firing all the time. The laser beams certainly could pass the grid of Grass, but could not pass the grid of Wall. A robot could only be placed in an Empty block. Surely the boss would not want to see one robot hurting another. In other words, two robots must not be placed in one line (horizontally or vertically) unless there is a Wall between them.

Now that you are such a smart programmer and one of Robert's best friends, He is asking you to help him solving this problem. That is, given the description of a map, compute the maximum number of robots that can be placed in the map.


Input

The first line contains an integer T (<= 11) which is the number of test cases.

For each test case, the first line contains two integers m and n (1<= m, n <=50) which are the row and column sizes of the map. Then m lines follow, each contains n characters of '#', '*', or 'o' which represent Wall, Grass, and Empty, respectively.


Output

For each test case, first output the case number in one line, in the format: "Case :id" where id is the test case number, counting from 1. In the second line just output the maximum number of robots that can be placed in that map.


Sample Input

2
4 4
o***
*###
oo#o
***o
4 4
#ooo
o#oo
oo#o
***#


Sample Output

Case :1
3
Case :2
5

      

       題目大意:給定一個 m*n 的地圖,地圖有方格組成,在地圖中有三種方格——牆、草地和空地。要求在地圖中放置盡可能多的機器人,每個機器人都配備了激光槍,可以同時向4個方向(上、下、左、右)開槍。機器人一直待在最初始放置的方格處,不可移動,然後一直向四個方向開槍。激光槍發射的激光可以穿透草地,但不能穿透牆壁。機器人只能放置在空地,並且不能使機器人相互攻擊。輸出可以放置的機器人的最大數目。‘o’表示空地,‘#’表示牆,‘*’表示草地。

       解題思路:此題不太容易想,關鍵是如何把此題轉換成求解二部圖最大匹配的問題,也就是如何建圖。

      下面具體闡述:將每一行被牆隔開且包含空地的連續區域稱作”塊“,顯然,在一個塊中,最多只能放置一個機器人。把這些塊編上號,注意:在同一行中,如果兩個空地之間沒有牆壁,只有草地,那麼這兩個空地應屬於“同一塊”。同樣,把豎直方向的塊也編上號。然後把每個橫向塊看作二部圖中頂點集合X中的頂點,豎向塊看作集合Y中的頂點,若兩個塊之間有公共的空地(注意,每兩個塊最多有一個公共空地),則在它們之間連邊。由於每條邊表示一個空地,有沖突的空地之間必有公共頂點,所以問題轉化為在二部圖中找沒有公共頂點的最大邊集,也就是求最大匹配問題。

        請看代碼:

 

#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std ;
const int MAXN = 55 ;
int n , m ;
char map[MAXN][MAXN] ;
short g[MAXN*MAXN][MAXN*MAXN] ; // 數組一定不要開小,否則可能直接WA !!
                                // 注意使用short或bool ,避免MLE
int cx[MAXN * MAXN] ;
int cy[MAXN * MAXN] ;
struct Point
{
    int br ;
    int bc ;
    Point():br(-1) , bc(-1) {}
} s[MAXN * MAXN] ;
int ha[MAXN][MAXN] ;     // 輔助數組
bool vis[MAXN * MAXN] ;  // 標記數組
int sumbr = -1 ;
int sumbc = -1 ;
void init ()
{
    memset(ha , -1 , sizeof(ha)) ;
    memset(g , 0 , sizeof(g)) ;
    memset(cx , -1 , sizeof(cx)) ;
    memset(cy , -1 , sizeof(cy)) ;
    sumbr = -1 ;   // 記錄橫向塊數
    sumbc = -1 ;   //記錄豎向塊數
    scanf("%d%d\n" , &n , &m) ;
    int i , j ;
    for(i = 0 ; i < n ; i ++)
    {
        scanf("%s" , map[i]) ;
    }
    int cnt = 0 ;
    int first = -1 ;
    for(i = 0 ; i < n ; i ++)  // 給橫向塊標號,從0開始
    {
        first = -1 ;
        for(j = 0 ; j < m ; j ++)
        {
            if(map[i][j] == '#')
            {
                first = -1 ;
            }
            if(map[i][j] == 'o')
            {
                if(first == -1)
                {
                    sumbr ++ ;
                    first = 1 ;
                }
                s[cnt].br = sumbr ;
                ha[i][j] = cnt ;
                cnt ++ ;
            }
        }
    }
    for(j = 0 ; j < m ; j ++)  // 給豎向塊標號,從0開始
    {
        first = -1 ;
        for(i = 0 ; i < n ; i ++)
        {
            if(map[i][j] == '#')
            {
                first = -1 ;
            }
            if(map[i][j] == 'o')
            {
                if(first == -1)
                {
                    sumbc ++ ;
                    first = 1 ;
                }
                int tmp = ha[i][j] ;
                s[tmp].bc = sumbc ;
            }
        }
    }
    for(i = 0 ; i < cnt ; i ++)  // 建圖
    {
        g[ s[i].br ][ s[i].bc ] = 1 ;
    }
}
int ca = 0 ;
int path(int v)  // 匈牙利算法
{
    int i ;
    for(i = 0 ; i <= sumbc ; i ++)
    {
        if(g[v][i] == 1 && !vis[i])
        {
            vis[i] = true ;
            if(cy[i] == -1 || path(cy[i]))
            {
                cy[i] = v ;
                cx[v] = i ;
                return 1 ;
            }
        }
    }
    return 0 ;
}
void solve()
{
    int i ;
    int ans = 0 ;
    for(i = 0 ; i <= sumbr ; i ++)
    {
        if(cx[i] == -1)
        {
            memset(vis , 0 , sizeof(vis)) ;
            if(path(i))
                ans ++ ;
        }
    }
    printf("Case :%d\n" , ++ ca) ;
    printf("%d\n" , ans) ;
}
int main()
{
    int T ;
    scanf("%d" , &T) ;
    while (T --)
    {
        init() ;
        solve() ;
    }
    return 0 ;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved