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 ...
随机推荐
- MongoDB 基础命令——数据库表的增删改查——遍历操作表中的记录
分组排序查询最大记录 //对 "catagory" 不等于 null 的数据进行分组查询,且查询结果倒序 db.getCollection('userAccount').aggre ...
- wampserver图标黄色
wampserver图标黄色(多个httpd.exe服务,以前装了apache) 服务--->httpd.exe右击这个服务打开文件位置就知道是不是wampserver的服务.如果不是就停掉这给 ...
- linux--关于shell的介绍
下面是最近学习shell的一些知识点总结***博客园-邦邦酱好*** 1.什么是shell(1)Shell将我们输入的指令与Kernel沟通,好让Kernel可以控刢硬件来正确无误地工作.(2)我们总 ...
- CSharp Oracle 登陆
=======后台Oracle存储过程================ 1.创建表 --判读表存在先删除begin EXECUTE IMMEDIATE 'DROP TABLE student'; ...
- 浅谈TCP优化(转)
很多人常常对TCP优化有一种雾里看花的感觉,实际上只要理解了TCP的运行方式就能掀开它的神秘面纱.Ilya Grigorik 在「High Performance Browser Networking ...
- Allegro绘制PCB流程
单位换算 1mil = 0.0254 mm 1mm = 39.3701 mil 默认情况下我们更倾向于使用mil单位绘制PCB板. 1 新建工程,File --> New... --> [ ...
- 实现Android ListView 自动加载更多内容
研究了几个小时终于实现了Android ListView 自动加载的效果. 说说我是怎样实现的.分享给大家. 1.给ListView增加一个FooterView,调用addFooterView(foo ...
- android平台TextView使用ImageSpan画廊GIF图像
android-gif-drawable(https://github.com/koral--/android-gif-drawable/releases)开源项目---是一个蛮不错的android ...
- Oracle学习(十四):管理用户安全性
--用户(user) SQL> --创建一个名为 grace password是password 的用户,新用户没有不论什么权限 SQL> create user grace identi ...
- Spring常见问题解决办法汇总
解决The prefix 'context' for element 'context:component-scan' is not bound<beans xmlns="http:/ ...