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<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int cap[1501][1501];//初始化要清零
int _link[1501];
bool used[1501];
int nx,ny;//1->nx
bool _find(int t)
{
for(int i=1;i<=ny;i++)
if(!used[i]&&cap[t][i]==1)
{
used[i]=true;
if(_link[i]==-1||_find(_link[i]))
{
_link[i]=t;
return true;
}
}
return false;
}
int MaxMatch()
{
int num=0;
memset(_link,-1,sizeof(_link));
for(int i=1;i<=nx;i++)
{
memset(used,false,sizeof(used));
if(_find(i)) num++;
}
return num;
}
char str[60][60];
int nr[60][60],nc[60][60];
int main()
{
int ci,pl=1;scanf("%d",&ci);
while(ci--)
{
int r,c;
scanf("%d%d",&r,&c);
for(int i=0;i<r;i++) scanf("%s",str[i]);
nx=0;
memset(nr,0,sizeof(nr));
memset(nc,0,sizeof(nc));
for(int i=0;i<r;i++)
{
int flag=0;//上一个是墙壁
for(int j=0;j<c;j++)
{
if(str[i][j]=='o')
{
if(flag==0) nx++;
nr[i][j]=nx;
flag=1;//上一个是机器人
}
else if(str[i][j]=='#') flag=0;
}
}
ny=0;
for(int j=0;j<c;j++)
{
int flag=0;//上一个是墙壁
for(int i=0;i<r;i++)
{
if(str[i][j]=='o')
{
if(flag==0) ny++;
nc[i][j]=ny;
flag=1;//上一个是机器人
}
else if(str[i][j]=='#') flag=0;
}
}
memset(cap,0,sizeof(cap));//important
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
if(nr[i][j])
{
cap[nr[i][j]][nc[i][j]]=1;
}
}
}
int ans=MaxMatch();
printf("Case :%d\n",pl++);
printf("%d\n",ans);
}
return 0;
}
解决一个关于在地图上放置尽可能多的机器人的问题,确保它们不会互相伤害,同时考虑墙壁和空地等因素。
2352

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



