棋盘游戏
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2659 Accepted Submission(s): 1555
Problem Description
小希和Gardon在玩一个游戏:对一个N*M的棋盘,在格子里放尽量多的一些国际象棋里面的“车”,并且使得他们不能互相攻击,这当然很简单,但是Gardon限制了只有某些格子才可以放,小希还是很轻松的解决了这个问题(见下图)注意不能放车的地方不影响车的互相攻击。
所以现在Gardon想让小希来解决一个更难的问题,在保证尽量多的“车”的前提下,棋盘里有些格子是可以避开的,也就是说,不在这些格子上放车,也可以保证尽量多的“车”被放下。但是某些格子若不放子,就无法保证放尽量多的“车”,这样的格子被称做重要点。Gardon想让小希算出有多少个这样的重要点,你能解决这个问题么?
所以现在Gardon想让小希来解决一个更难的问题,在保证尽量多的“车”的前提下,棋盘里有些格子是可以避开的,也就是说,不在这些格子上放车,也可以保证尽量多的“车”被放下。但是某些格子若不放子,就无法保证放尽量多的“车”,这样的格子被称做重要点。Gardon想让小希算出有多少个这样的重要点,你能解决这个问题么?
Input
输入包含多组数据,
第一行有三个数N、M、K(1<N,M<=100 1<K<=N*M),表示了棋盘的高、宽,以及可以放“车”的格子数目。接下来的K行描述了所有格子的信息:每行两个数X和Y,表示了这个格子在棋盘中的位置。
第一行有三个数N、M、K(1<N,M<=100 1<K<=N*M),表示了棋盘的高、宽,以及可以放“车”的格子数目。接下来的K行描述了所有格子的信息:每行两个数X和Y,表示了这个格子在棋盘中的位置。
Output
对输入的每组数据,按照如下格式输出:
Board T have C important blanks for L chessmen.
Board T have C important blanks for L chessmen.
Sample Input
3 3 4 1 2 1 3 2 1 2 2 3 3 4 1 2 1 3 2 1 3 2
Sample Output
Board 1 have 0 important blanks for 2 chessmen. Board 2 have 3 important blanks for 3 chessmen.
Author
Gardon
Source
Recommend
lcy
#include <cstring>
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <map>
#include <queue>
using namespace std;
const int maxn = 110;
int N,M,K;
int g[maxn][maxn];
int mat[maxn];
int used[maxn];
bool hunary(int x)
{
for(int i=0;i<M;i++)
{
if(!used[i] && g[x][i])
{
used[i] = true;
if(mat[i] == -1 || hunary(mat[i]))
{
mat[i] = x;
return true;
}
}
}
return false;
}
int match()
{
int matches = 0;
memset(mat,-1,sizeof(mat));
for(int i=0;i<N;i++)
{
memset(used,0,sizeof(used));
if(hunary(i))
{
matches ++;
}
}
return matches;
}
int main()
{
freopen("1.txt","r",stdin);
int Case = 1;
while(scanf("%d%d%d",&N,&M,&K) != EOF)
{
int u,v;
memset(g,0,sizeof(g));
for(int i=0;i<K;i++)
{
scanf("%d%d",&u,&v);
u --;
v --;
g[u][v] = 1;
}
int matches = match();
int ans = 0;
for(int i=0;i<N;i++)
for(int j=0;j<M;j++)
{
if(g[i][j])
{
g[i][j] = 0;
int ret = match();
if(ret < matches)
{
ans ++;
}
g[i][j] = 1;
}
}
printf("Board %d have %d important blanks for %d chessmen.\n",Case ++,ans,matches);
}
return 0;
}