Robert is a famous engineer. One day he was given a task by his boss. The background of the task was the following:
Given a map consisting of square blocks. There were three kinds of blocks: Wall, Grass, and Empty. His boss wanted to place as many robots as possible in the map. Each robot held a laser weapon which could shoot to four directions (north, east, south, west) simultaneously. A robot had to stay at the block where it was initially placed all the time and to keep firing all the time. The laser beams certainly could pass the grid of Grass, but could not pass the grid of Wall. A robot could only be placed in an Empty block. Surely the boss would not want to see one robot hurting another. In other words, two robots must not be placed in one line (horizontally or vertically) unless there is a Wall between them.
Now that you are such a smart programmer and one of Robert's best friends, He is asking you to help him solving this problem. That is, given the description of a map, compute the maximum number of robots that can be placed in the map.
Input
The first line contains an integer T (<= 11) which is the number of test cases.
For each test case, the first line contains two integers m and n (1<= m, n <=50) which are the row and column sizes of the map. Then m lines follow, each contains n characters of '#', '*', or 'o' which represent Wall, Grass, and Empty, respectively.
Output
For each test case, first output the case number in one line, in the format: "Case :id" where id is the test case number, counting from 1. In the second line just output the maximum number of robots that can be placed in that map.
Sample Input
2
4 4
o***
*###
oo#o
***o
4 4
#ooo
o#oo
oo#o
***#
Sample Output
Case :1
3
Case :2
5
此题就是Fire Net的加强版。
不同的地方就是两个点相连的条件是有相交的空地(因为草地上不能放置机器人)。
//characters of '#', '*', or 'o' which represent Wall, Grass, and Empty, respectively.
#include<cstdio>
#include<cstring>
using namespace std;
#define MAX 1255
char map[MAX][MAX];
int colx[MAX][MAX],rowx[MAX][MAX];
bool path[MAX][MAX],visit[MAX];
int match[MAX];
bool SearchPath(int s,int m)
{
for(int i=0; i<m; ++i)
{
if(path[s][i]&&!visit[i])
{
visit[i]=true;
if(match[i]==-1||SearchPath(match[i],m))
{
match[i]=s;
return true;
}
}
}
return false;
}
int main()
{
int n,m,row,col,cas;
scanf("%d",&cas);
for(int t=1; t<=cas; ++t)
{
scanf("%d%d",&n,&m);
getchar();
for(int i=0; i<n; ++i)
gets(map[i]);
row=col=0;
memset(colx,-1,sizeof(colx));
memset(rowx,-1,sizeof(rowx));
for(int i=0; i<n; ++i)
{
for(int j=0; j<m; ++j)
{
if(map[i][j]!='#'&&rowx[i][j]==-1)
{
for(int k=j; map[i][k]!='#'&&k<m; ++k)
rowx[i][k]=row;
++row;
}
if(map[i][j]!='#'&&colx[i][j]==-1)
{
for(int k=i; map[k][j]!='#'&&k<n; ++k)
colx[k][j]=col;
++col;
}
}
}
memset(path,false,sizeof(path));
for(int i=0; i<n; ++i)
for(int j=0; j<m; ++j)
if(map[i][j]=='o'&&rowx[i][j]!=-1&&colx[i][j]!=-1)
path[rowx[i][j]][colx[i][j]]=true;
int summ=0;
memset(match,-1,sizeof(match));
for(int i=0; i<row; ++i)
{
memset(visit,false,sizeof(visit));
if(SearchPath(i,col))
summ++;
}
printf("Case :%d\n%d\n",t,summ);
}
return 0;
}

本文介绍了一种解决特定机器人放置问题的方法,该问题要求在一张包含墙壁、草地和空地的地图上放置尽可能多的机器人,使得任意两个机器人不会直接互相伤害。通过构建二分图并运用匈牙利算法寻找最大匹配数来解决这一问题。
2354

被折叠的 条评论
为什么被折叠?



