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/Others)
Total Submission(s): 239    Accepted Submission(s): 110

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  

 

Recommend liuyiding  

 

 

先预处理好哪些点的组合可以构成正方形。

 

然后按照二进制,去寻找答案。

 

虽然感觉复杂度比较大,但是还是过了。

 

 

  1 /* ***********************************************
  2 Author        :kuangbin
  3 Created Time  :2013/9/15 星期日 14:08:43
  4 File Name     :2013杭州网络赛\1002.cpp
  5 ************************************************ */
  6 
  7 #pragma comment(linker, "/STACK:1024000000,1024000000")
  8 #include <stdio.h>
  9 #include <string.h>
 10 #include <iostream>
 11 #include <algorithm>
 12 #include <vector>
 13 #include <queue>
 14 #include <set>
 15 #include <map>
 16 #include <string>
 17 #include <math.h>
 18 #include <stdlib.h>
 19 #include <time.h>
 20 using namespace std;
 21 
 22 pair<int,int>p[100];
 23 int n;
 24 vector<int>vec;
 25 
 26 
 27 bool judge(pair<int,int>p1,pair<int,int>p2,pair<int,int>p3,pair<int,int>p4)
 28 {
 29     if(p3.first - p1.first != 0 && p3.second - p1.second == p3.first - p1.first)
 30     {
 31         if(p2.first == p3.first && p2.second == p1.second)
 32             if(p4.first == p1.first && p4.second == p3.second)
 33                 return true;
 34     }
 35     return false;
 36 }
 37 //判断p1p2p3p4四个点能不能形成正方形
 38 bool check(pair<int,int>p1,pair<int,int>p2,pair<int,int>p3,pair<int,int>p4)
 39 {
 40     if(judge(p1,p2,p3,p4))return true;
 41     if(judge(p1,p2,p4,p3))return true;
 42     if(judge(p1,p3,p2,p4))return true;
 43     if(judge(p1,p3,p4,p2))return true;
 44     if(judge(p1,p4,p2,p3))return true;
 45     if(judge(p1,p4,p3,p2))return true;
 46 
 47     swap(p1,p2);
 48     if(judge(p1,p2,p3,p4))return true;
 49     if(judge(p1,p2,p4,p3))return true;
 50     if(judge(p1,p3,p2,p4))return true;
 51     if(judge(p1,p3,p4,p2))return true;
 52     if(judge(p1,p4,p2,p3))return true;
 53     if(judge(p1,p4,p3,p2))return true;
 54     swap(p1,p2);
 55 
 56     swap(p1,p3);
 57     if(judge(p1,p2,p3,p4))return true;
 58     if(judge(p1,p2,p4,p3))return true;
 59     if(judge(p1,p3,p2,p4))return true;
 60     if(judge(p1,p3,p4,p2))return true;
 61     if(judge(p1,p4,p2,p3))return true;
 62     if(judge(p1,p4,p3,p2))return true;
 63     swap(p1,p3);
 64 
 65     swap(p1,p4);
 66     if(judge(p1,p2,p3,p4))return true;
 67     if(judge(p1,p2,p4,p3))return true;
 68     if(judge(p1,p3,p2,p4))return true;
 69     if(judge(p1,p3,p4,p2))return true;
 70     if(judge(p1,p4,p2,p3))return true;
 71     if(judge(p1,p4,p3,p2))return true;
 72     swap(p1,p4);
 73 
 74 
 75     return false;
 76 
 77 }
 78 
 79 int dp[1<<20];
 80 int solve(int s)
 81 {
 82     if(dp[s] != -1)return dp[s];
 83     int ans  = 0;
 84     int sz = vec.size();
 85     for(int i = 0;i < sz;i++)
 86         if((s&vec[i]) == vec[i])
 87         {
 88             ans = max(ans,1+solve(s^vec[i]));
 89         }
 90     return dp[s] = ans;
 91 }
 92 
 93 int main()
 94 {
 95     //freopen("in.txt","r",stdin);
 96     //freopen("out.txt","w",stdout);
 97     while(scanf("%d",&n) == 1)
 98     {
 99         if(n == -1)break;
100         vec.clear();
101         for(int i = 0;i < n;i++)
102             scanf("%d%d",&p[i].first,&p[i].second);
103         //找出所有可以组成正方形的组合
104         for(int i = 0;i < n;i++)
105             for(int j = i+1;j < n;j++)
106                 for(int x = j+1;x < n;x++)
107                     for(int y = x+1;y < n;y++)
108                         if(check(p[i],p[j],p[x],p[y]))
109                         {
110                             vec.push_back((1<<i)|(1<<j)|(1<<x)|(1<<y));
111                         }
112         memset(dp,-1,sizeof(dp));
113         int tot = (1<<n) -1;
114         printf("%d\n",4*solve(tot));
115     }
116     return 0;
117 }

 

 

 

 

 

 

 

 

    原文作者:算法小白
    原文地址: https://www.cnblogs.com/kuangbin/p/3323439.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