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的,问你最多可以放多少个炮,炮和炮之间不可以相互攻击,这块只的是只能走一步,不存在两个炮中间三个棋子的情况.. 思路: 刚开始想的是把所有的空位置都 ...
随机推荐
- 使用pycharm运行调试scrapy
摘要 Scrapy是爬虫抓取框架,Pycharm是强大的python的IDE,为了方便使用需要在PyCharm对scrapy程序进行调试 python PyCharm Scrapy scrapy指令其 ...
- jq遍历table表demo
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- LoadRunner 学习(基础一)
最近开始正式系统地学习LoadRunner11.本想在自己觉得确实学到了比较有成就感的时候再mark一下,写个博客分享.阶段性地或者在自己有所小收获的时候,做做笔记分享下也好.这次作为开篇,我想记录下 ...
- 【2008nmj】Logistic回归二元分类感知器算法.docx
给你一堆样本数据(xi,yi),并标上标签[0,1],让你建立模型(分类感知器二元),对于新给的测试数据进行分类. 要将两种数据分开,这是一个分类问题,建立数学模型,(x,y,z),z指示[0,1], ...
- DPF.Android.Native.Components.v2.8.1 for delphi xe6 使用DPFJAlertDialog遇到的问题
使用DPFJAlertDialog控件时发现DPFJAlertDialog1Click不能捕获到对话框到底按了那个按键,上网搜索后找到了解决方法: 打开DPF.Android.JAlertDialog ...
- 命令(Command)模式
命令(Command)模式:命令模式是对命令的封装.命令模式把发出命令的责任和执行命令的责任分割开,委派给不同的对象 /* * 客户(Client)角色:创建了一个具体命令(ConcreteComma ...
- .net Stream篇(四)
FileStream 目录: 如何去理解FileStream? FileStream的重要性 FileStream常用构造函数(重要) 非托管参数SafeFileHandle简单介绍 FileStre ...
- 微信运动数据抓取(PHP)
“微信运动”能够向朋友分享一个包含有运动数据的网页,网页中就有我们需要的数据.url类似于:http://hw.weixin.qq.com/steprank/step/personal?openid= ...
- 国外物联网平台(7):FogHorn
国外物联网平台(7) ——FogHorn 马智 引言: 据外媒在本月20日报道,硅谷初创公司FogHorn正在与谷歌合作以简化工业物联网应用的部署.本文对FogHorn的技术.产品.应用和生态进行了分 ...
- sqlserver中 多条数据合并成一条数据 (stuff 与 for xml path 连用)
SQL 列转行,即多行合并成一条 需求:按照分组,将多条记录内容合并成一条,效果如下: 数据库示例: CREATE TABLE [t2]([NID] [bigint] NULL,[district ...