HDU4499
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.
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.
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring> using namespace std; typedef long long LL;
const int MAXN = ;
const int INF = 0x3f3f3f3f; int n, m, dis[][] = {, , , , , -, -, };
int vis[MAXN][MAXN], ans = ; bool check(int x, int y)
{
int i, j, k;
int flag;
for(k = ;k < ;++k)
{
flag = ;
i = x + dis[k][];
j = y + dis[k][];
while(i >= && j >= && i < n && j < m)
{
if(flag && vis[i][j] == )
return false;
else if(flag && vis[i][j] == -)
break;
if(vis[i][j] && !flag)
flag++;
i += dis[k][];
j += dis[k][];
}
}
return true;
} void dfs(int x, int y, int cnt)
{
if(x >= n)
{
ans = max(ans, cnt);
// cout << endl;
// for(int i = 0;i < n;++i)
// {
// for(int j = 0;j < m;++j)
// cout << vis[i][j] << " ";
// cout << endl;
// }
return ;
}
if(y != m - )
{
dfs(x, y + , cnt);
if(vis[x][y] != - && check(x, y))
{
vis[x][y] = ;
dfs(x, y + , cnt + );
vis[x][y] = ;
}
}
else
{
dfs(x + , , cnt);
if(vis[x][y] != - && check(x, y))
{
vis[x][y] = ;
dfs(x + , , cnt + );
vis[x][y] = ;
}
}
} int main()
{
int q;
while(scanf("%d%d%d", &n, &m, &q) != EOF)
{
memset(vis, , sizeof(vis));
ans = ;
int x, y;
while(q--)
{
scanf("%d%d", &x, &y);
vis[x][y] = -;
}
dfs(, , );
printf("%d\n", ans);
}
return ;
}
HDU4499的更多相关文章
- hdu4499 Cannon (DFS+回溯)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=4499 Cannon ...
- HDU4499 Cannon DFS 回溯的应用
题意就是给你一个n*m的棋盘,然后上面已经有了 棋子.并给出这些棋子的坐标,可是这些棋子是死的就是不能动,然后让你在棋盘上面摆炮.可是炮之间不能互相吃.吃的规则我们斗懂得 炮隔山打嘛.问你最多能放几个 ...
- hdu4499 搜索
题意: 给你一个棋盘,最大是5*5的,问你最多可以放多少个炮,炮和炮之间不可以相互攻击,这块只的是只能走一步,不存在两个炮中间三个棋子的情况.. 思路: 刚开始想的是把所有的空位置都 ...
随机推荐
- 简单的Cooki案例——记录用户上次访问该网页的时间
功能: 帮助网站实现提示客户端计算机上次访问网站的时间 实现原理: 将每一个会话作为一次访问过程,将每次会话的开始时间作为每次访问网站的时间,然后将这个时间以Cookie的形式存储到客户端的计算机中, ...
- hdu 2211 杀人游戏
设f(N,K)返回最后取出的编号 那么f(n,k)进行第一次选后,剩下n-n/k个人,这剩下的人里最后被取出的编号为f(n-n/k,k)记为x 那么它在前一次队列里的编号则是(x-1)/(k-1)+x ...
- MVC异常过滤器在三种作用范围下的执行顺序
对于一般过滤器(即:除了IExceptionFilter ),当同时在Controller和Action中都设置了同一个过滤器后(例如IActionFilter),执行顺序一般是由外到里,即“全局”- ...
- 编写高质量代码改善C#程序的157个建议——建议11: 区别对待==和Equals
建议11: 区别对待==和Equals 在开始本建议之前,首先要明确概念“相等性”.CLR中将“相等性”分为两类:“值相等性”和“引用相等性”.如果用来比较的两个变量所包含的数值相等,那么将其定义为“ ...
- java8之流的基本使用(二)
概述 流(stream())是java8的一个新特性,主要的作用就是将各种类型的集合转换为流,然后的方便迭代数据用的.例如: //将List类型的集合转换为流 list.stream() 转换为流之后 ...
- 选择性的使用 serialize() 进行序列化
serialize 非常方便的帮我们创建 URL 编码文本字符串 输出的字符串格式为 a=1&b=2&c=3 直接可用于Url传参 下面介绍一下选择性的序列化某些标签的使用方法 将 ...
- golang并发练习代码笔记
golang语言的精髓就是它的并发机制,十分简单,并且极少数在语言层面实现并发机制的语言,golang被成为网络时代的c语言,golang的缔造者也有c语言的缔造者,Go语言是google 推出的一门 ...
- BZOJ2668:[CQOI2012]交换棋子(费用流)
题目描述 有一个n行m列的黑白棋盘,你每次可以交换两个相邻格子(相邻是指有公共边或公共顶点)中的棋子,最终达到目标状态.要求第i行第j列的格子只能参与mi,j次交换. 输入输出格式 输入格式: 第一行 ...
- SDUT OJ 数据结构实验之链表二:逆序建立链表
数据结构实验之链表二:逆序建立链表 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...
- 洛谷 P1503 鬼子进村
题目背景 小卡正在新家的客厅中看电视.电视里正在播放放了千八百次依旧重播的<亮剑>,剧中李云龙带领的独立团在一个县城遇到了一个鬼子小队,于是独立团与鬼子展开游击战. 题目描述 描述 县城里 ...