UVA1514 piece it together
2013-08-08 21:36
351 查看
题外话:比赛时只想到二分图匹配了居然忘记拆点这事,真不熟练,这次恰好长记性。
思路:拆点+二分图匹配
用vector会TLE,所有后来改用邻接表。
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.
[align=CENTER]A puzzle piece[/align]
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?
one line with two integers n and m (1
n, m
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.
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.
思路:拆点+二分图匹配
用vector会TLE,所有后来改用邻接表。
1514 - Piece it together
Time limit: 30.000 secondsTom 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.
[align=CENTER]A puzzle piece[/align]
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
n, m
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
2 3 4 BWW. WWBW ..WB 3 3 W.. BW. WBW
Sample Output
YES NO
#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 , vis ; struct Edge{ int v, next; }edge[N*4]; int cnt; int head ; 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 ; 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"); } }
相关文章推荐
- UVA 1514 Piece it together (二分图匹配)
- Uva1514 Piece it together
- uva 1514 - Piece it together(2SAT)
- UVA 1514 - Piece it together(二分图匹配+拆点)
- UVALive 5903 Piece it together(二分图匹配)
- UVALive 5903 Piece it together 二分匹配,拆点 难度:1
- UVALive 5903 Piece it together
- UVALive 5903 Piece it together
- uva658 - It's not a Bug, it's a Feature! 状态压缩+隐式图搜索+优先队列的dijkstra
- uva10341 Solve It(二分法求单调方程的根)
- UVa 10176 - Ocean Deep ! - Make it shallow !!
- Just Finish it up UVA - 11093
- UVA 10341 Solve It
- uvalive 4764 Bing it 基础dp
- 神经网络(14)--具体实现:put it together
- UVa 602 - What Day Is It?
- uva 10341 Solve it
- uva 10341 Solve It(二分搜索)
- UVa 574 - Sum It Up
- uva 10341 Solve It