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
思路:二分匹配,一行变多行,一列变多列;
#include<stdio.h>
#include<string.h>
int mapx[100][100];
int mapy[100][100];
int x,y,n,m;
int visit[2500];
int match[2500][2500];
int link[2500];
char mmp[100][100];
int find(int t)
{
int i;
for(i=1;i<=y;i++)
{
if(!visit[i]&&match[t][i])
{
visit[i]=1;
if(!link[i]||find(link[i]))
{
link[i]=t;
return 1;
}
}
}
return 0;
}
int main()
{
int t;
while(scanf("%d",&t)!=EOF)
{
int k;
for(k=1;k<=t;k++)
{
memset(link,0,sizeof(link));
memset(match,0,sizeof(match));
memset(mapx,0,sizeof(mapx));
memset(mapy,0,sizeof(mapy));
scanf("%d %d",&n,&m);
getchar();
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
mmp[i][j]=getchar();
mapx[i][j]=i; //记录原来的x;
mapy[i][j]=j; //记录原来的y;(可以用结构体)
}
getchar();
}
x=0;
for(i=1;i<=n;i++) //在一行中出现#就重新换一行;
{
for(j=1;j<=m;j++)
{
if(mmp[i][j]=='#'&&j!=1&&mmp[i][j-1]!='#') x++;
else if(mmp[i][j]=='o') mapx[i][j]+=x;
}
}
y=m+1;
for(i=1;i<=m;i++) //在一列中出现#就重新换一列,和x不一样要从m+1开始;
{
int g=0;
for(j=1;j<=n;j++)
{
if(mmp[j][i]!='#'&&!g) continue;
else if(mmp[j][i]=='#'&&j!=1&&mmp[j-1][i]!='#')
{
if(!g) g=1;
else y++;
}
else if(mmp[j][i]=='o') mapy[j][i]=y;
}
y++;
}
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(mmp[i][j]=='o')
{
match[mapx[i][j]][mapy[i][j]]=1;
}
}
}
int sum=0;
for(i=1;i<=n+x;i++)
{
memset(visit,0,sizeof(visit));
if(find(i))
sum++;
}
printf("Case :%d\n",k);
printf("%d\n",sum);
}
}
}
本文介绍了一个有趣的算法问题——如何在一个由墙壁、草地和空地组成的地图上放置尽可能多的机器人,使得任意两个机器人的激光射线不会直接伤害到对方。通过二分匹配的方法,将地图转换为更易于计算的形式,并实现了一种有效的算法来解决该问题。
2354

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



