HDU 4499 Cannon
来源:互联网 发布:tcpip网络层协议有哪些 编辑:程序博客网 时间:2024/05/17 21:49
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4499
Cannon
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 518 Accepted Submission(s): 300
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.
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.
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
2013 ACM-ICPC吉林通化全国邀请赛——题目重现
此题乃是1045 Fire Net的 升级版估计命题人也是根据这道题改编的吧。
/*题意:
给你一个棋盘,最大是5*5的,问你最多可以放多少个炮,炮和炮之间不可以相互攻击,
这块只的是只能走一步,不存在两个炮中间三个棋子的情况..
给你一个棋盘,最大是5*5的,问你最多可以放多少个炮,炮和炮之间不可以相互攻击,
这块只的是只能走一步,不存在两个炮中间三个棋子的情况..
*/
做过1045就很容易解决这道题了,就是在判断合法性的时候稍微写写就OK了下面是AC的代码
#include<iostream>#include<cstring>using namespace std;const int maxn=15;int Map[maxn][maxn],n,m,q,ans;bool judge(int x,int y){ int i,j; for(i=x-1;i>=0;i--) if(Map[i][y]!=0) break; for(j=i-1;j>=0;j--) if(Map[j][y]==2) return 0; else if(Map[j][y]==1) break; for(i=y-1;i>=0;i--) if(Map[x][i]!=0) break; for(j=i-1;j>=0;j--) if(Map[x][j]==2) return 0; else if(Map[x][j]==1) break; return 1;}void dfs(int d,int cnt){ if(d==n*m) { ans=max(ans,cnt); return ; } int x=d/m,y=d%m; if(judge(x,y)&&Map[x][y]==0) { Map[x][y]=2; dfs(d+1,cnt+1); Map[x][y]=0; } dfs(d+1,cnt);}int main(){ while(cin>>n>>m>>q) { memset(Map,0,sizeof(Map)); while(q--) { int x,y; cin>>x>>y; Map[x][y]=1; } ans=0; dfs(0,0); cout<<ans<<endl; } return 0;}
0 0
- HDU 4499 Cannon
- HDU 4499 Cannon (状态)
- HDU 4499 Cannon(回溯)
- hdu 4499 Cannon(暴力)
- HDU 4499 Cannon
- HDU 4499 Cannon
- HDU 4499 Cannon
- hdu 4499 Cannon dfs
- HDU 4499 Cannon 解题报告
- hdu 4499 Cannon(DFS)
- HDU-4499 Cannon (DFS)
- HDU 4499 Cannon (暴力搜索)
- HDU 4499 Cannon (暴力搜索,回溯)
- hdu 4499 Cannon 暴力dfs搜索
- HDU ACM 4499 Cannon 暴力DFS
- HDU 4499 Cannon (DFS回溯)
- HDU Cannon(DFS)
- HDU 5091 Beam Cannon
- LeetCode: Find Peak Element
- (4.1.9)gallery
- 知乎:如何评价2010年柴静采访丁仲礼的视频?
- CentOS 6.4下编译安装MySQL 5.6
- app 手机网页一些小知识
- HDU 4499 Cannon
- 操作系统之内存管理2
- LeetCode Maximum Subarray
- viewpager和fragment结合实现碎片切换,改变字体颜色
- SDN你必须知道的十大问题——值得关注的SDN博客
- java中读取和写入property文件的方法
- ORACLE之VBO-5530无法删除用户的解决办法
- java 匿名内部类
- 运行aireplay-ng的时候可能会有fixed channel mon0的问题