Zhuge Liang's Mines

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 236    Accepted Submission(s): 107
Problem Description
In the ancient three kingdom period, Zhuge Liang was the most famous and smartest military leader. His enemy was Shima Yi, who always looked stupid when fighting against Zhuge Liang. But it was Shima Yi who laughed to the end.

Once, Zhuge Liang sent the arrogant Ma Shu to defend Jie Ting, a very important fortress. Because Ma Shu is the son of Zhuge Liang's good friend Ma liang, even Liu Bei, the Ex. king, had warned Zhuge Liang that Ma Shu was always bragging and couldn't be used, Zhuge Liang wouldn't listen. Shima Yi defeated Ma Shu and took Jie Ting. Zhuge Liang had to kill Ma Shu and retreated. To avoid Shima Yi's chasing, Zhuge Liang put some mines on the only road. Zhuge Liang deployed the mines in a Bagua pattern which made the mines very hard to remove. If you try to remove a single mine, no matter what you do ,it will explode. Ma Shu's son betrayed Zhuge Liang , he found Shima Yi, and told Shima Yi the only way to remove the mines: If you remove four mines which form the four vertexes of a square at the same time, the removal will be success. In fact, Shima Yi was not stupid. He removed as many mines as possible. Can you figure out how many mines he removed at that time?

The mine field can be considered as a the Cartesian coordinate system. Every mine had its coordinates. To simplify the problem, please only consider the squares which are parallel to the coordinate axes.

 
Input
There are no more than 15 test cases.

In each test case:

The first line is an integer N, meaning that there are N mines( 0 < N <= 20 ).

Next N lines describes the coordinates of N mines. Each line contains two integers X and Y, meaning that there is a mine at position (X,Y). ( 0 <= X,Y <= 100)

The input ends with N = -1.

 
Output
For each test case ,print the maximum number of mines Shima Yi removed in a line.
 
Sample Input
3
1 1
0 0
2 2
8
0 0
1 0
2 0
0 1
1 1
2 1
10 1
10 0
-1
 
Sample Output
0
4
 
Source


思路:
将点按照y从小到大,x从小到大排序后,从前往后dfs就够了。

注意:
要考虑重点的情况。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define maxn 105
using namespace std; int n,m,ans;
bool mp[maxn][maxn];
int vis[maxn][maxn];
struct Node
{
int x,y;
} pp[21]; bool cmp(Node xx,Node yy)
{
if(xx.y!=yy.y) return xx.y<yy.y;
if(xx.x!=yy.x) return xx.x<yy.x;
}
void dfs(int pos,int val)
{
if(ans<val) ans=val;
if(pos>n||val+n-pos+1<=ans) return ;
int i,j,x,y,tx,ty,edge;
x=pp[pos].x;
y=pp[pos].y;
if(vis[x][y]<=0) dfs(pos+1,val);
else
{
for(i=pos+1; i<=n; i++)
{
tx=pp[i].x;
ty=pp[i].y;
if(ty>y) break ;
edge=tx-x;
if(edge==0) continue ;
if(mp[x][y+edge]&&mp[tx][y+edge])
{
if(vis[x][y]<=0||vis[tx][ty]<=0||vis[x][y+edge]<=0||vis[tx][y+edge]<=0) continue ;
vis[x][y]--,vis[tx][ty]--;
vis[x][y+edge]--,vis[tx][y+edge]--;
dfs(pos+1,val+4);
vis[x][y]++,vis[tx][ty]++;
vis[x][y+edge]++,vis[tx][y+edge]++;
}
}
dfs(pos+1,val);
}
}
int main()
{
int i,j;
while(scanf("%d",&n))
{
if(n==-1) break ;
memset(mp,0,sizeof(mp));
memset(vis,0,sizeof(vis));
for(i=1; i<=n; i++)
{
scanf("%d%d",&pp[i].x,&pp[i].y);
mp[pp[i].x][pp[i].y]=1;
vis[pp[i].x][pp[i].y]++;
}
sort(pp+1,pp+n+1,cmp);
ans=0;
dfs(1,0);
printf("%d\n",ans);
}
return 0;
}




 

