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的,问你最多可以放多少个炮,炮和炮之间不可以相互攻击,这块只的是只能走一步,不存在两个炮中间三个棋子的情况.. 思路: 刚开始想的是把所有的空位置都 ...
随机推荐
- 9.hive聚合函数,高级聚合,采样数据
本文主要使用实例对Hive内建的一些聚合函数.分析函数以及采样函数进行比较详细的讲解. 一.基本聚合函数 数据聚合是按照特定条件将数据整合并表达出来,以总结出更多的组信息.Hive包含内建的一些基本聚 ...
- tensorflow 中 feed的用法
Feed 上述示例在计算图中引入了 tensor, 以常量或变量的形式存储. TensorFlow 还提供了 feed 机制, 该机制 可以临时替代图中的任意操作中的 tensor 可以对图中任何操作 ...
- vue.js的生命周期 及其created和mounted的部分
网上很多人有所总结,转载自: https://segmentfault.com/a/1190000008570622 关于created和mounted的部分,也可以参考: https://blo ...
- Cookie存中文乱码的问题
有个奇怪的问题:登录页面中使用Cookie存值,Cookie中要存中文汉字.代码在本地调试,一切OK,汉字也能顺利存到Cookie和从Cookie中读出,但是放到服务器上不管用了,好好的汉字成了乱码, ...
- c# 二分查找法(2分钟算法)
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- MVC值提供组件ValueProvider的继承关系
MVC请求过程中中各组件调用顺序:值提供组件(IValueProvider)->模型绑定组件(IModelBinder)->模型验证组件 值提供组件接口 public interface ...
- 前端mock利器:randomjson
randomjson的应用场景 前后端分离时,前端根据后端提供的数据模型模拟后端请求.如果数据写死,每次返回的都一样,这个时候randomjson就能派上用场了.在前端规定数据类型,每次用random ...
- MongoDB整理笔记のReplica Sets
MongoDB支持在多个机器中通过异步复制达到故障转移和实现冗余.多机器中同一时刻只有一台机器是用于写操作,正因为如此,MongoDB提供了数据一致性的保障.而担当primary角色的机器,可以把读的 ...
- MongoDB整理笔记の指定命令和指定文件
MongoDB shell 不仅仅是一个交互式的shell,它也支持执行指定javascript 文件,也支持执行指定的命令片断.有了这个特性,就可以将MongoDB 与linux shell 完美结 ...
- 国外物联网平台(5):Exosite Murano
国外物联网平台(5)——Exosite Murano 马智 定位 Murano是一个基于云的IoT软件平台,提供安全.可扩展的基础设施,支持端到端的生态系统,帮助客户安全.可扩展地开发.部署和管理应用 ...