标题来源:

pid=3360">HDU 3360 National Treasures

意甲冠军:假设a[i][j] != -1 把他转成二进制 最多有12位 代表题目那张图的12个位置 假设相应位是1 说明在那里放一个守卫能够看住a[i][j]位置上的这个东西

思路:明显死最小点覆盖 奇偶匹配建图

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <vector>
  4. using namespace std;
  5. const int maxn = 55;
  6. int vis[maxn*maxn];
  7. int y[maxn*maxn];
  8. vector <int> G[maxn*maxn];
  9. int n, m;
  10. int a[maxn][maxn];
  11. int dir[12][2] = {-1, -2, -2, -1, -2, 1, -1, 2, 1, 2, 2, 1, 2, -1, 1, -2, -1, 0, 0, 1, 1, 0, 0, -1};
  12. bool dfs(int u)
  13. {
  14. for(int i = 0; i < G[u].size(); i++)
  15. {
  16. int v = G[u][i];
  17. if(vis[v])
  18. continue;
  19. vis[v] = true;
  20. if(y[v] == -1 || dfs(y[v]))
  21. {
  22. y[v] = u;
  23. return true;
  24. }
  25. }
  26. return false;
  27. }
  28. int match()
  29. {
  30. int ans = 0;
  31. memset(y, -1, sizeof(y));
  32. for(int i = 0; i < n; i++)
  33. {
  34. for(int j = 0; j < m; j++)
  35. {
  36. if((i+j)%2)
  37. {
  38. memset(vis, 0, sizeof(vis));
  39. if(dfs(i*m+j))
  40. ans++;
  41. }
  42. }
  43. }
  44. return ans;
  45. }
  46.  
  47. int main()
  48. {
  49. int cas = 1;
  50. while(scanf("%d %d", &n, &m) && (n||m))
  51. {
  52. for(int i = 0; i < n; i++)
  53. for(int j = 0; j < m; j++)
  54. scanf("%d", &a[i][j]);
  55. for(int i = 0; i < n*m; i++)
  56. G[i].clear();
  57. for(int i = 0; i < n; i++)
  58. {
  59. for(int j = 0; j < m; j++)
  60. {
  61. int x = a[i][j];
  62. if(x == -1)
  63. continue;
  64. for(int k = 0; k < 12; k++, x >>= 1)
  65. {
  66. if(!x)
  67. break;
  68. if(!(x&1))
  69. continue;
  70.  
  71. int xx = i + dir[k][0];
  72. int yy = j + dir[k][1];
  73. if(xx < 0 || xx >= n || yy < 0 || yy >= m)
  74. continue;
  75. if(a[xx][yy] == -1)
  76. continue;
  77. if((i+j)%2)
  78. G[i*m+j].push_back(xx*m+yy);
  79. else
  80. G[xx*m+yy].push_back(i*m+j);
  81. }
  82. }
  83. }
  84. printf("%d. %d\n", cas++, match());
  85. }
  86. return 0;
  87. }

版权声明:本文博客原创文章。博客,未经同意,不得转载。

HDU 3360 National Treasures 奇偶匹配的最低点覆盖的更多相关文章

  1. HDU 3360 National Treasures(二分匹配,最小点覆盖)

    National Treasures Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  2. HDU 3360 National Treasures(最小点覆盖)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3360 题目大意: 在一个n*m的格子中,每个格子有一个数值,-1表示空,其余表示财宝.每个财宝的数值转 ...

  3. HDU 3360 National Treasures

    题目大意:大厅每个位置都有一个文物或者一个守卫,文物是安全的前提是: 关键位置上必须有一个守卫,或者文物本身的位置上有一个守卫.求保证每个文物是安全的守卫的最少数量. #include <cst ...

  4. HDU 1083 网络流之二分图匹配

    http://acm.hdu.edu.cn/showproblem.php?pid=1083 二分图匹配用得很多 这道题只需要简化的二分匹配 #include<iostream> #inc ...

  5. hdu 5727 Necklace dfs+二分图匹配

    Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...

  6. hdu 2255 二分图最大权匹配 *

    题意:说在遥远的地方有一个非常富裕的村落,有一天,村长决定进行制度改革:重新分配房子.这可是一件大事,关系到人民的住房问题啊.村里共有n间房间,刚好有n家老百姓,考虑到每家都要有房住(如果有老百姓没房 ...

  7. HDU 1010 (DFS搜索+奇偶剪枝)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意:给定起点和终点,问刚好在t步时能否到达终点. 解题思路: 4个剪枝. ①dep&g ...

  8. HDU(1853),最小权匹配,KM

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 Cyclic Tour Time Limit: 1000/1000 MS (Java/Other ...

  9. HDU(3605),二分图多重匹配

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3605 Escape Time Limit: 4000/2000 MS (Java/Others)    ...

随机推荐

  1. 详解HTML的a标签(超链接标签)

    原文 简书原文:https://www.jianshu.com/p/d6a2499db73b 大纲 1.什么是<a>标签 2.<a>标签的几个重要属性 3.a标签的运行机制 4 ...

  2. shell脚本if条件总结

    原文链接:https://geniuspeng.github.io/2018/03/12/shell-if/ shell编程中,if-then是一种常见的控制流命令,而if的条件判断一般采用内置命令t ...

  3. Java解惑八:很多其它库之谜

    本文是依据JAVA解惑这本书,做的笔记. 电子书见:http://download.csdn.net/detail/u010378705/7527721 谜题76 将线程的启动方法start(),写成 ...

  4. 用live555将内网摄像机视频推送到外网server,附源代码

    近期非常多人问,怎样将内网的摄像机流媒体数据公布到公网,假设用公网与局域网间的port映射方式太过麻烦,一个摄像机要做一组映射,并且不是每个局域网都是有固定ip地址,即使外网主机配置好了每个摄像机的映 ...

  5. iOS开发SDWebImage的基本使用

    #import "ViewController.h" #import "UIImageView+WebCache.h" #import "SDWebI ...

  6. 【41.43%】【codeforces 560C】Gerald's Hexagon

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. eclipse jdt

    http://www.cnblogs.com/hoojo/p/use_eclipse_ant_javac_JDT_compiler_class.html

  8. js实现表格配对小游戏

    js实现表格配对小游戏 一.总结 一句话总结: 二.js实现表格配对 1.配对游戏案例说明 实例描述: 当用户点击两个相同的图案或字符后配对成功,全部配对成功后游戏获胜 案例008采用了大家常见的小游 ...

  9. Web开发标配--开发人员工具-JS调试

    喜欢从业的专注,七分学习的态度. JS:全称JavaScript,Web中,js主要在两个地方: html的<script type="text/javascript"> ...

  10. NOIP模拟 Math - 数学

    题目大意: 给定a,n(\(a \le 1e9, n\le30\)),求有多少\(b(1 \le b \le 2^n)\)满足:\(a^b \equiv b^a(mod 2^n)\). 题目分析: 数 ...