UVA1514 piece it together
来源:互联网 发布:ff14猫娘的捏脸数据 编辑:程序博客网 时间:2024/05/16 22:15
题外话:比赛时只想到二分图匹配了居然忘记拆点这事,真不熟练,这次恰好长记性。
思路:拆点+二分图匹配
用vector会TLE,所有后来改用邻接表。
1514 - Piece it together
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.
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 (1n, m500): 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"); }}
- UVA1514 piece it together
- Uva1514 Piece it together
- UVALive 5903 Piece it together
- UVA 1514 - Piece it together(二分图匹配+拆点)
- uva 1514 - Piece it together(2SAT)
- Get it together 打起精神
- Bringing It All Together
- Putting It All Together
- SpokenEnglish--It's a piece of cake
- Putting it Together: Understanding Pointers
- Together,
- piece by piece
- First piece
- One Piece!
- code piece
- code piece.
- One piece
- one piece
- Qt drag和drop演示
- java编程练习一
- 翻转单词顺序
- LabVIEW让鼠标的光标显示曲线的值
- CYQ.Data V4系列全面开源(2013-08-04)
- UVA1514 piece it together
- strcpy函数
- NDK的awk错误:Android NDK: Host 'awk' tool is outdated. Please define......
- dsPIC30F osc config
- nyoj-2-括号配对问题
- B-树和B+树的应用:数据搜索和数据库索引
- 左旋转字符串
- 配置环境变量(java+ant)
- C风格字符串基本操作