题目描述
输入
输出
输出一个整数a,表示最多能放置炸弹的个数
样例输入
```
4 4
#***
*#**
**#*
xxx#
```
样例输出
5
题解:每一个空位都能引爆一行和列,将行和列拆分出来,进行二分图匹配,当然也可以用网络流来做
二分图匹配:
#include
#include
#include
#define N 6000
#define M 300000
using namespace std;
int map[60][60] , mp[1000][1000], bx[60][60] , by[60][60] , tx , ty , head[N] , to[M] , val[M] , next[M] , cnt = 1 , s , t , dis[N] , frm[N];
char str[60];
bool vis[N];
bool bfs(int x)
{
for(int i = 1; i <= ty ; ++i)
{
if(!mp[x][i] || vis[i]) continue;
vis[i] = 1;
if(!frm[i] || bfs(frm[i])) {
frm[i] = x;
return true;
}
}
return false;
}
int main()
{
int n , m , i , j , ans = 0;
scanf("%d%d" , &n , &m);
for(i = 1 ; i <= n ; i ++ )
{
scanf("%s" , str + 1);
for(j = 1 ; j <= m ; j ++ )
map[i][j] = (str[j] == '*' ? 0 : str[j] == 'x' ? 1 : 2);
}
for(i = 1 ; i <= n ; i ++ )
{
tx ++ ;
for(j = 1 ; j <= m ; j ++ )
{
bx[i][j] = tx;
if(map[i][j] == 2) tx ++ ;
}
}
for(j = 1 ; j <= m ; j ++ )
{
ty ++ ;
for(i = 1 ; i <= n ; i ++ )
{
by[i][j] = ty;
if(map[i][j] == 2) ty ++ ;
}
}
for(i = 1 ; i <= n ; i ++ )
for(j = 1 ; j <= m ; j ++ )
if(!map[i][j])
mp[bx[i][j]][by[i][j]] = 1;
for(int i = 1; i <= tx ; ++i)
{
memset(vis,0,sizeof(vis));
if(bfs(i)) ++ans;
}
printf("%d\n" , ans);
return 0;
}
网络流:
#include
#include
#include
#define N 6000
#define M 300000
using namespace std;
queue q;
int map[60][60] , bx[60][60] , by[60][60] , tx , ty , head[N] , to[M] , val[M] , next[M] , cnt = 1 , s , t , dis[N];
char str[60];
void add(int x , int y , int z)
{
to[++cnt] = y , val[cnt] = z , next[cnt] = head[x] , head[x] = cnt;
to[++cnt] = x , val[cnt] = 0 , next[cnt] = head[y] , head[y] = cnt;
}
bool bfs()
{
int x , i;
memset(dis , 0 , sizeof(dis));
while(!q.empty()) q.pop();
dis[s] = 1 , q.push(s);
while(!q.empty())
{
x = q.front() , q.pop();
for(i = head[x] ; i ; i = next[i])
{
if(val[i] && !dis[to[i]])
{
dis[to[i]] = dis[x] + 1;
if(to[i] == t) return 1;
q.push(to[i]);
}
}
}
return 0;
}
int dinic(int x , int low)
{
if(x == t) return low;
int temp = low , i , k;
for(i = head[x] ; i ; i = next[i])
{
if(val[i] && dis[to[i]] == dis[x] + 1)
{
k = dinic(to[i] , min(temp , val[i]));
if(!k) dis[to[i]] = 0;
val[i] -= k , val[i ^ 1] += k;
if(!(temp -= k)) break;
}
}
return low - temp;
}
int main()
{
int n , m , i , j , ans = 0;
scanf("%d%d" , &n , &m);
for(i = 1 ; i <= n ; i ++ )
{
scanf("%s" , str + 1);
for(j = 1 ; j <= m ; j ++ )
map[i][j] = (str[j] == '*' ? 0 : str[j] == 'x' ? 1 : 2);
}
for(i = 1 ; i <= n ; i ++ )
{
tx ++ ;
for(j = 1 ; j <= m ; j ++ )
{
bx[i][j] = tx;
if(map[i][j] == 2) tx ++ ;
}
}
for(j = 1 ; j <= m ; j ++ )
{
ty ++ ;
for(i = 1 ; i <= n ; i ++ )
{
by[i][j] = ty;
if(map[i][j] == 2) ty ++ ;
}
}
s = 0 , t = tx + ty + 1;
for(i = 1 ; i <= tx ; i ++ ) add(s , i , 1);
for(i = 1 ; i <= ty ; i ++ ) add(i + tx , t , 1);
for(i = 1 ; i <= n ; i ++ )
for(j = 1 ; j <= m ; j ++ )
if(!map[i][j])
add(bx[i][j] , by[i][j] + tx , 1);
while(bfs()) ans += dinic(s , 1 << 30);
printf("%d\n" , ans);
return 0;
}