Submission #1308109
Source Code Expand
import java.io.*;
import java.util.*;
public class Main {
private static boolean debug = false;
private static boolean elapsed = false;
private static PrintWriter _out = new PrintWriter(System.out);
private static PrintWriter _err = new PrintWriter(System.err);
private Point s;
private Point t;
private Map<String, int[]> map;
private boolean done;
private void solve(Scanner sc) {
int sx = sc.nextInt();
int sy = sc.nextInt();
int tx = sc.nextInt();
int ty = sc.nextInt();
s = new Point(sx, sy);
t = new Point(tx, ty);
map = new LinkedHashMap<>();
map.put("U", new int[] { 0, 1 });
map.put("R", new int[] { 1, 0 });
map.put("D", new int[] { 0, -1 });
map.put("L", new int[] { -1, 0 });
search(0, s, t, new HashSet<>(), new StringBuilder());
}
private void search(int cnt, Point current, Point target, Set<Point> set, StringBuilder route) {
if (cnt == 4) {
_out.println(route);
done = true;
return;
}
if (done) {
return;
}
Map<String, Point> directionMap = new HashMap<>();
int minDistance = Integer.MAX_VALUE;
String minDirection = null;
for (Map.Entry<String, int[]> entry : map.entrySet()) {
int[] d = entry.getValue();
Point p = new Point(current.x + d[0], current.y + d[1]);
if (!(set.contains(p) || target.equals(t) && p.equals(s) || target.equals(s) && p.equals(t))) {
int distance = distance(p, target);
if (distance < minDistance) {
minDistance = distance;
minDirection = entry.getKey();
}
directionMap.put(entry.getKey(), p);
}
}
Point p = directionMap.get(minDirection);
if (p.equals(target)) {
if (cnt % 2 == 0) {
target = s;
} else {
target = t;
}
route.append(minDirection);
search(cnt + 1, p, target, set, route);
route.delete(route.length() - 1, route.length());
} else if (!set.contains(p)) {
set.add(p);
route.append(minDirection);
search(cnt, p, target, set, route);
set.remove(p);
route.delete(route.length() - 1, route.length());
}
}
private int distance(Point p1, Point p2) {
return (int)(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2));
}
private long C(long n, long r) {
long res = 1;
for (long i = n; i > n - r; --i) {
res *= i;
}
for (long i = r; i > 1; --i) {
res /= i;
}
return res;
}
private long P(long n, long r) {
long res = 1;
for (long i = n; i > n - r; --i) {
res *= i;
}
return res;
}
private static class Point implements Comparable<Point> {
long x;
long y;
public Point(long x, long y) {
this.x = x;
this.y = y;
}
public boolean equals(Object o) {
if (o instanceof Point) {
Point that = (Point)o;
return this.x == that.x && this.y == that.y;
}
return false;
}
public int hashCode() {
int hashCode = 17;
hashCode = hashCode * 31 + (int)(x ^ (x >>> 32));
hashCode = hashCode * 31 + (int)(y ^ (y >>> 32));
return hashCode;
}
public int compareTo(Point that) {
if (this.x != that.x) {
return this.x > that.x ? 1 : -1;
}
if (this.y != that.y) {
return this.y > that.y ? 1 : -1;
}
return 0;
}
public String toString() {
return "(" + x + ", " + y + ")";
}
}
/*
* 10^10 > Integer.MAX_VALUE = 2147483647 > 10^9
* 10^19 > Long.MAX_VALUE = 9223372036854775807L > 10^18
*/
public static void main(String[] args) {
long S = System.currentTimeMillis();
Scanner sc = new Scanner(System.in);
new Main().solve(sc);
_out.flush();
long G = System.currentTimeMillis();
if (elapsed) {
_err.println((G - S) + "ms");
}
_err.flush();
}
}
Submission Info
Submission Time |
|
Task |
C - Back and Forth |
User |
unirita135 |
Language |
Java8 (OpenJDK 1.8.0) |
Score |
300 |
Code Size |
4639 Byte |
Status |
AC |
Exec Time |
173 ms |
Memory |
35788 KB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
300 / 300 |
Status |
|
|
Set Name |
Test Cases |
Sample |
sample_01.txt, sample_02.txt |
All |
sample_01.txt, sample_02.txt, subtask_1_01.txt, subtask_1_02.txt, subtask_1_03.txt, subtask_1_04.txt, subtask_1_05.txt, subtask_1_06.txt, subtask_1_07.txt, subtask_1_08.txt, subtask_1_09.txt, subtask_1_10.txt |
Case Name |
Status |
Exec Time |
Memory |
sample_01.txt |
AC |
94 ms |
18900 KB |
sample_02.txt |
AC |
100 ms |
21972 KB |
subtask_1_01.txt |
AC |
95 ms |
20564 KB |
subtask_1_02.txt |
AC |
173 ms |
35788 KB |
subtask_1_03.txt |
AC |
124 ms |
28500 KB |
subtask_1_04.txt |
AC |
134 ms |
21460 KB |
subtask_1_05.txt |
AC |
121 ms |
23380 KB |
subtask_1_06.txt |
AC |
111 ms |
19796 KB |
subtask_1_07.txt |
AC |
103 ms |
19412 KB |
subtask_1_08.txt |
AC |
121 ms |
22220 KB |
subtask_1_09.txt |
AC |
133 ms |
25428 KB |
subtask_1_10.txt |
AC |
127 ms |
25300 KB |