sgu234 Black-White King Strikes Back（二分图 输出方案）

题意：

n*m的0,1矩阵(n,m<=200)，要你在1的地方放士兵，不能现

tip：

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
typedef pair<int,int> pii;
const int maxn = 210;
int dir[4][2] = { {0,1},{0,-1},{-1,0},{1,0} };
int a[maxn][maxn],n,m,ans,res,cnt,vis[maxn][maxn],num,book[maxn][maxn];
char s[maxn][maxn];
void init(){
scanf("%d%d",&n,&m);
for(int i = 1; i <= n ; i++){
for(int j =1; j <= m ; j++){
s[i][j] = '.';
scanf("%d",&a[i][j]);
if(a[i][j] == 1)    cnt++;
else    s[i][j] = '#';
}
}

}
bool Hungary (int x,int y){
for(int i = 0 ;i < 4; i++){
int u = x+dir[i][0] ,v = y+ dir[i][1];
if(u <= 0 || u > n || v <=0 || v >m || a[u][v] == 0||vis[u][v]==num)    continue;
vis[u][v] = num;
return true;
}
}
return false;
}

void sov(){
for(int i = 1; i <= n ; i++){
for(int j = 1; j <= m ; j++){
if( (i+j) % 2 == 0 && a[i][j] ){
num++;
if(Hungary(i,j))    res++;
}
}
}
printf("%d\n",cnt-res);
}
void dfs(int x,int y){
book[x][y] = 1;
s[x][y] = 'G';
for(int i = 0 ; i < 4; i++){
int u = x+dir[i][0],v = y+dir[i][1];
if(u >= 1 && u <= n && v >=1 && v <=m && book[u][v] == 0){
book[u][v] = 1;
}
}
}
}
void print(){
for(int i = 1; i <= n ; i++){
for(int j = 1; j <= m ; j++){
dfs(i,j);
}
}
}
for(int i = 1; i <= n ; i++){
for(int j = 1; j <=m ; j++){
if(s[i][j] == '.' && (i+j)% 2 && book[i][j] == 0){
s[i][j] = 'G';
}
putchar(s[i][j]);
}
printf("\n");
}
}
int main(){
init();
sov();
print();
}