Place the Robots
问题 j: Place the Robots
时间限制: 1 Sec 内存限制: 128 MB提交: 5 解决: 2
[提交] [状态] [讨论版] [命题人:admin]
题目描述
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.
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.
输入
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.
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.
输出
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.
output the maximum number of robots that can be placed in that map.
样例输入
2
4 4
o***
*###
oo#o
***o
4 4
#ooo
o#oo
oo#o
***#
样例输出
Case :1
3
Case :2
5
思路:只需要考虑空地和墙壁即可,找出行和列的联通块,用交点建立二分图,进行最大匹配就好了。
#include<bits/stdc++.h> #include<vector> using namespace std; const int maxn=2e3+100; int ans,f[maxn],mt[maxn],n,m,cx,cy,cnt; int l[60][60],r[60][60]; char str[60][60]; vector<int>q[maxn]; int match(int now){ for(int i=0;i<q[now].size();i++){ if(!mt[q[now][i]]){ mt[q[now][i]]=1; if(f[q[now][i]]==-1||match(f[q[now][i]])){ f[q[now][i]]=now; return 1; } } } return 0; } int hg(){ ans=0; for(int i=1;i<=cx;i++){ memset(mt,0,sizeof(mt)); if(match(i))ans++; } return ans; } int main() { int T; cin>>T; while(T--){ for(int i=0;i<maxn;i++)f[i]=-1,q[i].clear(); cin>>n>>m; cx=cy=0; memset(l,0,sizeof(l)); memset(r,0,sizeof(r)); for(int i=1;i<=n;i++)scanf("%s",str[i]+1); for(int i=1;i<=n;i++){ int c=0; for(int j=1;j<=m;j++){ if(str[i][j]=='o'){ if(!c)++cx; c=1; l[i][j]=cx; }else if(str[i][j]=='#')c=0; } } for(int i=1;i<=m;i++){ int u=0; for(int j=1;j<=n;j++){ if(str[j][i]=='o'){ if(!u)++cy; u=1; r[j][i]=cy; }else if(str[j][i]=='#')u=0; } } for(int i=1;i<=cx;++i) q[i].clear(); for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(l[i][j]&&r[i][j]){ q[l[i][j]].push_back(r[i][j]); } } } cout<<"Case "<<':'<<++cnt<<endl; cout<<hg()<<endl; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!