1171 - Knights in Chessboard (II)
![]() ![]() | PDF (English) | Statistics | Forum |
Time Limit: 3 second(s) | Memory Limit: 32 MB |
Given an m x n chessboard where some of the cells are broken. Now you are about to place chess knights in the chessboard. You have to find the maximum number of knights that can be placed in the chessboard such that no two knights attack each other. You can't place knights in the broken cells.
Those who are not familiar with chess knights, note that a chess knight can attack eight positions in the board as shown in the picture below.
Input
Input starts with an integer T (≤ 125), denoting the number of test cases.
Each case starts with a blank line. The next line contains three integers m, n, K (1 ≤ m, n ≤ 200). Here m and n corresponds to the number of rows and the number of columns of the board respectively. Each of the next K lines will contain two integers x, y (1 ≤ x ≤ m, 1 ≤ y ≤ n) denoting that the cell(x, y) is broken already. No broken cell will be reported more than once.
Output
For each case of input, print the case number and the maximum number of knights that can be placed in the board considering the above restrictions.
Sample Input | Output for Sample Input |
2
8 8 0
2 5 4 1 3 1 4 2 3 2 4 | Case 1: 32 Case 2: 6 |
题目大意:一个n*m的棋盘上放棋子。然后有k个棋子是不能放的。放了一个棋子后,它周围八个地方就不能放了,问最多放多少个棋子。
奇偶建图, 但是参加匹配的是那些不能放在一起的棋子,这样我们得出一个匹配值后,就用所有的棋子个数减去匹配个数减去不能放的就是答案了
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=210;
const int maxm=40010;
struct Node
{
int to;
int next;
}edge[400010];
int cnt;
int head[maxm];
int match[maxm];
bool vis[maxm];
int map[maxn][maxn];
int dirx[]={-2,-2,-1,-1,2,1,2,1};
int diry[]={-1,1,-2,2,-1,-2,1,2};
int n,m;
int odd,even;
int getIndex(int x,int y)
{
return (x-1)*m+y;
}
void init()
{
memset(head,-1,sizeof(head));
cnt=0;
return;
}
void add(int u,int v)
{
edge[cnt].to=v;
edge[cnt].next=head[u];
head[u]=cnt++;
return;
}
bool dfs(int node)
{
for(int i=head[node];~i;i=edge[i].next)
{
int v=edge[i].to;
if(!vis[v])
{
vis[v]=true;
if(match[v]==-1||dfs(match[v]))
{
match[v]=node;
return true;
}
}
}
return false;
}
int hungry()
{
int ans=0;
memset(match,-1,sizeof(match));
for(int i=1;i<=odd;i++)
{
memset(vis,false,sizeof(vis));
if(dfs(i))
{
ans++;
}
}
return ans;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int test;
scanf("%d",&test);
for(int cas=1;cas<=test;cas++)
{
init();
int k;
memset(map,0,sizeof(map));
scanf("%d%d%d",&n,&m,&k);
int sum=0;
for(int i=0;i<k;i++)
{
int u,v;
scanf("%d%d",&u,&v);
map[u][v]=-1;
sum++;
}
odd=0;
even=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(map[i][j]==0)
{
if((i+j)%2)
{
map[i][j]=++odd;
}
else
{
map[i][j]=++even;
}
}
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(map[i][j]!=-1&&(i+j)%2)
{
for(int k=0;k<8;k++)
{
int nx=i+dirx[k];
int ny=j+diry[k];
if(nx>=1&&nx<=n&&ny>=1&&ny<=m&&map[nx][ny]!=-1)
{
add(map[i][j],map[nx][ny]);
}
}
}
}
}
int ans=hungry();
printf("Case %d: %d\n",cas,n*m-ans-k);
}
return 0;
}