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的,问你最多可以放多少个炮,炮和炮之间不可以相互攻击,这块只的是只能走一步,不存在两个炮中间三个棋子的情况.. 思路: 刚开始想的是把所有的空位置都 ...
随机推荐
- python之yield函数
yield的英文单词意思是生产,刚接触Python的时候感到非常困惑,一直没弄明白yield的用法. 只是粗略的知道yield可以用来为一个函数返回值塞数据,比如下面的例子: def addlist( ...
- Ubuntu 12.04 LTS 中文输入法的安装 (转载)
第一步:安装语言包 进入 “System Settings” 找到 “Language Support” 那一项,点击进入 选择 “Install/Remove Languages” 找到 “Chin ...
- 为什么要引入zookeeper系统
为什么要引入zookeeper系统?这篇文章将说明几个引入zookeeper的原因,首先,先对zookeeper做一个简单的介绍. zookeeper是hadoop下的一个子项目,它是一个针对大型分布 ...
- Dynamically loading unmanaged OCX in C#
You'll have to perform a number of steps that are normally taken of automatically when you use the t ...
- UI控件的位置
1.该位置指的是本控件的中心点位于点 (100, 100)上(不包含尺寸),可以用于中心对齐在使用frame设置位置的情况下 self.view.center = CGPointMake(100, 1 ...
- Spring AOP的实现机制
AOP(Aspect Orient Programming),一般称为面向切面编程,作为面向对象的一种补充,用于处理系统中分布于各个模块的横切关注点,比如事务管理,日志,缓存等等.AOP 实现的关键在 ...
- react.js学习之路四
针对学习react.js中,我感觉最大的疑惑点就是bind(this)的绑定和指向问题了,我被这个问题弄的头昏,有时候调用组件的时候,直接显示undefined,不存在这个组件,当时的心情是崩溃的,整 ...
- vs引入资源
把资源放在程序目录中,然后点击显示所有文件,然后在资源上右键“包括在项目中”
- 我的csdn博客地址
呆雁 持续的谦虚与努力 http://blog.csdn.net/u013539183
- 3、pandas
原文出处: pandas.pydata.org 译文出处:石卓林 这是关于pandas的简短介绍,主要面向新用户.可以参阅Cookbook了解更复杂的使用方法. 链接:http://python. ...