hdu 4739 Zhuge Liang's Mines (简单dfs)的更多相关文章

  1. hdu 4739 Zhuge Liang's Mines 随机化

    Zhuge Liang's Mines Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.p ...

  2. HDU 4739 Zhuge Liang's Mines (2013杭州网络赛1002题)

    Zhuge Liang's Mines Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  3. hdu 4739 Zhuge Liang's Mines DFS

    http://acm.hdu.edu.cn/showproblem.php?pid=4739 题意: 给定100*100的矩阵中n(n<= 20)个点,每次只能一走能够形成正方形的四个点,正方形 ...

  4. hdu 4739 Zhuge Liang's Mines

    一个简单的搜索题,唉…… 当时脑子抽了,没做出来啊…… 代码如下: #include<iostream> #include<stdio.h> #include<algor ...

  5. HDU 4739 Zhuge Liang's Mines (状态压缩+背包DP)

    题意 给定平面直角坐标系内的N(N <= 20)个点,每四个点构成一个正方形可以消去,问最多可以消去几个点. 思路 比赛的时候暴力dfs+O(n^4)枚举写过了--无意间看到有题解用状压DP(这 ...

  6. HDOJ 4739 Zhuge Liang&#39;s Mines

    Zhuge Liang's Mines Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  7. 2013 ACM/ICPC Asia Regional Hangzhou Online hdu4739 Zhuge Liang's Mines

    Zhuge Liang's Mines Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  8. HDU 4772 Zhuge Liang&#39;s Password (简单模拟题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4772 题面: Zhuge Liang's Password Time Limit: 2000/1000 ...

  9. HDU 4048 Zhuge Liang's Stone Sentinel Maze

    Zhuge Liang's Stone Sentinel Maze Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/327 ...

随机推荐

  1. Hadoop学习之Hadoop案例分析

    一.日志数据分析1.背景1.1 ***论坛日志,数据分为两部分组成,原来是一个大文件,是56GB:以后每天生成一个文件,大约是150-200MB之间: 每行记录有5部分组成:1.访问ip:2.访问时间 ...

  2. SnappyDB—Android上的NoSQL数据库简介

    参考:http://www.open-open.com/lib/view/open1420816891937.html 参考:http://android-arsenal.com/details/1/ ...

  3. hdu 4545 魔法串 2013金山西山居创意游戏程序挑战赛——初赛(1)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4545 这题太坑了,小明的串可以任意删掉某个字符 这句话不知道大家是怎么理解的,我觉得应该是能够删除其中 ...

  4. 关于BFC

    参考  http://www.html-js.com/article/1866(很棒! 还有栗子) http://www.cnblogs.com/lhb25/p/inside-block-format ...

  5. MySQL 用户登录与操作执行

    一个用户可以不登录进Mysql 数据库,由两方面的因数决定 1.你是谁:也就是mysql 数据库中记录的用户名和密码,在SQL Server数据库,中只要求说明你是谁就可以登录了,可是mysql 不是 ...

  6. 转载:Ajax及 GET、POST 区别

    转载:Ajax及 GET.POST 区别 收获: xhr.setRequestHeader(), xhr.getResponseHeader() 可以设置和获取请求头/响应头信息; new FormD ...

  7. 【玩转Ubuntu】08. Linux报错:Syntax error: "(" unexpected解决办法

    问题: 在MAC上写了一段shell脚本,放到Ubuntu上运行总是报下面这个错误,单步调试都是对的,就是直接运行会报错. bixiaopeng@ubuntu:~/package$ sh packag ...

  8. Codeforces 123E Maze(树形DP+期望)

    [题目链接] http://codeforces.com/problemset/problem/123/E [题目大意] 给出一棵,给出从每个点出发的概率和以每个点为终点的概率,求出每次按照dfs序从 ...

  9. VS2010/MFC对话框:向导对话框的创建及显示

    向导对话框的创建及显示 本节将为大家演示如何创建向导对话框. 仍然以前面的“加法计算器”的例子为基础,在其中加入向导对话框,我们可以用它来说明加法计算器的使用方法,一步一步引导用户操作,这也是比较常见 ...

  10. aliyun opts 集锦

    <一,>,aliyun 使用数据盘(aliyun新增数据盘使用,创建vg,aliyun 镜像系统本身未使用lvm-vg-lv) 1.1直接挂载文件系统 较易,不做分析 http://hel ...