Uva1514 Piece it together

二分匹配   當時一看題覺得是最大流  就不停的試   兩百行代碼敲完  調試完 才發現建圖不對.....

後來想想   其實可以把黑色小塊拆成兩個   水平方向及豎直方向   兩個方向分別向各自方向白色小塊連邊

那麼匹配完全後才能輸出yes   否側就是no

唉  思想完全不夠啊   拆點都沒有想到!      牢記


還有就是2sat可以做   可以試試   但感覺建圖比匹配難


//#pragma comment(linker, "/STACK:102400000,102400000")
//HEAD
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <string>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
//LOOP
#define FF(i, a, b) for(int i = (a); i < (b); ++i)
#define FD(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define FE(i, a, b) for(int i = (a); i <= (b); ++i)
#define FED(i, b, a) for(int i = (b); i>= (a); --i)
#define REP(i, N) for(int i = 0; i < (N); ++i)
#define CLR(A,value) memset(A,value,sizeof(A))
#define CPY(a, b) memcpy(a, b, sizeof(a))
#define FC(it, c) for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
//STL
#define SZ(V) (int)V.size()
#define PB push_back
#define EQ(a, b) (fabs((a) - (b)) <= 1e-10)
#define ALL(c) (c).begin(), (c).end()
//INPUT
#define RI(n) scanf("%d", &n)
#define RII(n, m) scanf("%d%d", &n, &m)
#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)
#define RV(n, m, k, p, q) scanf("%d%d%d%d%d", &n, &m, &k, &p, &q)
#define RS(s) scanf("%s", s)
//OUTPUT
#define WI(n) printf("%d\n", n)
#define WS(s) printf("%s\n", s)
typedef long long LL;
typedef unsigned long long ULL;
typedef vector <int> VI;
const int INF = 100000000;
const double eps = 1e-10;
const int maxn = 510;
const int MAXN = 500 * 510;
const LL MOD = 1e9 + 7;

vector<int> G[maxn * maxn];
char mat[maxn][maxn];
int uN, vN, cnt, now;
int xM[MAXN], yM[MAXN];
int id[maxn][maxn];
int check[MAXN];

inline void add(int u, int v)
{
    G[u].PB(v);
}

inline void init()
{
    REP(i, MAXN)
        G[i].clear();
}

bool dfs(int u)
{
    int sz = G[u].size(), v;
    REP(i, sz)
    {
        v = G[u][i];
        if (check[v] != now)                ///保證每次增廣都是不同的路徑
        {
            check[v] = now;
            if (yM[v] == -1 || dfs(yM[v]))
            {
                yM[v] = u;
                xM[u] = v;
                return true;
            }
        }
    }
    return false;
}

int maxmatch()
{
    vN = uN = cnt;
    now = 0;
    int ret = 0;
    CLR(xM, -1);
    CLR(yM, -1);
    CLR(check, 0);
    REP(u, uN)
        if (xM[u] == -1)
        {                              ////加入時間戳優化,不必每次初始化標記數組
            now++;
            if (!dfs(u)) return false;
        }
    return true;
}

int main()
{
    int N, M, T;
    RI(T);
    while (T--)
    {
        init();
        cnt = uN = vN = 0;
        int white = 0, black = 0;
        RII(N, M);
        REP(i, N)
        {
            RS(mat[i]);
            REP(j, M)
                if(mat[i][j] == 'W')
                    id[i][j] = white++, cnt++;
                else if (mat[i][j] == 'B')      ///0 水平方向  +1 豎直方向
                    id[i][j] = black++, black++;
                else
                    id[i][j] = -1;
        }
        if (black != cnt)
        {
            puts("NO");
            continue;
        }
        REP(i, N)
            REP(j, M)
            {
                if (mat[i][j] == 'B')
                {
                    int x = id[i][j];
                    if (i + 1 < N && mat[i + 1][j] == 'W')
                        add(x + 1, id[i + 1][j]);
                    if (i - 1 >= 0 && mat[i - 1][j] == 'W')
                        add(x + 1, id[i - 1][j]);
                    if (j + 1 < M && mat[i][j + 1] == 'W')
                        add(x, id[i][j + 1]);
                    if (j - 1 >= 0 && mat[i][j - 1] == 'W')
                        add(x, id[i][j - 1]);
                }
            }
//        cout << ans << endl;
        if ( maxmatch())
            puts("YES");
        else
            puts("NO");
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章