ZOJ 1654 Place the Robots(放置机器人)

Place the Robots?problemCode=1654Time Limit: 5 Seconds Memory Limit: 32768 KB

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.

InputThe 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.

OutputFor 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

24 4o****###oo#o***o4 4#oooo#oooo#o***#

Sample Output

Case :13Case :25

题目描述: Robert 是一个著名的工程师。一天,他的老板给他分配了一个任务。任务的背景是:给定一 个m×n 大小的地图,地图由方格组成,在地图中有3 种方格-墙、草地和空地,他的老板希望 能在地图中放置尽可能多的机器人。每个机器人都配备了激光枪,可以同时向四个方向(上、下、 左、右)开枪。机器人一直待在最初始放置的方格处,不可移动,然后一直朝四个方向开枪。激 光枪发射出的激光可以穿透草地,但不能穿透墙壁。机器人只能放置在空地。当然,老板不希望 机器人互相攻击,也就是说,两个机器人不能放在同一行(水平或垂直),除非他们之间有一堵墙 格开。 给定一张地图,你的程序需要输出在该地图中可以放置的机器人的最大数目。

解题思路:

将每行相连接的空白快用一个序号表示,放在x集合中;每列相连的空白快用一个表示放在集合y中。(如果两个空白中间含有草地,那么他们与属于同一个序号中)

把每个横向块看作二部图中顶点集合X 中的顶点,竖向块看作集合Y 中的顶点,若两个块有公共的空地(注意,,每两个块最多有一个公共空地),则在它们之间连边。

所以问题转化为在二部图中找没有公共顶点的最大边集,这就是最大匹配问题。

更加详细可以参考:

#include<iostream>#include<cstring>#include<cstdio>#include<vector>#include<cmath>#include<algorithm>using namespace std;const int maxnn=2505;const int maxn=55;int n,m;int nx,ny;char pic[maxn][maxn];int xs[maxn][maxn],ys[maxn][maxn];vector<int> lin[maxnn];int from[maxnn];int tot;bool use[maxnn];int flag;bool match(int x){for(int i=0;i<lin[x].size();i++){if(!use[lin[x][i]]){use[lin[x][i]]=true;if(from[lin[x][i]]==-1 || match(from[lin[x][i]])){from[lin[x][i]]=x;return true;}}}return false;}int hungary(){tot=0;memset(from,255,sizeof(from));for(int i=1;i<=nx;i++){memset(use,0,sizeof(use));if(match(i))++tot;}return tot;}int main(){int N;cin>>N;for(int k=1;k<=N;k++){printf("Case :%d\n",k);scanf("%d %d",&n,&m);for(int i=0;i<n;i++) scanf("%s",pic[i]);nx=ny=0;memset(xs,0,sizeof(xs));memset(ys,0,sizeof(ys));for(int i=0;i<n;i++){flag=0;for(int j=0;j<n;j++){if(pic[i][j]=='o'){if(!flag) ++nx;flag=1;xs[i][j]=nx;}else if(pic[i][j]=='#')flag=0;}}for(int j=0;j<n;j++){flag=0;for(int i=0;i<n;i++){if(pic[i][j]=='o'){if(!flag) ++ny;flag=1;ys[i][j]=ny;}else if(pic[i][j]=='#')flag=0;}}for(int i=1;i<=nx;i++) lin[i].clear();for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(xs[i][j] && ys[i][j])lin[xs[i][j]].push_back(ys[i][j]);}}printf("%d\n",hungary());}return 0;}

版权声明:本文为博主原创文章,未经博主允许不得转载。

到底通向了什么样的远方呢?

ZOJ 1654 Place the Robots(放置机器人)

相关文章:

你感兴趣的文章:

标签云: