-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGCJ2018_Qual_CubicUFO.cs
286 lines (247 loc) · 8.13 KB
/
GCJ2018_Qual_CubicUFO.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
namespace Contest
{
public struct Point2D
{
private readonly double _x, _y;
public double X { get { return _x; } }
public double Y { get { return _y; } }
public Point2D(double x, double y)
{
_x = x;
_y = y;
}
public static double DistanceSquare(Point2D p1, Point2D p2)
{
return (p1.X - p2.X) * (p1.X - p2.X) + (p1.Y - p2.Y) * (p1.Y - p2.Y);
}
public static double Distance(Point2D p1, Point2D p2)
{
return Math.Sqrt(DistanceSquare(p1, p2));
}
public static Point2D MiddlePoint(Point2D p1, Point2D p2)
{
return new Point2D(0.5 * (p1.X + p2.X), 0.5 * (p1.Y + p2.Y));
}
public override string ToString()
{
return String.Format(Helper.FormatProvider, "({0}, {1})", X, Y);
}
}
public struct Vector3D
{
private double _x, _y, _z;
public double X { get { return _x; } }
public double Y { get { return _y; } }
public double Z { get { return _z; } }
public Vector3D(double x, double y, double z)
{
_x = x;
_y = y;
_z = z;
}
}
public class GCJ2018_Problem
{
private static readonly double SQRT2 = Math.Sqrt(2.0);
private double A;
private static Vector3D[] GetVectors1Plane()
{
return new Vector3D[]
{
new Vector3D(0.5, 0.0, 0.0),
new Vector3D(0.0, 0.5, 0.0),
new Vector3D(0.0, 0.0, 0.5),
};
}
private static Vector3D[] GetVectors2Planes(double area)
{
Vector3D[] vectors = GetVectors2Planes(SQRT2);
area = area - SQRT2;
double areaSQ;
double sideSQ;
if (Math.Abs(SQRT2 - area) < 0.000001)
{
area = SQRT2;
areaSQ = 2;
sideSQ = 0;
}
else
{
areaSQ = area * area;
sideSQ = 2.0 - areaSQ;
}
double hP1 = Math.Sqrt(sideSQ);
Point2D p1 = new Point2D(-0.5 * area, 0.5 * hP1);
Point2D p2 = new Point2D(0.5 * area, -0.5 * hP1);
// Dot Product is zero => p1.x * p3.x + p1.y * p3.y == 0
double p3x = p1.Y / p1.X;
double p3y = -1.0;
double p3len = Math.Sqrt(2.0 * (p3x * p3x + 1));
Point2D p3 = new Point2D(p3x / p3len, p3y / p3len);
Point2D p13mid = Point2D.MiddlePoint(p1, p3);
Point2D p23mid = Point2D.MiddlePoint(p2, p3);
return new Vector3D[]
{
new Vector3D(0.3535533905932738, 0.3535533905932738, 0.0),
new Vector3D(-0.3535533905932738, 0.3535533905932738, 0.0),
new Vector3D(0.0, 0.0, 0.5),
};
}
private static Vector3D[] GetVectors3Planes(double area)
{
double areaSQ;
double sideSQ;
if (Math.Abs(SQRT2 - area) < 0.000001)
{
area = SQRT2;
areaSQ = 2;
sideSQ = 0;
}
else
{
areaSQ = area * area;
sideSQ = 2.0 - areaSQ;
}
double hP1 = Math.Sqrt(sideSQ);
Point2D p1 = new Point2D(-0.5 * area, 0.5 * hP1);
Point2D p2 = new Point2D(0.5 * area, -0.5 * hP1);
// Dot Product is zero => p1.x * p3.x + p1.y * p3.y == 0
double p3x = p1.Y / p1.X;
double p3y = -1.0;
double p3len = Math.Sqrt(2.0 * (p3x * p3x + 1));
Point2D p3 = new Point2D(p3x / p3len, p3y / p3len);
Point2D p13mid = Point2D.MiddlePoint(p1, p3);
Point2D p23mid = Point2D.MiddlePoint(p2, p3);
return new Vector3D[]
{
new Vector3D(p13mid.X, p13mid.Y, 0.0),
new Vector3D(p23mid.X, p23mid.Y, 0.0),
new Vector3D(0.0, 0.0, 0.5),
};
}
private static double CalculateSideInRect(double hypotenuse, double otherSide)
{
if (Math.Abs(hypotenuse - otherSide) < 0.000001)
return 0.0;
double side = Math.Sqrt(hypotenuse * hypotenuse - otherSide * otherSide);
return side;
}
private double[] Rotate(double x, double y, double angle)
{
double ca = Math.Cos(angle);
double sa = Math.Sin(angle);
return new double[] { ca * x - sa * y, sa * x + ca * y };
}
private double[] SolveQuadraticEquation(double a, double b, double c)
{
double[] r = new double[]
{
(-b + Math.Sqrt(b * b - 4 * a * c)) / (2 * a),
(-b - Math.Sqrt(b * b - 4 * a * c)) / (2 * a),
};
return r;
}
protected void SolveAndWrite(int problemIndex, TextWriter writer)
{
Vector3D[] vectors = null;
if(A == 1.0)
{
vectors = GetVectors1Plane();
}
else if(A <= 1.414213f)
{
vectors = GetVectors2Planes(A);
}
else
{
}
foreach (Vector3D vector in vectors)
{
writer.WriteLine();
writer.Write(String.Format(Helper.FormatProvider, "{0} {1} {2}", vector.X, vector.Y, vector.Z));
}
}
protected void ReadInput(TextReader reader)
{
A = Helper.ParseFloat(reader.ReadLine());
}
public void Run()
{
TextReader reader = null;
TextWriter writer = null;
try
{
reader = new StreamReader("input.txt");
writer = new StreamWriter("output.txt");
//reader = Console.In;
//writer = Console.Out;
int problemCount = Helper.ParseInt(reader.ReadLine());
for (int problemIndex = 1; problemIndex <= problemCount; problemIndex++)
{
ReadInput(reader);
writer.Write("Case #{0}:", problemIndex);
SolveAndWrite(problemIndex, writer);
writer.WriteLine();
}
}
catch (Exception e)
{
String msg = "Exception: " + e;
Console.WriteLine(msg);
Debug.Fail(msg);
}
finally
{
if (reader != null)
reader.Dispose();
if (writer != null)
writer.Dispose();
}
}
}
public class Helper
{
/// <summary>
/// Run one problem at a time.
/// Just initialize the solver with the correct one.
/// </summary>
public static void Main(string[] args)
{
GCJ2018_Problem instance = new GCJ2018_Problem();
instance.Run();
}
public static int ParseInt(string value)
{
return Int32.Parse(value, FormatProvider);
}
public static double ParseDouble(string value)
{
return Double.Parse(value, FormatProvider);
}
public static float ParseFloat(string value)
{
return Single.Parse(value, FormatProvider);
}
public static int[] ParseInts(string line)
{
string[] parts = line.Split(' ');
int[] result = new int[parts.Length];
for (int i = 0; i < parts.Length; i++)
{
result[i] = ParseInt(parts[i]);
}
return result;
}
public static string ToStr(double v)
{
return v.ToString(FormatProvider);
}
public static readonly IFormatProvider FormatProvider = CultureInfo.InvariantCulture;
}
}