HDU 3360 National Treasures 奇偶匹配最小點覆蓋

來源:互聯網
上載者:User

題目來源:HDU 3360 National Treasures

題意:如果a[i][j] != -1 把他轉成二進位 最多有12位 代表題目那張圖的12個位置 如果對應位是1 說明在那裡放一個守衛可以看住a[i][j]位置上的這個東西

思路:明顯死最小點覆蓋 奇偶匹配建圖 

#include <cstdio>#include <cstring>#include <vector>using namespace std;const int maxn = 55;int vis[maxn*maxn];int y[maxn*maxn];vector <int> G[maxn*maxn];int n, m;int a[maxn][maxn];int dir[12][2] = {-1, -2, -2, -1, -2, 1, -1, 2, 1, 2, 2, 1, 2, -1, 1, -2, -1, 0, 0, 1, 1, 0, 0, -1};bool dfs(int u){    for(int i = 0; i < G[u].size(); i++)    {        int v = G[u][i];        if(vis[v])            continue;        vis[v] = true;        if(y[v] == -1 || dfs(y[v]))        {            y[v] = u;            return true;        }    }    return false;}int match(){    int ans = 0;    memset(y, -1, sizeof(y));    for(int i = 0; i < n; i++)    {    for(int j = 0; j < m; j++)        {        if((i+j)%2){memset(vis, 0, sizeof(vis));        if(dfs(i*m+j))            ans++;}        }    }    return ans;}int main(){int cas = 1;while(scanf("%d %d", &n, &m) && (n||m)){for(int i = 0; i < n; i++)for(int j = 0; j < m; j++)scanf("%d", &a[i][j]);for(int i = 0; i < n*m; i++)G[i].clear();for(int i = 0; i < n; i++){for(int j = 0; j < m; j++){int x = a[i][j];if(x == -1)continue;for(int k = 0; k < 12; k++, x >>= 1){if(!x)break;if(!(x&1))continue;int xx = i + dir[k][0];int yy = j + dir[k][1];if(xx < 0 || xx >= n || yy < 0 || yy >= m)continue;if(a[xx][yy] == -1)continue;if((i+j)%2)G[i*m+j].push_back(xx*m+yy);elseG[xx*m+yy].push_back(i*m+j);}}}printf("%d. %d\n", cas++, match());}    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.