Chessboard
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 14004 | Accepted: 4356 |
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 2 2 1 3 3
Sample Output
YES
Hint
A possible solution for the sample input.
组合数学的完全覆盖问题,对棋盘的格子进行黑白相间染色,去掉洞后,黑色的格子一个集合,白色的一个集合,建图,找最大匹配,是否等于剩下的格子数。若洞为两个的话,这两个同色,则不存在完美覆盖,不同色,就一定存在完美覆盖(nyoj328)
#include <iostream>
#include <cstdio>
#include <cstring>
#define N 2000
using namespace std;
bool map[N][N];
int is_hole[40][40];
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int n, m;
int linker[2000];
bool visit[2000];
int len;
bool dfs(int num)
{
for (int i = 1; i < len; i++)
{
if (!visit[i] && map[num][i])
{
visit[i] = 1;
if (linker[i] == -1 || dfs(linker[i]))
{
linker[i] = num;
return 1;
}
}
}
return 0;
}
int match()
{
int res = 0;
memset(linker, -1, sizeof(linker));
for (int i = 1; i < len ; i++)
{
memset(visit, 0, sizeof(visit));
if (dfs(i))
res++;
}
return res;
}
int main()
{
int num;
scanf("%d%d%d", &n, &m, &num);
memset(map, 0, sizeof(map));
for (int i = 0; i < num; i++)
{
int x, y;
scanf("%d%d", &y, &x);
is_hole[x][y] = -1;
}
if ((n * m - num) % 2)
{
printf("NO\n");
return 0;
}
len = 1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if(!is_hole[i][j])
is_hole[i][j] = len++;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (is_hole[i][j] != -1)
{
for (int k = 0; k < 4; k++)
{
int x = i + dx[k];
int y = j + dy[k];
if (x > 0 && y > 0 && x <= n && y <= m && is_hole[x][y] != -1)
{
int temp = is_hole[i][j];
int temp2 = is_hole[x][y];
map[temp][temp2] = 1;
}
}
}
}
}
int res = match();
if (res == (n *m - num))
printf("YES\n");
else
printf("NO\n");
return 0;
}