poj3020

匈牙利,似乎还是有多余空格

View Code
   
     
#include < iostream >
#include
< cstdio >
#include
< cstdlib >
#include
< cstring >
using namespace std;

#define maxn 55

char map[maxn][maxn];
bool chk[maxn][maxn];
int match[maxn][maxn];
int tot, n, m;
int dir[ 4 ][ 2 ] =
{
{
0 , 1 },
{
1 , 0 },
{
- 1 , 0 },
{
0 , - 1 } };

void input()
{
scanf(
" %d%d " , & n, & m);
gets(map[
0 ]);
tot
= 0 ;
for ( int i = 0 ; i < n; i ++ )
gets(map[i]);
for ( int i = 0 ; i < n; i ++ )
for ( int j = 0 ; j < m; j ++ )
if (map[i][j] == ' * ' )
tot
++ ;
}

bool SearchPath( int x, int y)
{
for ( int i = 0 ; i < 4 ; i ++ )
{
int x1 = x + dir[i][ 0 ];
int y1 = y + dir[i][ 1 ];
if ( ! chk[x1][y1] && map[x1][y1] == ' * ' )
{
chk[x1][y1]
= true ;
if (match[x1][y1] == - 1 || SearchPath(x1 - dir[match[x1][y1]][ 0 ],
y1
- dir[match[x1][y1]][ 1 ]))
{
match[x1][y1]
= i;
return true ;
}
}
}
return false ;
}

int MaxMatch()
{
int ret = 0 ;
memset(match,
- 1 , sizeof (match));
for ( int i = 0 ; i < n; i ++ )
for ( int j = 0 ; j < m; j ++ )
if (((i + j) & 1 ) && map[i][j] == ' * ' && match[i][j] == - 1 )
{
memset(chk,
0 , sizeof (chk));
if (SearchPath(i, j))
ret
++ ;
}
return ret;
}

int main()
{
// freopen("t.txt", "r", stdin);
int t;
scanf(
" %d " , & t);
while (t -- )
{
input();
printf(
" %d\n " , tot - MaxMatch());
}
return 0 ;
}

你可能感兴趣的:(poj)