POJ2446Chessboard
来源:互联网 发布:夏洛克原著父母 知乎 编辑:程序博客网 时间:2024/06/14 05:15
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.
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 22 13 3
Sample Output
YES
Hint
A possible solution for the sample input.
这题太坑,坐标输进来是列,行的,开始没注意错了很久,其他没什么难的,只要相邻且不是洞就连边。
然后求最大匹配,如果最大匹配等于剩余格子数量,就是YES,否则是NO
#include<stdio.h>#include<string.h>const int maxn=1605;struct node{ int to; int next;}edge[4*maxn];int head[maxn];int mark[maxn];bool used[maxn];bool mat[55][55];int cnt[55][55];int tot;int n,m;int index;void addedge(int from,int to){ edge[tot].to=to; edge[tot].next=head[from]; head[from]=tot++;}bool dfs(int x){ for (int i=head[x];i!=-1;i=edge[i].next) { if (!used[edge[i].to]) { used[edge[i].to]=1; if (mark[edge[i].to]==-1 || dfs(mark[edge[i].to])) { mark[edge[i].to]=x; return true; } } } return false;}int hungary(){ memset(mark,-1,sizeof(mark)); int ans=0; for (int i=0;i<index;i++) { memset(used,0,sizeof(used)); if (dfs(i)) ans++; } return ans;}int main(){ int k; while (~scanf("%d%d%d",&n,&m,&k)) { memset(head,-1,sizeof(head)); memset(mat,false,sizeof(mat)); tot=0; int x,y; for (int i=0;i<k;i++) { scanf("%d%d",&x,&y); x--; y--; mat[y][x]=true; } index=0; for (int i=0;i<n;i++) for (int j=0;j<m;j++) if (!mat[i][j]) cnt[i][j]=index++; for (int i=0;i<n;i++) for (int j=0;j<m;j++) { if (!mat[i][j]) { if (i>0 && !mat[i-1][j]) addedge(cnt[i][j],cnt[i-1][j]); if (i<n-1 && !mat[i+1][j]) addedge(cnt[i][j],cnt[i+1][j]); if (j>0 && !mat[i][j-1]) addedge(cnt[i][j],cnt[i][j-1]); if (j<m-1 && !mat[i][j+1]) addedge(cnt[i][j],cnt[i][j+1]); } } int res=hungary(); if (res==index) printf("YES\n"); else printf("NO\n"); } return 0;}
0 0
- POJ2446Chessboard
- poj2446Chessboard 奇偶建图
- HDU2189 悼念512汶川大地震遇难同胞——来生一起走 【母函数】
- Android OpenGL ES 开发教程(17):投影变换Projection
- Word Ladder && Word Ladder II ---TLE的盛宴
- linux 常用命令
- Linux下安装JRE和Eclipse IDE for C/C++ Developers
- POJ2446Chessboard
- HDU 1002 A + B Problem II(大整数相加)
- Linux下高并发socket最大连接数
- Android之单击返回键两次退出
- String to Integer (atoi)
- 二十一:微信公众帐号开发-符号表情的发送(上)
- POJ 2349 Arctic Network
- krpano 教程 - events事件标签中文说明
- GPT分区基础知识及如何在GPT分区上安装WIN7