hdu4499 Cannon (DFS+回溯)
转载请注明出处:http://blog.csdn.net/u012860063
题目链接:http://acm.hdu.edu.cn/showproblem.php?
pid=4499
Cannon
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.
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.
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
8
9
给你一个棋盘,最大是5*5的,问你最多能够放多少个炮,炮和炮之间不能够相互攻击,
这块仅仅的是仅仅能走一步,不存在两个炮中间三个棋子的情况..
#include <cstdio>
#include <cstring>
int n, m, ans;
int g[7][7];
int MAX(int a, int b)
{
if(a > b)
return a;
return b;
}
void dfs(int x, int y, int cnt)
{
if(x >= n)//表示已经搜索完成
{
ans = MAX(ans,cnt);
return;
}
if(y >= m)//列出界。表示当前行已经搜索完成
{
dfs(x+1,0,cnt);//又一次从下一行的0開始
return;
}
if(g[x][y] == 1)//若当前位置已经有棋子
{
dfs(x,y+1,cnt);//则从下一个又一次開始搜索
return;
}
dfs(x,y+1,cnt);
int t, flag = 0;
for(t = x-1; t >= 0; t--)//以下的两个for是查找同一列是否存在
{ //前面已经有炮和炮架
if(g[t][y])
{
break;
}
}
for(int i = t-1; i >= 0; i--)
{
if(g[i][y])
{
if(g[i][y] == 2)
{
flag = 1;
}
break;
}
}
if(flag)
{
return;//假设存在上面说得情况,就返回上一层
}
for(t = y-1; t >= 0; t--)//以下的两个for是查找同一行是否存在
{ //前面已经有炮和炮架
if(g[x][t])
break;
}
for(int j = t-1; j >= 0 ; j--)
{
if(g[x][j])
{
if(g[x][j] == 2)
{
flag = 1;
}
break;
}
}
if(flag)
{
return;//假设存在上面说得情况,就返回上一层
}
g[x][y] = 2;//表示此处暂放一个炮
dfs(x,y+1,cnt+1);
g[x][y] = 0;//回溯
}
int main()
{
int Q, u, v, i;
while(~scanf("%d%d%d",&n,&m,&Q))
{
memset(g,0,sizeof(g));
for(i = 0; i < Q; i++)
{
scanf("%d%d",&u,&v);
g[u][v] = 1; //表示開始此处已经有棋子
}
ans = 0;
dfs(0, 0, 0);
printf("%d\n",ans);
}
return 0;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
hdu4499 Cannon (DFS+回溯)的更多相关文章
- HDU4499 Cannon DFS 回溯的应用
题意就是给你一个n*m的棋盘,然后上面已经有了 棋子.并给出这些棋子的坐标,可是这些棋子是死的就是不能动,然后让你在棋盘上面摆炮.可是炮之间不能互相吃.吃的规则我们斗懂得 炮隔山打嘛.问你最多能放几个 ...
- 素数环(dfs+回溯)
题目描述: 输入正整数n,把整数1,2...n组成一个环,使得相邻两个数和为素数.输出时从整数1开始逆时针排列并且不能重复: 例样输入: 6 例样输出: 1 4 3 2 5 6 1 6 5 2 3 4 ...
- NOJ 1074 Hey Judge(DFS回溯)
Problem 1074: Hey Judge Time Limits: 1000 MS Memory Limits: 65536 KB 64-bit interger IO format: ...
- HDU 1016 Prime Ring Problem(经典DFS+回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 2181 哈密顿绕行世界问题(经典DFS+回溯)
哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- HDU1016 Prime Ring Problem(DFS回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- uva 193 Graph Coloring(图染色 dfs回溯)
Description You are to write a program that tries to find an optimal coloring for a given graph. Col ...
- P1074 靶形数独 dfs回溯法
题目描述 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向 Z 博士请教,Z 博士拿出了他最近发明的“靶 ...
- 剪格子---(dfs回溯)
如图p1.jpg所示,3 x 3 的格子中填写了一些整数. 我们沿着图中的红色线剪开,得到两个部分,每个部分的数字和都是60. 本题的要求就是请你编程判定:对给定的m x n 的格子中的整数,是否可以 ...
- 蓝桥杯 算法提高 8皇后·改 -- DFS 回溯
算法提高 8皇后·改 时间限制:1.0s 内存限制:256.0MB 问题描述 规则同8皇后问题,但是棋盘上每格都有一个数字,要求八皇后所在格子数字之和最大. 输入格式 一个8*8 ...
随机推荐
- android2.2应用开发之IccCard(sim卡或USIM卡)
tyle="margin:20px 0px 0px; font-size:14px; line-height:26px; font-family:Arial; color:rgb(51,51 ...
- SQL参数化查询的问题
最近碰到个问题, SQL语句中的 "... like '%@strKeyword%'"这样写查不出结果, 非的写成 "... like '%" + strKey ...
- Linux查看连接数,并发数
Linux查看连接数,并发数 博客分类: 小记 linux 软连接 ln -s /home/ictfmcg/data/photo /var/jtnd/data/photo tomcat 6的Conn ...
- POJ2239_Selecting Courses(二分图最大匹配)
解题报告 http://blog.csdn.net/juncoder/article/details/38154699 题目传送门 题意: 每天有12节课.一周上7天,一门课在一周有多天上课. 求一周 ...
- Javascript语言精粹之String常用方法分析
Javascript语言精粹之String常用方法分析 1. String常用方法分析 1.1 String.prototype.slice() slice(start,end)方法复制string的 ...
- HibernateReview Day2–Hibernate体系结构
本文摘自 李刚 著 <Java EE企业应用实战> 现在我们知道了一个概念Hibernate Session,只有处于Session管理下的POJO才具有持久化操作能力.当应用程序对于处于 ...
- 正則表達式 取出img标签 保存于指定路径
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- 大页(huge pages) 三大系列 ---计算大页配置参数
使用以下shell 脚本来计算大页配置参数,确保使用脚本实例之前的数据已经开始, 如果数据库的版本号11g,确认是否使用自己主动的内存管理(AMM) +++++++++++++++++++++++++ ...
- 项目架构mvc+webapi
mvc+webapi 项目架构 首先项目是mvc5+webapi2.0+orm-dapper+ef codefirst. 1.项目框架层次结构: 这个mvc项目根据不同的业务和功能进行不同的区域划分, ...
- 比較Swift与HDFS话Ceph本质(by quqi99)
作者:张华 发表于:2014-06-21版权声明:能够随意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 (http://blog.csdn.net/quqi99 ) 作者将又 ...