hdu4499 Cannon (DFS+回溯)

时间:2022-09-26 09:04:35

转载请注明出处:http://blog.csdn.net/u012860063

题目链接:http://acm.hdu.edu.cn/showproblem.php?

pid=4499

Cannon

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)

Total Submission(s): 363    Accepted Submission(s): 214
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

/*题意:

给你一个棋盘,最大是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+回溯)的更多相关文章

  1. HDU4499 Cannon DFS 回溯的应用

    题意就是给你一个n*m的棋盘,然后上面已经有了 棋子.并给出这些棋子的坐标,可是这些棋子是死的就是不能动,然后让你在棋盘上面摆炮.可是炮之间不能互相吃.吃的规则我们斗懂得 炮隔山打嘛.问你最多能放几个 ...

  2. 素数环(dfs&plus;回溯)

    题目描述: 输入正整数n,把整数1,2...n组成一个环,使得相邻两个数和为素数.输出时从整数1开始逆时针排列并且不能重复: 例样输入: 6 例样输出: 1 4 3 2 5 6 1 6 5 2 3 4 ...

  3. NOJ 1074 Hey Judge(DFS回溯)

    Problem 1074: Hey Judge Time Limits:  1000 MS   Memory Limits:  65536 KB 64-bit interger IO format: ...

  4. HDU 1016 Prime Ring Problem(经典DFS&plus;回溯)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  5. HDU 2181 哈密顿绕行世界问题(经典DFS&plus;回溯)

    哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. HDU1016 Prime Ring Problem(DFS回溯)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  7. uva 193 Graph Coloring(图染色 dfs回溯)

    Description You are to write a program that tries to find an optimal coloring for a given graph. Col ...

  8. P1074 靶形数独 dfs回溯法

    题目描述 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向 Z 博士请教,Z 博士拿出了他最近发明的“靶 ...

  9. 剪格子---(dfs回溯)

    如图p1.jpg所示,3 x 3 的格子中填写了一些整数. 我们沿着图中的红色线剪开,得到两个部分,每个部分的数字和都是60. 本题的要求就是请你编程判定:对给定的m x n 的格子中的整数,是否可以 ...

  10. 蓝桥杯 算法提高 8皇后&&num;183&semi;改 -- DFS 回溯

      算法提高 8皇后·改   时间限制:1.0s   内存限制:256.0MB      问题描述 规则同8皇后问题,但是棋盘上每格都有一个数字,要求八皇后所在格子数字之和最大. 输入格式 一个8*8 ...

随机推荐

  1. 让IE6&sol;IE7&sol;IE8浏览器支持CSS3属性

    让IE6/IE7/IE8浏览器支持CSS3属性 一.下载 您可以狠狠地点击这里:ie-css3.htc,这个玩意儿是让IE浏览器支持CSS3表现的关键东东. 二.上面的是什么东西 首先说说.htc文件 ...

  2. redis java对象操作

    使用Jedis客户端 1. java 对象,需序列化 public class Person implements Serializable { private int id; private Str ...

  3. Echarts 地图(map)插件之 鼠标HOVER和tooltip自定义提示框

    [自行修改 "引号"] 一.鼠标HOVER时的事件: 参照官方文档解释, 可以看出这款插件有丰富的鼠标事件可供选择: 调用鼠标HOVER事件的方法很简单,只需把以下代码放到char ...

  4. json全套

    JS文件 function pager1_InitData() { //基础配置 $("#pager1").myPagination({ currPage: 1, pageCoun ...

  5. JAVA算法系列 冒泡排序

    java算法系列之排序 手写冒泡 冒泡算是最基础的一个排序算法,简单的可以理解为,每一趟都拿i与i+1进行比较,两个for循环,时间复杂度为 O(n^2),同时本例与选择排序进行了比较,选择排序又叫直 ...

  6. Android icons集合

    Android icons集合: Be aware that the style changes occur fairly regularly with each major release, so ...

  7. 使用Powershell 脚本发送邮件乱码问题?

    最近在使用Powershell 编码的时候发现一个问题,只要邮件中有中文字符的邮件执行脚本以后,我们发现收到邮件都是乱码,状况如下: 对比下Powershell脚本,我们将邮件的·ENCODING 加 ...

  8. Windows实用快捷键

    1 返回上一级目录:Backspace 2 复制当前窗口:Ctr + N 3 在当前文件目录中选定首个文件(夹):Space 4  切换窗口:Alt + Tab 5 当前浏览器Tab页倒退或前进历史页 ...

  9. python2&period;7-巡风源码阅读

    推荐个脚本示例网站:https://www.programcreek.com/python/example/404/thread.start_new_thread,里面可以搜索函数在代码中的写法,只有 ...

  10. opengl中VAO&comma;VBO&comma;IBO用法小结&lpar;zz&rpar; 【转】

    http://cowboy.1988.blog.163.com/blog/static/751057982014380251300/ opengl中VAO,VBO,IBO用法小结 这三个玩意全面取代旧 ...