您的位置:首页 > 其它

poj 2446 Chessboard (二分匹配)

2014-06-10 12:50 465 查看
Chessboard

Time Limit: 2000MSMemory Limit: 65536K
Total Submissions: 12800Accepted: 4000
Description

Alice and Bob often play games on chessboard. One day, Alice draws a board with size M * N. She wants Bob to use a lot of cards with size 1 * 2 to cover the board. However, she thinks it too easy to bob, so she makes some holes on the board (as shown in the figure below).



We call a grid, which doesn’t contain a hole, a normal grid. Bob has to follow the rules below:
1. Any normal grid should be covered with exactly one card.
2. One card should cover exactly 2 normal adjacent grids.

Some examples are given in the figures below:



A VALID solution.



An invalid solution, because the hole of red color is covered with a card.



An invalid solution, because there exists a grid, which is not covered.
Your task is to help Bob to decide whether or not the chessboard can be covered according to the rules above.
Input

There are 3 integers in the first line: m, n, k (0 < m, n <= 32, 0 <= K < m * n), the number of rows, column and holes. In the next k lines, there is a pair of integers (x, y) in each line, which represents a hole in the y-th row, the x-th column.
Output

If the board can be covered, output "YES". Otherwise, output "NO".
Sample Input

4 3 2
2 1
3 3

Sample Output

YES

Hint



A possible solution for the sample input.
Source

POJ Monthly,charlescpp

和 hdu 1507类似,构无向图然后判断匹配数是否等于合法的格数。

心算32*32错了= = RE了两次,开始以为32*32是90+,第二次以为是900+,笔算后才知道是1024..

//224K    125MS    C++    1731B    2014-06-10 12:44:41
#include<iostream>
#include<vector>
#define N 1050
using namespace std;
vector<int>V
;
int match
;
int vis
;
int g[35][35];
int dfs(int u)
{
for(int i=0;i<V[u].size();i++){
int v=V[u][i];
if(!vis[v]){
vis[v]=1;
if(match[v]==-1 ||  dfs(match[v])){
match[v]=u;
return 1;
}
}
}
return 0;
}
int hungary(int n)
{
int ret=0;
memset(match,-1,sizeof(match));
for(int i=1;i<=n;i++){
memset(vis,0,sizeof(vis));
ret+=dfs(i);
}
return ret;
}
int main(void)
{
int n,m,k,x,y;
while(scanf("%d%d%d",&n,&m,&k)!=EOF)
{
memset(g,0,sizeof(g));
for(int i=0;i<N;i++) V[i].clear();
for(int i=0;i<k;i++){
scanf("%d%d",&y,&x);
g[x-1][y]=1;
}
int map
={0},pos=0;
for(int i=0;i<n;i++)
for(int j=1;j<=m;j++)
if(!g[i][j]){
if(!map[i*m+j]) map[i*m+j]=++pos;
int u=map[i*m+j];
if(j<m && !g[i][j+1]){
if(!map[i*m+j+1]) map[i*m+j+1]=++pos;
V[u].push_back(map[i*m+j+1]);
V[map[i*m+j+1]].push_back(u);
}
if(i<n-1 &&  !g[i+1][j]){
if(!map[(i+1)*m+j]) map[(i+1)*m+j]=++pos;
V[u].push_back(map[(i+1)*m+j]);
V[map[(i+1)*m+j]].push_back(u);
}
}
//printf("%d\n",pos);
if(hungary(pos)==pos) puts("YES");
else puts("NO");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: