Place the Robots

╰+攻爆jí腚メ 2021-12-23 12:57 248阅读 0赞

问题 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.

输入

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

样例输入

复制样例数据

  1. 2
  2. 4 4
  3. o***
  4. *###
  5. oo#o
  6. ***o
  7. 4 4
  8. #ooo
  9. o#oo
  10. oo#o
  11. ***#

样例输出

  1. Case :1
  2. 3
  3. Case :2
  4. 5
  5. 思路:只需要考虑空地和墙壁即可,找出行和列的联通块,用交点建立二分图,进行最大匹配就好了。
  6. #include<bits/stdc++.h>
  7. #include<vector>
  8. using namespace std;
  9. const int maxn=2e3+100;
  10. int ans,f[maxn],mt[maxn],n,m,cx,cy,cnt;
  11. int l[60][60],r[60][60];
  12. char str[60][60];
  13. vector<int>q[maxn];
  14. int match(int now){
  15. for(int i=0;i<q[now].size();i++){
  16. if(!mt[q[now][i]]){
  17. mt[q[now][i]]=1;
  18. if(f[q[now][i]]==-1||match(f[q[now][i]])){
  19. f[q[now][i]]=now;
  20. return 1;
  21. }
  22. }
  23. }
  24. return 0;
  25. }
  26. int hg(){
  27. ans=0;
  28. for(int i=1;i<=cx;i++){
  29. memset(mt,0,sizeof(mt));
  30. if(match(i))ans++;
  31. }
  32. return ans;
  33. }
  34. int main()
  35. {
  36. int T;
  37. cin>>T;
  38. while(T--){
  39. for(int i=0;i<maxn;i++)f[i]=-1,q[i].clear();
  40. cin>>n>>m;
  41. cx=cy=0;
  42. memset(l,0,sizeof(l));
  43. memset(r,0,sizeof(r));
  44. for(int i=1;i<=n;i++)scanf("%s",str[i]+1);
  45. for(int i=1;i<=n;i++){
  46. int c=0;
  47. for(int j=1;j<=m;j++){
  48. if(str[i][j]=='o'){
  49. if(!c)++cx;
  50. c=1;
  51. l[i][j]=cx;
  52. }else if(str[i][j]=='#')c=0;
  53. }
  54. }
  55. for(int i=1;i<=m;i++){
  56. int u=0;
  57. for(int j=1;j<=n;j++){
  58. if(str[j][i]=='o'){
  59. if(!u)++cy;
  60. u=1;
  61. r[j][i]=cy;
  62. }else if(str[j][i]=='#')u=0;
  63. }
  64. }
  65. for(int i=1;i<=cx;++i) q[i].clear();
  66. for(int i=1;i<=n;i++){
  67. for(int j=1;j<=m;j++){
  68. if(l[i][j]&&r[i][j]){
  69. q[l[i][j]].push_back(r[i][j]);
  70. }
  71. }
  72. }
  73. cout<<"Case "<<':'<<++cnt<<endl;
  74. cout<<hg()<<endl;
  75. }
  76. }

转载于:https://www.cnblogs.com/czy-power/p/10361287.html

发表评论

表情:
评论列表 (有 0 条评论,248人围观)

还没有评论,来说两句吧...

相关阅读

    相关 robots

    robots是网站跟爬虫间的协议,用简单直接的txt格式文本方式告诉对应的爬虫被允许的权限,也就是说robots.txt是搜索引擎中访问网站的时候要查看的第一个文件。当一个搜索

    相关 Robot Motion

    链接:http://poj.org/problem?id=1573 Problem Description: 机器人已经被编程以遵循其路径中的指示。机器人要移动的下一个方向

    相关 robots协议

              Robots协议(也称为爬虫协议、机器人协议等)的全称是“网络爬虫排除标准”(Robots Exclusion Protocol),网站通过Robots协议