Cannon

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 21    Accepted Submission(s): 14

Problem Description
In Chinese Chess, there is one kind of powerful chessmen called Cannon. It can move horizontally or vertically along the chess grid. At each move, it can either simply move to another empty cell in the same line without any other chessman along the route or perform an eat action. The eat action, however, is the main concern in this problem. 
An eat action, for example, Cannon A eating chessman B, requires two conditions: 
1、A and B is in either the same row or the same column in the chess grid. 
2、There is exactly one chessman between A and B. 
Here comes the problem. 
Given an N x M chess grid, with some existing chessmen on it, you need put maximum cannon pieces into the grid, satisfying that any two cannons are not able to eat each other. It is worth nothing that we only account the cannon pieces you put in the grid, and no two pieces shares the same cell.
 
Input
There are multiple test cases. 
In each test case, there are three positive integers N, M and Q (1<= N, M<=5, 0<=Q <= N x M) in the first line, indicating the row number, column number of the grid, and the number of the existing chessmen. 
In the second line, there are Q pairs of integers. Each pair of integers X, Y indicates the row index and the column index of the piece. Row indexes are numbered from 0 to N-1, and column indexes are numbered from 0 to M-1. It guarantees no pieces share the same cell.
 
Output
There is only one line for each test case, containing the maximum number of cannons.
 
Sample Input
4 4 2
1 1 1 2
5 5 8
0 0 1 0 1 1 2 0 2 3 3 1 3 2 4 0
 
Sample Output
8
9
 
Source
 
Recommend
liuyiding
 

数据范围很小,明显是搜索。

主要剪枝,就是不要和前面的冲突了、

  1. /* ***********************************************
  2. Author :kuangbin
  3. Created Time :2013/8/24 14:38:00
  4. File Name :F:\2013ACM练习\比赛练习\2013通化邀请赛\1007.cpp
  5. ************************************************ */
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <iostream>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <queue>
  13. #include <set>
  14. #include <map>
  15. #include <string>
  16. #include <math.h>
  17. #include <stdlib.h>
  18. #include <time.h>
  19. using namespace std;
  20. int n,m;
  21. int g[][];
  22. int ans ;
  23.  
  24. void dfs(int x,int y,int cnt)
  25. {
  26. if(x >= n)
  27. {
  28. ans = max(ans,cnt);
  29. return;
  30. }
  31. if(y >= m)
  32. {
  33. dfs(x+,,cnt);
  34. return;
  35. }
  36. if(g[x][y] == )
  37. {
  38. dfs(x,y+,cnt);
  39. return;
  40. }
  41. dfs(x,y+,cnt);
  42. bool flag = true;
  43. int t;
  44. for(t = x-;t >= ;t--)
  45. if(g[t][y])
  46. {
  47. break;
  48. }
  49. for(int i = t-;i >= ;i--)
  50. if(g[i][y])
  51. {
  52. if(g[i][y]==)flag = false;
  53. break;
  54. }
  55. if(!flag)return;
  56. for(t = y-;t >= ;t--)
  57. if(g[x][t])
  58. break;
  59. for(int j = t-;j >= ;j--)
  60. if(g[x][j])
  61. {
  62. if(g[x][j] == )flag = false;
  63. break;
  64. }
  65. if(!flag)return;
  66. g[x][y] = ;
  67. dfs(x,y+,cnt+);
  68. g[x][y] = ;
  69. }
  70.  
  71. int main()
  72. {
  73. //freopen("in.txt","r",stdin);
  74. //freopen("out.txt","w",stdout);
  75. int Q;
  76. int u,v;
  77. while(scanf("%d%d%d",&n,&m,&Q) == )
  78. {
  79. memset(g,,sizeof(g));
  80. while(Q--)
  81. {
  82. scanf("%d%d",&u,&v);
  83. g[u][v] = ;
  84. }
  85. ans = ;
  86. dfs(,,);
  87. printf("%d\n",ans);
  88. }
  89. return ;
  90. }

