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的,问你最多可以放多少个炮,炮和炮之间不可以相互攻击,这块只的是只能走一步,不存在两个炮中间三个棋子的情况.. 思路: 刚开始想的是把所有的空位置都 ...
随机推荐
- Monkey进行压力测试定位问题分析
Monkey测试的log分析,我们可以通过几个关键词来判断测试是否通过. 分析log方法一(粗糙一点): 1)Monkey finished打开LOG,查看log的最下端,是否有类似以下字段:## ...
- 【Andoid学习】GridView学习
简介 一个在平面上可显示多个条目的可滚动的视图组件,该组件中的条目通过一个ListAdapter和该组件进行关联.比如实现九宫格图,用GridView是首选,也是最简单的. 属性:android:co ...
- poj2299 Ultra-QuickSort(线段树求逆序对)
Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...
- 百度图片API
转载请注明出处:http://blog.csdn.net/yuanwofei/article/details/16343743 一.通用api http://image.baidu.com/i?tn= ...
- [database] postgresql 外网访问
配置 环境 ubuntu 14.04 LTS 版本 postgresql version 9.3 修改监听地址 编辑 /etc/postgresql/9.3/main/postgresql.conf ...
- C#构造函数用法
1.实例构造函数 2.静态构造函数 3.私有构造函数 例: 创建一个类 using System; using System.Collections.Generic; using System.Lin ...
- Java高质量代码之 — 泛型与反射
在Java5后推出了泛型,使我们在编译期间操作集合或类时更加的安全,更方便代码的阅读,而让身为编译性语言的Java提供动态性的反射技术,更是在框架开发中大行其道,从而让Java活起来,下面看一下在使用 ...
- 在成员函数中调用虚函数(关于多态的注意事项)------新标准c++程序设计
类的成员函数之间可以互相调用.在成员函数(静态成员函数.构造函数和析构函数除外)中调用其他虚成员函数的语句是多态的.例如: #include<iostream> using namespa ...
- 20165219 2017-2018-2《Java程序设计》结对编程一 第一周总结
20165219 2017-2018-2<Java程序设计>结对编程一 第一周总结 结对对象 20165219王彦博 20165232何彦达 需求分析 实现一个程序,要求: 1 支持整数运 ...
- Windows系统如何安装Redis?
转自 http://blog.csdn.net/lamp_yang_3533/article/details/52024744 一.Redis的下载地址 Redis官方并没有提供Redis的win ...