UVA1514 piece it together

来源:互联网 发布:ff14猫娘的捏脸数据 编辑:程序博客网 时间:2024/05/16 22:15

题外话:比赛时只想到二分图匹配了居然忘记拆点这事,真不熟练,这次恰好长记性。

思路:拆点+二分图匹配

用vector会TLE,所有后来改用邻接表。

1514 - Piece it together

Time limit: 30.000 seconds

Tom has developed a special kind of puzzle: it involves a whole bunch of identical puzzle pieces. The pieces have the shape of three adjoint squares in an L-shape. The corner square is black, the two adjacent squares are white.

\epsfbox{p5903.eps}

A puzzle piece

The puzzler is given a pattern of black and white squares in a rectangular grid. The challenge is to create that pattern using these pieces. The pieces can be rotated, but must not overlap.

Tom has already designed a few nice patterns, but he needs to find out if they can be constructed with the pieces at all. Rather than trying to test this for each pattern by hand, he wants to write a computer program to determine this for him. Can you help him?

Input 

On the first line a positive integer: the number of test cases, at most 100. After that per test case:


  • one line with two integers n and m (1$ \le$nm$ \le$500): the height and width of the grid containing the pattern, respectively.
  • n lines, each containing m characters, denoting the grid. Each character is `B', `W', or `.', indicating a black, white or empty square respectively.

The grid contains at least one black or white square.

Output 

Per test case:


  • one line with either ``YES" or ``NO", indicating whether or not it is possible to construct the pattern with the puzzle pieces. You may assume that there is an infinite supply of pieces.

Sample Input 

23 4BWW.WWBW..WB3 3W..BW.WBW

Sample Output 

YESNO

#include <cstdio>#include <algorithm>#include <cstring>#include <vector>const int N = 250005;using namespace std;int b, w, r, c;char cap[505][505];int num[505][505];int left[N], vis[N];struct Edge{    int v, next;}edge[N*4];int cnt;int head[N];void addedge(int u, int v){    edge[cnt].v = v;    edge[cnt].next = head[u];    head[u] = cnt++;}int node;int match(int n){    for(int i = head[n]; i != -1; i = edge[i].next){        int j = edge[i].v;        if(!vis[j]){            vis[j] = true;            if(left[j] == -1 || match(left[j])){                left[j] = n;                return 1;            }        }    }    return 0;}int hungary(){    int sum = 0;    memset(left, -1, sizeof(left));    for(int i = 0; i < b; i++){        memset(vis, 0, sizeof(vis));        if(match(i)) sum++;        else break;    }    return sum;}int main(){    int cases;    scanf("%d", &cases);    while(cases--){        scanf("%d %d", &r, &c);        b = 0, w = 0;        for(int i = 0; i < r; i++){            scanf("%s", cap[i]);            for(int j = 0; j < c; j++){                if(cap[i][j] == 'B'){                    num[i][j] = b;                    b++;                }                if(cap[i][j] == 'W'){                    num[i][j] = w;                    w++;                }            }        }        if(b == 0 && w == 0){            printf("YES\n");            continue;        }                if(b*2 != w){            printf("NO\n");            continue;        }        cnt = 0;        memset(head, -1, sizeof(head));        for(int i = 0; i < r; i++){            for(int j = 0; j < c; j++){                if(cap[i][j] == 'B'){                    if(i > 0 && cap[i-1][j] == 'W'){                        addedge(num[i][j], num[i-1][j]);                    }                    if(i < r-1 && cap[i+1][j] == 'W'){                        addedge(num[i][j], num[i+1][j]);                    }                    if(j > 0 && cap[i][j-1] == 'W'){                        addedge(num[i][j]+b, num[i][j-1]);                    }                    if(j < c-1 && cap[i][j+1] == 'W'){                        addedge(num[i][j]+b, num[i][j+1]);                    }                }            }        }        b *= 2;        if(hungary() == w)            printf("YES\n");        else printf("NO\n");    }}