hdu 4499 Cannon dfs
Cannon
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=4499
Description
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
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
HINT
题意
在一个象棋棋盘上放炮,要求两个炮不能互相打到,然后问你最多能放几个炮
题解:
直接dfs就好了,范围很小
代码:
//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 2000001
#define mod 10007
#define eps 1e-9
int Num;
char CH[];
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void P(int x)
{
Num=;if(!x){putchar('');puts("");return;}
while(x>)CH[++Num]=x%,x/=;
while(Num)putchar(CH[Num--]+);
puts("");
}
//************************************************************************************** int a[],b[],visit[][],n,m,q,ans;
void dfs(int x,int y,int cnt)//一行一行地搜索,直到找到最后一行时结束时记录最大值
{
if(x>=n){
ans=max(ans,cnt);
return;
}
if(y>=m){
dfs(x+,,cnt);
return;
}
if(visit[x][y]){
dfs(x,y+,cnt);
return;
}
dfs(x,y+,cnt);
int t,flag=;
for(t=y-;t>=;t--)
if(visit[x][t]) break;
for(int i=t-;i>=;i--)
{
if(visit[x][i]==) {flag=;break;}
if(visit[x][i]) break;
}
if(flag)return;//判断这一列上是否存在炮互吃
for(t=x-;t>=;t--)
if(visit[t][y]) break;
for(int i=t-;i>=;i--){
if(visit[i][y]==) {flag=;break;}
if(visit[i][y]) break;
}
if(flag) return;//判断这一行上是否存在炮互吃
visit[x][y]=;
dfs(x,y+,cnt+);
visit[x][y]=;//回溯
}
int main()
{
while(scanf("%d%d%d",&n,&m,&q)!=EOF){
memset(visit,,sizeof(visit));
for(int i=;i<q;i++){
scanf("%d%d",&a[i],&b[i]);
visit[a[i]][b[i]]=;
}
ans=;
dfs(,,);
printf("%d\n",ans);
}
return ;
}
hdu 4499 Cannon dfs的更多相关文章
- hdu 4499 Cannon(暴力)
题目链接:hdu 4499 Cannon 题目大意:给出一个n*m的棋盘,上面已经存在了k个棋子,给出棋子的位置,然后求能够在这种棋盘上放多少个炮,要求后放置上去的炮相互之间不能攻击. 解题思路:枚举 ...
- HDU 4499.Cannon 搜索
Cannon Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Subm ...
- HDU 4499 Cannon (搜索)
Cannon Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Subm ...
- HDU 4499 Cannon (暴力求解)
题意:给定一个n*m个棋盘,放上一些棋子,问你最多能放几个炮(中国象棋中的炮). 析:其实很简单,因为棋盘才是5*5最大,那么直接暴力就行,可以看成一行,很水,时间很短,才62ms. 代码如下: #i ...
- HDU 4499 Cannon (暴力搜索)
题意:在n*m的方格里有t个棋子,问最多能放多少个炮且每一个炮不能互相攻击(炮吃炮) 炮吃炮:在同一行或同一列且中间有一颗棋子. #include <stdio.h> #include & ...
- HDU.5692 Snacks ( DFS序 线段树维护最大值 )
HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...
- HDU 5091---Beam Cannon(线段树+扫描线)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5091 Problem Description Recently, the γ galaxies bro ...
- hdu 5727 Necklace dfs+二分图匹配
Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...
- hdu 1175 连连看 DFS
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1175 解题思路:从出发点开始DFS.出发点与终点中间只能通过0相连,或者直接相连,判断能否找出这样的路 ...
随机推荐
- Linux基本命令(6)线上查询的命令
线上查询的命令 命令 功能 man 查询和解释一个命令的使用方法,以及这个命令的说明事项 locate 定位文件和目录 whatis 寻找某个命令的含义 6.1 man命令 man命令用来查询和解释一 ...
- winform form
WinForm:Windows Form,.Net中用来开发Windows窗口程序的技术,无论是之前学的控制台程序,还是后面要学的asp.net都是调用.net框架,因此所有知识点都是一样的.新建一个 ...
- nohub命令
http://jingyan.baidu.com/article/335530daa4707f19cb41c3ef.html
- motan源码解读:注册中心zookeeper(2)
上文大概讲解了利用zookeeper如何实现注册中心的.本文主要是从源码角度说明下.代码都在模块motan-registry-zookeeper中,其实在在这个模块中就3个类. ZkNodeType: ...
- PHP中mysql_affected_rows()和mysql_num_rows()区别
mysql_affected_rows -- 取得前一次 MySQL 操作所影响的记录行数mysql_num_rows -- 函数返回结果集中行的数目. config.php <?php hea ...
- Hive QL
转自http://www.alidata.org/archives/581 Hive 的官方文档中对查询语言有了很详细的描述,请参考:http://wiki.apache.org/hadoop/Hiv ...
- 10个优质PSD文件资源下载
很多设计需求并不一定要从头开始设计,你完全可以通过已有的灵感或素材开始设计任务.这个时候你可能需要一些PSD资源作为参考.今天我整理了一些常用的PSD资源供需要的朋友免费下载使用. Web & ...
- ninject学习笔记一:IOC的实现
这篇文章主要介绍ninject在IOC方面的实现,至于IOC的含义,网络资源很丰富,我这儿就不再赘述了.官方的文档其实挺好的,只是本人英语很烂,看起来比较费劲,下面这些东西是看官方的代码推敲的,我觉得 ...
- Flex通信-与Java实现Socket通信实例
Flex通信-与Java实现Socket通信实例 转自:http://blessht.iteye.com/blog/1136888 博客分类: Flex 环境准备 [服务器端] JDK1.6,“ja ...
- VS2012打包部署Winform程序
打包前的准备工作: 新建一个打包部署项目,点OK,如果是第一次使用的话,会打开一个网页,按照提示的步骤来做, 点击上面的step2的网址,进入到另一个网页: 填写完右边的信息,点击“download ...