hdu 4739Zhuge Liang's Mines(简单dfs,需要注意重点)

Zhuge Liang‘s Mines

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1166    Accepted Submission(s): 505

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

2013 ACM/ICPC Asia Regional Hangzhou Online

题目大意:给你一些点,最多二十个,让你找用这些点弄出正方形的个数,输出个数*4。

不过需要注意的是有重点。

还是有些细节没考虑到,WA了很多,T了几次。。

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4739

AC代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;

int ans,n;
int cnt[105][105];

struct node
{
    int x;
    int y;
}nod[25];

int cmp(node p1,node p2)
{
    if(p1.x<p2.x) return 1;
    if(p1.x==p2.x&&p1.y<p2.y) return 1;
    return 0;
}

void dfs(int cur,int score)
{
    if(score>ans) ans=score;
    if(cur>=n) return;
    if(ans>=n-cur+score) return;

    int i;
    int cx,cy,px,py;
    cx=nod[cur].x,cy=nod[cur].y;

    if(cnt[cx][cy]<=0)
    {
        dfs(cur+1,score);
        return;
    }
    for(i=cur+1;i<n;i++)
    {
        px=nod[i].x,py=nod[i].y;
        if(px>cx) break;
        if(py==cy) continue;
        int len=py-cy;
        if(cnt[cx][cy]&&cnt[cx][cy+len]&&cnt[cx+len][cy]&&cnt[cx+len][cy+len])
        {
            cnt[cx][cy]--,cnt[cx][cy+len]--;
            cnt[cx+len][cy]--,cnt[cx+len][cy+len]--;
            dfs(cur+1,score+4);
            cnt[cx][cy]++,cnt[cx][cy+len]++;
            cnt[cx+len][cy]++,cnt[cx+len][cy+len]++;
        }
    }
    dfs(cur+1,score);
}

int main()
{
    int i;
    while(scanf("%d",&n))
    {
        if(n==-1) break;
        memset(cnt,0,sizeof(cnt));

        for(i=0;i<n;i++)
        {
            scanf("%d%d",&nod[i].x,&nod[i].y);
            cnt[nod[i].x][nod[i].y]++;
        }
        sort(nod,nod+n,cmp);

        ans=0;
        dfs(0,0);
        cout<<ans<<endl;
    }
    return 0;
}

hdu 4739Zhuge Liang's Mines(简单dfs,需要注意重点)

时间: 2024-12-25 11:50:53

hdu 4739Zhuge Liang's Mines(简单dfs,需要注意重点)的相关文章

hdu 1016 Prime Ring Problem (简单DFS)

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25700    Accepted Submission(s): 11453 Problem Description A ring is compose of n circles as shown in diagram. Put natural numb

HDU 2102 A计划(简单DFS)

Description 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验.魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老.年迈的国王正是心急如焚,告招天下勇士来拯救公主.不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出. 现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示.骑士们一进入时空传输机就会被转到另一层的相对位置,但如果被转到

hdu 1274 展开字符串 (简单dfs)

http://acm.hdu.edu.cn/showproblem.php?pid=1274 思路: 找到匹配的区间 之后dfs #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<cmath> using namespace std; char str[300]; void dfs(int l,int r) { int i,j,k;

hdu 4739 Zhuge Liang&#39;s Mines 随机化

Zhuge Liang's Mines Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4739 Description In the ancient three kingdom period, Zhuge Liang was the most famous and smartest military leader. His enemy was Shima Yi, who

hdu 1258 Sum It Up (dfs+路径记录)

Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3953    Accepted Submission(s): 2032 Problem Description Given a specified total t and a list of n integers, find all distinct sums usi

hdu 4763 Theme Section (简单KMP)

Theme Section Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1184    Accepted Submission(s): 621 Problem Description It's time for music! A lot of popular musicians are invited to join us in t

HDU 3910 Liang Guo Sha

Liang Guo Sha Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 527    Accepted Submission(s): 367 Problem Description Maybe you know "San Guo Sha", but I guess you didn't hear the game: &qu

HDU 1016 Prime Ring Problem --- 经典DFS

思路:第一个数填1,以后每个数判断该数和前一个数想加是否为素数,是则填,然后标记,近一步递归求解. 然后记得回溯,继续判断下一个和前一个数之和为素数的数. /* HDU 1016 Prime Ring Problem --- 经典DFS */ #include <cstdio> #include <cstring> int n; bool primer[45], visit[25]; //primer打素数表,visit标记是否访问 int a[25]; //数组a存储放置的数 /

HDU 1846 Brave Game (简单博弈)

HDU 1846 Brave Game (简单博弈) ACM 题目地址: HDU 1846 Brave Game 题意: 中文. 分析: 博弈入门. 如果n=m+1,因为最多取m个,所以先拿的人拿多少个,后拿的人能全拿走. 所以判断n%(m+1)即可. 代码: /* * Author: illuz <iilluzen[at]gmail.com> * File: 1846.cpp * Create Date: 2014-09-20 10:05:26 * Descripton: game */ #