Cannon

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 965    Accepted Submission(s): 556

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.
 
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
 
 
题意:在n×m的棋盘上面有Q的棋子,它们之间不可以相互吃对方。现在要在棋盘上面增加棋子“炮”,问最多可以增加多少个炮使得炮之间不能相互吃对方(1<= N, M<=5, 0<=Q <= N x M)。炮a吃棋子b的规则是,a和b在一行或者一列,a和b之间有一个棋子。
 
思路:n和m很小,直接暴力BFS搜索。注意炮可以增加的规则。
 
代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m;
int ans=;
int edge[][];
int dfs(int x,int y,int cou)
{
int i,j,t,sign,ok;
/*
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
cout<<edge[i][j]<<" ";
cout<<endl;
}
cout<<endl;
*/
if(cou>ans) ans=cou;
edge[x][y]=;
/**当前行可以增加*/
for(j=y+; j<n; j++)
{
if(edge[x][j]==) continue;
sign=;
ok=;
for(t=j-; t>=; t--)
{
if(edge[x][t]!=) sign++;
if(sign==&&edge[x][t]==)
{
ok=;
break;
}
}
if(ok==)
{
sign=;
for(t=x-; t>=; t--)
{
if(edge[t][j]!=) sign++;
if(sign==&&edge[t][j]==)
{
ok=;
break;
}
}
if(ok==)
{
edge[x][j]=;
dfs(x,j,cou+);
edge[x][j]=;
}
}
}
/**当前行不能增加,加入后面的行*/
for(i=x+; i<n; i++)
{
for(j=; j<m; j++)
{
if(edge[i][j]==) continue;
sign=;
ok=;
for(t=i-; t>=; t--)
{
if(edge[t][j]!=) sign++;
if(sign==&&edge[t][j]==)
{
ok=;
break;
}
}
if(ok==)
{
edge[i][j]=;
dfs(i,j,cou+);
edge[i][j]=;
}
}
}
}
int main()
{
int i,j,q;
int x,y;
while(scanf("%d%d%d",&n,&m,&q)!=EOF)
{
memset(edge,,sizeof(edge));
while(q--)
{
scanf("%d%d",&x,&y);
edge[x][y]=;
}
ans=;
for(i=; i<n; i++)
for(j=; j<m; j++)
if(edge[i][j]==)
{
edge[i][j]=;
dfs(i,j,);
edge[i][j]=;
}
cout<<ans<<endl;
}
return ;
}

HDU 4499.Cannon 搜索的更多相关文章

  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 (暴力搜索)

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

  4. hdu 4499 Cannon dfs

    Cannon Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4499 D ...

  5. HDU 4499 Cannon (暴力求解)

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

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

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

  7. hdu 5468(莫比乌斯+搜索)

    hdu 5468 Puzzled Elena   /*快速通道*/ Sample Input 5 1 2 1 3 2 4 2 5 6 2 3 4 5   Sample Output Case #1: ...

  8. HDU 1045 (DFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1045 题目大意:在不是X的地方放O,所有O在没有隔板情况下不能对视(横行和数列),问最多可以放多少个 ...

  9. HDU 1180 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...

随机推荐

  1. 属性,类方法@classmethod

    # 属性的初识# class Person:## def __init__(self,name,hight,weight):# self.name = name# self.__hight = hig ...

  2. 杂: PYTHON上数据储存:推荐h5py

    一篇很短的小短文,主要推荐下做科学计算是大量数据的储存问题 最近在做一个CNN的项目,文件夹里有20w张图片要读入并保存到一个data文件(不然每次都读20w文件太麻烦). 折腾了一个下午,发现了一个 ...

  3. UVA133

    减少领取救济金排队的长度是一个严重的问题,The New National Green Labour RhinocerosParty (这个党派)依据如下规则.每天来领取救济金的人排成一个大圆环.任选 ...

  4. Xeon Phi 编程备忘

    ▶ 闲鱼的 Xeon Phi 3120A 配办公室的新 Xeon 服务器,记录一下环境安装过程. ● 原本尝试搭 Ubuntu 服务器,参考[https://software.intel.com/en ...

  5. 安装设置IIS5.1

    1.防止不停提示无法复制staxmem.dll: esentutl /p %windir%/security/database/secedit.sdb提示数据库损坏,是否恢复,选是,出现以下提示后退出 ...

  6. 前端-CSS-介绍及三种引入方式

    我们为什么需要CSS? 使用css的目的就是让网页具有美观一致的页面,另外一个最重要的原因是内容与格式分离 在没有CSS之前,我们想要修改HTML元素的样式需要为每个HTML元素单独定义样式属性,当H ...

  7. redis数据迁移

    redis的备份和还原,借助了第三方的工具---redis-dump,  redis中使用redis-dump导出.导入.还原数据实例 1.安装redis-dump # yum install rub ...

  8. jstl fmt

    1)导入jstl 包,加载ftm标签 首先将jstl的jar包放入类库中,使用1.2版本 其次在jsp文件中引入所需要的 标记库,对于 ftm 标签,如下: <%@ taglib prefix= ...

  9. ML_入门

    N-gram 输入法后来提醒nlp自然语言理解一个向量映射到另一个空间,为什么是向量呢?模型其实是向量,一张图片表示成向量,像素表示成rgb ,每一个维度 数的度文本变成向量 one-hot repr ...

  10. hibernate中多对多的注解配置

    hibernate多对多的注解配置中的自动生成中间表的配置: @Entity@Table(name="test_student")public class Students { @ ...