hdu 4499
来源:互联网 发布:大疆官网模拟软件p 编辑:程序博客网 时间:2024/05/02 23:25
Cannon
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 1006 Accepted Submission(s): 577
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
思路:5x5的图回溯硬暴就行了。
#include<cstdio>#include<cstring>#include<iostream>using namespace std;int n,m;int vis[10][10];int ans;void dfs(int x,int y,int num){ int dy = (y+1)%m; int dx = x+(y+1)/m; if(dx == n) { ans = max(num,ans); return ; } if(vis[dx][dy]) { dfs(dx,dy,num); return ; } bool ok = true; for(int i = 0;i < dx;i++) { if(vis[i][dy] == 2) { int num = 0; for(int j = i+1;j < dx;j++) { if(vis[j][dy])num++; } if(num == 1) ok = false; } } for(int i = 0;i < dy;i++) { if(vis[dx][i] == 2) { int num = 0; for(int j = i+1;j < dy;j++) { if(vis[dx][j])num++; } if(num == 1) ok = false; } } if(ok) { vis[dx][dy] = 2; dfs(dx,dy,num+1); vis[dx][dy] = 0; dfs(dx,dy,num); } else dfs(dx,dy,num); return ;}int main(){ int q; while(scanf("%d%d%d",&n,&m,&q)!=EOF) { ans = 0; memset(vis,0,sizeof(vis)); while(q--) { int a,b; scanf("%d%d",&a,&b); vis[a][b] = 1; } dfs(0,-1,0); printf("%d\n",ans); } return 0;}
0 0
- HDU 4499
- hdu 4499
- HDU 4499 Cannon
- HDU 4499 Cannon (状态)
- HDU 4499 Cannon(回溯)
- hdu 4499 Cannon(暴力)
- hdu 4499 dfs
- HDU 4499 Cannon
- HDU 4499 Cannon
- HDU 4499 Cannon
- hdu 4499 Cannon dfs
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- poj 3680 Intervals(离散化+费用流)
- 一个c++新手写的迷宫,新手向,原生态
- 基于深度学习的目标检测方法
- 走遍美国 —— 军队军衔、美元
- 【POJ 3984】迷宫问题(BFS)
- hdu 4499
- 原厂内核移植流程
- ja21--Java中如何实现代理机制与aop
- 正则表达式
- SG 博弈论 hdu 1760 A New Tetris Game
- <script>标签不能自闭合
- MapReduce:随机生成100个小数并求最大值
- ubuntu 14.04 安装设置ssh远程连接服务
- Day4、进制的转换、运算符(位操作符)