110,499
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
namespace Problem51Nod
{
class Program
{
class Item { public double X, Y; }
static void Main(string[] args)
{
//4为行数,应该可以算到6,再大有些慢
Init(4);
int max = 0;
List<int> result = new List<int>();
int upper = 1 << Items.Length;
for (int i = 0; i < upper; i++)
{
int count = Count(i);
if (Check(i) && count >= max)
{
if (count > max)
{
max = count;
result.Clear();
}
result.Add(i);
}
}
foreach (var item in result)
{
for(int i = 0; i < Items.Length; i++)
{
int mask = 1 << i;
if ((item & mask) != mask)
Console.Write("{0} ", i + 1);
}
Console.WriteLine();
}
Console.ReadKey();
}
static Item[] Items;
static int[] CheckInt;
static int Count(int n)
{
int count = 0;
while (n > 0)
{
n &= n - 1;
count++;
}
return count;
}
static void Init(int n)
{
Items = new Item[n * (n + 1) / 2];
int index = 0;
double hight = Math.Sqrt(3) / 2;
for (int i = 0; i < n; i++)
{
for (int j = 0; j <= i; j++)
{
Items[index] = new Item { X = (double)j - (double)i / 2, Y = i * hight };
index++;
}
}
List<int> checks = new List<int>();
for (int i = 0; i < Items.Length - 2; i++)
for (int j = i + 1; j < Items.Length - 1; j++)
for (int k = j + 1; k < Items.Length; k++)
if (Check(Items[i], Items[j], Items[k]))
checks.Add((1 << i) + (1 << j) + (1 << k));
CheckInt = checks.ToArray();
}
static bool Check(Item a, Item b, Item c)
{
double ab = Distance(a, b);
double bc = Distance(c, b);
double ac = Distance(a, c);
double min = ab;
double max = ab;
min = Math.Min(bc, min);
min = Math.Min(ac, min);
max = Math.Max(bc, max);
max = Math.Max(ac, max);
return max - min < 0.00000001;
}
static double Distance(Item a, Item b) { return Math.Sqrt((a.X - b.X) * (a.X - b.X) + (a.Y - b.Y) * (a.Y - b.Y)); }
static bool Check(int n)
{
foreach (var item in CheckInt)
if ((n & item) == item)
return false;
return true;
}
}
}