Cannon

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

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
 
Source
 
Recommend
liuyiding
 

数据范围很小,明显是搜索。

主要剪枝,就是不要和前面的冲突了、

 /* ***********************************************
Author :kuangbin
Created Time :2013/8/24 14:38:00
File Name :F:\2013ACM练习\比赛练习\2013通化邀请赛\1007.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int n,m;
int g[][];
int 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(g[x][y] == )
{
dfs(x,y+,cnt);
return;
}
dfs(x,y+,cnt);
bool flag = true;
int t;
for(t = x-;t >= ;t--)
if(g[t][y])
{
break;
}
for(int i = t-;i >= ;i--)
if(g[i][y])
{
if(g[i][y]==)flag = false;
break;
}
if(!flag)return;
for(t = y-;t >= ;t--)
if(g[x][t])
break;
for(int j = t-;j >= ;j--)
if(g[x][j])
{
if(g[x][j] == )flag = false;
break;
}
if(!flag)return;
g[x][y] = ;
dfs(x,y+,cnt+);
g[x][y] = ;
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int Q;
int u,v;
while(scanf("%d%d%d",&n,&m,&Q) == )
{
memset(g,,sizeof(g));
while(Q--)
{
scanf("%d%d",&u,&v);
g[u][v] = ;
}
ans = ;
dfs(,,);
printf("%d\n",ans);
}
return ;
}

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

  1. HDU 4499.Cannon 搜索

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

  2. hdu 4499 Cannon(暴力)

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

  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. ArcMap2SLD添加中文支持

    首先,你可以从作者提供的链接下载ArcMap2SLD.zip(支持ArcMap10.2) 1.打开LUT_sld_mapping_file.xml文件(上传文件中已经修改)修改文件<LUT> ...

  2. Codeforces 807C - Success Rate(二分枚举)

    题目链接:http://codeforces.com/problemset/problem/807/C 题目大意:给你T组数据,每组有x,y,p,q四个数,x/y是你当前提交正确率,让你求出最少需要再 ...

  3. redis主从,哨兵(windows版)

    一.下载 由于redis官方并不支持windows操作系统,所以官网上是下不到的,需要到gitlab上下载,下载地址如下: https://github.com/MicrosoftArchive/re ...

  4. SQL 标量函数-----日期函数 day() 、month()、year()

    select day(createtime) from life_unite_product --取时间字段的天值 select month(createtime) from life_unite_p ...

  5. python 删除前3天的文件

    一.需求分析 1. 删除前3天的文件 2.如果目录为空,也一并删除掉 如果使用shell脚本,一条命令就搞定了.干啥还要用python? 1. 因为需要记录一些日志,使用shell不好实现 2. 作为 ...

  6. Linux 系统目录结构和常用指令

    一.系统目录结构 /bin 经常使用的命令 /etc 所有系统管理所需的配置文件和子目录 /home 用户主目录 /usr 应用程序目录 /usr/bin 系统用户使用的应用程序 /usr/sbin ...

  7. MySQL学习笔记:insert into select

    从一个表复制数据插入到另外一个表,目标表中任何已存在的行都不会受影响. 语法: INSERT INTO table_xxx VALUES(); INSERT INTO table_xxx SELECT ...

  8. PHP性能调优,PHP慢日志---PHP脚本执行效率性能检测之WebGrind的使用

    如何一睹webgrind这个神奇的php性能检测工具神奇呢? 废话不多说首先webgrind这个性能检测是需要xdebug来配合,因为webgrind 进行性能检测分析就是通过xdebug生成的日志文 ...

  9. 基于范围的for循环

    语法: for ( for-range-declaration : expression ) statement 注意一般用auto表达类型.不需要修改时常用引用类型 例子: // range-bas ...

  10. Ionic Js十八:滑动框

    ion-slide-box 滑动框是一个包含多页容器的组件,每页滑动或拖动切换: 效果图如下:   用法 <ion-slide-box on-slide-changed="slid ...