HDU-4499 Cannon (DFS)
来源:互联网 发布:日本转区软件 编辑:程序博客网 时间:2024/04/30 23:08
Cannon
http://acm.hdu.edu.cn/showproblem.php?pid=4499
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)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
题目大意:给一个n*m的棋盘,某些点放有非炮的中国象棋棋子,求这个棋盘上最多能放多少炮,使得任意两个炮之间不能互相攻击?
很简单的搜索,但是还是WA了好久,按行枚举总出错(已经考虑每行可以放三个的情况),改成按点枚举就AC。。。
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int n,m,q,ans,cnt;int g[7][7];bool isOK(int r,int c) { cnt=1; for(int i=r;i>=0;--i) { if(g[i][c]==2) { if(cnt==2) { return false; } ++cnt; } else if(g[i][c]==1) { ++cnt; } } cnt=1; for(int j=c;j>=0;--j) { if(g[r][j]==2) { if(cnt==2) { return false; } ++cnt; } else if(g[r][j]==1) { ++cnt; } } return true;}void dfs(int r,int c,int num) { if(c==m) { ++r; c=0; } if(r==n) { if(num>ans) { ans=num; } return ; } dfs(r,c+1,num);//当前点没有炮 if(g[r][c]==0&&isOK(r,c)) { g[r][c]=2; dfs(r,c+1,num+1);//当前点有炮 g[r][c]=0; }}int main() { int r,c; while(3==scanf("%d%d%d",&n,&m,&q)) { memset(g,0,sizeof(g)); ans=0; while(q-->0) { scanf("%d%d",&r,&c); g[r][c]=1; } dfs(0,0,0); printf("%d\n",ans); }}
0 0
- hdu 4499 Cannon(DFS)
- HDU-4499 Cannon (DFS)
- hdu 4499 Cannon dfs
- HDU 4499 Cannon (DFS回溯)
- hdu 4499 Cannon 暴力dfs搜索
- HDU ACM 4499 Cannon 暴力DFS
- HDU Cannon(DFS)
- HDU 4499 Cannon (暴力搜索)
- HDU 4499 Cannon (暴力搜索,回溯)
- HDU 4499 Cannon
- HDU 4499 Cannon (状态)
- HDU 4499 Cannon(回溯)
- hdu 4499 Cannon(暴力)
- HDU 4499 Cannon
- HDU 4499 Cannon
- HDU 4499 Cannon
- hdu4499 Cannon (DFS+回溯)
- HDU 4499 Cannon 解题报告
- Linux Mint (应用软件:QT 5.X)
- java 基础数据类型与循环结构要点
- 数据包嗅探 Packet Sniffing
- 关于<T extends Comparable<? super T>>的理解
- 1042. 字符统计(20)
- HDU-4499 Cannon (DFS)
- uva 274 Calling Circles
- 调整单链表使得奇数位置的元素位于偶数位置元素之前
- isapi_redirect.exe安装失败处理
- 微信公众号第三方开发之四回调url中获取授权方的授权信息以及基本信息
- Linux rpm命令教程
- [递推递归] 集合的划分
- 增量更新——新兴的一种更新方式
- LightOJ - 1414 February 29 (日期模拟)有多少个2月29