Cannon

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=4499

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

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的更多相关文章

  1. hdu 4499 Cannon(暴力)

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

  2. HDU 4499.Cannon 搜索

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

  3. HDU 4499 Cannon (搜索)

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

  4. HDU 4499 Cannon (暴力求解)

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

  5. HDU 4499 Cannon (暴力搜索)

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

  6. HDU.5692 Snacks ( DFS序 线段树维护最大值 )

    HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...

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

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

  8. hdu 5727 Necklace dfs+二分图匹配

    Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...

  9. hdu 1175 连连看 DFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1175 解题思路:从出发点开始DFS.出发点与终点中间只能通过0相连,或者直接相连,判断能否找出这样的路 ...

随机推荐

  1. oracle 查看表的相关信息

    1.查看当前用户的表 SELECT * FROM user_tables; 2.查看指定用户的表 SELECT * FROM all_tables WHERE owner = 'SYS';

  2. 使用FTP搭建YUM

    VSFTP搭建YUM源 1.安装FTP [root@FTP kel]# rpm -qa |grep vsftp vsftpd-2.2.2-6.el6_0.1.x86_64 首先需要安装的ftp软件为v ...

  3. C++模板详解

    参考:C++ 模板详解(一) 模板:对类型进行参数化的工具:通常有两种形式: 函数模板:仅参数类型不同: 类模板:   仅数据成员和成员函数类型不同. 目的:让程序员编写与类型无关的代码. 注意:模板 ...

  4. (转)QR二维码生成及原理

    二维码又称QR Code,QR全称Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的Bar Code条形码能存更多的信息,也能表示更多的数据类型:比如:字符,数字, ...

  5. django 搭建自己的博客

    原文链接:http://www.errdev.com/post/4/ 每一个爱折腾的程序员都有自己的博客,好吧,虽然我不太喜欢写博客,但是这样骚包的想法却不断涌现.博客园虽好,可以没有完全的掌控感,搭 ...

  6. dom 关键字提示

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  7. 【原】创建Hive表,分号分隔符“;”引起的异常

    [障碍再现] 在创建支持Map数据结构的Hive表时,抛出如下异常 hive> create table tab_map(name string,info map<string,strin ...

  8. HDU 1079 Calendar Game(简单博弈)

    Calendar Game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  9. 您的IP不在有效范围 ip:port为 [10.15.22.15]

  10. CodeForces 7C Line

    ax+by+c=0可以转化为ax+by=-c: 可以用扩展欧几里德算法来求ax1+by1=gcd(a,b)来求出x1,y1 此时gcd(a,b)不一定等于-c,假设-c=gcd(a,b)*z,可得z= ...