实在是。。服了自己
光是记得输入了
忘记输入都是0的时候结束和读到文件末尾是不一样的
于是乎
改来改去
总算是成功了
也总算是能够证明我真的是懂得二部图的最大匹配的
只是一开始还是SF让我有点郁闷。。
应该是里面有些参数设的不规范
也总算是AC了
#include<iostream>
#include<string.h>
using namespace std;
#define MAXN 105
int n, m, imap[MAXN][MAXN], nei[MAXN][MAXN], pos[4][2] = { {0,-1},{0,1},{-1,0},{1,0} };
bool map[MAXN][MAXN],v[MAXN];//记录地图,从1开始
void creatnei(int i, int j);
int DFS(int start);
int num = 0, pre[MAXN],x=1;
int main()
{
int i = 0, j = 0, k = 0, a = 0, b = 0;
while (cin >> n >> m&&n&&m)
{
cin >> k;
memset(map, 0, sizeof(map));
memset(imap, 0, sizeof(imap));
memset(pre, -1, sizeof(pre));
memset(nei, 0, sizeof(nei));
num = 0;
x = 1;
for (i = 1; i <= k; i++) //产生地图
{
cin >> a >> b;
map[a][b] = 1;
}
for (i = 1; i <= n; i++) //产生编号
{
for (j = 1; j <= m; j++)
{
if (!map[i][j])
{
imap[i][j] = x++;
}
}
}
for (i = 1; i <= n; i++) //产生邻接矩阵
{
for (j = 1; j <= m; j++)
{
if (!map[i][j]&& (i + j & 1) )
{
creatnei(i, j);
}
}
}
for (i = 1; i < x; i++)
{
memset(v, 0, sizeof(v));
DFS(i);
}
for (i = 1; i <= x; i++)
if (pre[i] != -1)
num++;
cout << num / 2 << endl;
}
return 0;
}
void creatnei(int i, int j)
{
int t, a=0, b=0;
for (t = 0; t < 4; t++)
{
a = i + pos[t][0];
b = j + pos[t][1];
if (a >= 1 && a <= n&&b >= 1 && b <= m)
{
if (!map[a][b])
{
nei[imap[i][j]][imap[a][b]] = 1;
nei[imap[a][b]][imap[i][j]] = 1;
}
}
}
}
int DFS(int start)
{
for (int i = 1; i < x; i++)
{
if (nei[start][i]==1 && v[i]==0)
{
v[i] = 1;
if (pre[i] == -1 || DFS(pre[i]))
{
pre[i] = start;
return 1;
}
}
}
return 0;
}