HDU 4499 Cannon (搜索)的更多相关文章

  1. HDU 4499.Cannon 搜索

    Cannon Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Subm ...

  2. hdu 4499 Cannon(暴力)

    题目链接:hdu 4499 Cannon 题目大意:给出一个n*m的棋盘,上面已经存在了k个棋子,给出棋子的位置,然后求能够在这种棋盘上放多少个炮,要求后放置上去的炮相互之间不能攻击. 解题思路:枚举 ...

  3. HDU 4499 Cannon (暴力搜索)

    题意:在n*m的方格里有t个棋子,问最多能放多少个炮且每一个炮不能互相攻击(炮吃炮) 炮吃炮:在同一行或同一列且中间有一颗棋子. #include <stdio.h> #include & ...

  4. hdu 4499 Cannon dfs

    Cannon Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4499 D ...

  5. HDU 4499 Cannon (暴力求解)

    题意:给定一个n*m个棋盘,放上一些棋子,问你最多能放几个炮(中国象棋中的炮). 析:其实很简单,因为棋盘才是5*5最大,那么直接暴力就行,可以看成一行,很水,时间很短,才62ms. 代码如下: #i ...

  6. HDU 5091---Beam Cannon(线段树+扫描线)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5091 Problem Description Recently, the γ galaxies bro ...

  7. hdu 5468(莫比乌斯+搜索)

    hdu 5468 Puzzled Elena   /*快速通道*/ Sample Input 5 1 2 1 3 2 4 2 5 6 2 3 4 5   Sample Output Case #1: ...

  8. HDU 1045 (DFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1045 题目大意:在不是X的地方放O,所有O在没有隔板情况下不能对视(横行和数列),问最多可以放多少个 ...

  9. HDU 1180 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...

随机推荐

  1. C# TimeSpan获取 年月

    public static string GetYearMonthDayString(this DateTime expires) { try { var now = DateTime.Now; Ti ...

  2. vue项目中,Iview打包到生产环境时, woff 字体引用问题

    出现这问题的原因是文件路径不对,与webpack有关,解决的办法为: 一.修改webpack.prod.conf.js module: { rules: utils.styleLoaders({ so ...

  3. wpf mvvm模式下的image绑定

    view文件 <Image Grid.Column="2" Width="48" Height="64" Stretch=" ...

  4. 关于json中转义字符/正斜杠的问题。

    1.首先有关转义字符 可以看百度百科: 先不管/是否需要转义,我们去json的官方网站去看看:http://www.json.org/ 可见有这个,那么意思是 json中 又规定建议了一下,意思是虽然 ...

  5. 在SQL中有时候我们需要查看现在正在SQL Server执行的命令

    在SQL中有时候我们需要查看现在正在SQL Server执行的命令.在分析管理器或者Microsoft SQL Server Management Studio中,我们可以在"管理-SQL  ...

  6. 关于文件格式Fuzzing测试与漏洞挖掘的学习

    最近对于文件的漏洞挖掘比较感兴趣,所以在找资料来看.顺带记录笔记,把这些笔记贴在博客中分享一下.最近打算把精力放在mp3格式的漏洞发掘上,一来这是常见的文件格式格式也比较清晰.二来这也是学长推荐的入手 ...

  7. Excel根据单元格内容设置整行颜色

    1. 选择需要设置的区域,条件格式中找到“新建规则” 2. 弹出窗口中选择“使用公式确定要设置格式的单元格”一项.填写公式如下: =IF(OR($D1="已完成",$D1=&quo ...

  8. day6 subprocess模块、logging模块

        logging模块 很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储 ...

  9. 【LOJ】#2537. 「PKUWC2018」Minimax

    题解 加法没写取模然后gg了QwQ,de了半天 思想还是比较自然的,线段树合并的维护方法我是真的很少写,然后没想到 很显然,我们有个很愉快的想法是,对于每个节点枚举它所有的叶子节点,对于一个叶子节点的 ...

  10. 安卓手机获取IP地址

    public class IpGetUtil { public static String getIPAddress(Context context) { NetworkInfo info = ((C ...