Submission #1174479
Source Code Expand
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Queue;
import java.util.Scanner;
/**
* 日本橋ハーフマラソン 2017/03/04 B
*
* @author tsukammo
*/
public class Main {
// B
public static void main(String[] args) {
new Main().play();
}
private void play() {
Scanner in = new Scanner(System.in);
input(in);
solve();
}
private void solve() {
for (turn = 0; turn < 300; turn++) {
StringBuffer output = new StringBuffer();
init();
for (int i = 0; i < K; i++) {
String move = move(cars[i]);
output.append(move);
}
out.add(output.toString());
}
System.out.println(out.size());
for (String s : out) {
System.out.println(s);
}
}
private String move(Car car) {
int ans = -1;
if (!car.finish && !car.stop && (car.goTar || car.escape)) {
for (int i = 0; i < dx.length; i++) {
int nx = car.x + dx[i];
int ny = car.y + dy[i];
if (inField(nx, ny) && field[ny][nx] == 0) {
if (car.goTar) { // 目的値になるべく近づく
int[][] dist = calcMoveTar(car);
if (dist[car.y][car.x] > dist[ny][nx]) {
field[ny][nx] = 1;
car.x = nx;
car.y = ny;
return order[i];
}
} else if (car.escape) { // なるべく邪魔にならないところに移動
if (calcCenterDist(car.x, car.y) < calcCenterDist(nx, ny)) {
field[ny][nx] = 1;
car.x = nx;
car.y = ny;
return order[i];
}
}
if (rnd.nextInt(10) < 8) {
ans = i;
}
}
}
}
if (ans == -1) {
field[car.y][car.x] = 2; // 動く気がない
} else {
int nx = car.x + dx[ans];
int ny = car.y + dy[ans];
field[ny][nx] = 1;
car.x = nx;
car.y = ny;
return order[ans];
}
return "-";
}
private int[][] calcMoveTar(Car car) {
boolean[][] checked = new boolean[H][W];
Queue<int[]> list = new ArrayDeque<int[]>();
// 目的地から逆算する
list.add(new int[] { car.tarX, car.tarY });
int[][] dist = new int[H][W];
dist[car.tarY][car.tarX] = 1;
while (list.size() > 0) {
int[] now = list.poll();
int x = now[0];
int y = now[1];
for (int k = 0; k < dx.length; k++) {
int nx = x + dx[k];
int ny = y + dy[k];
if (inField(nx, ny) && field[ny][nx] < 2) {
if ((dist[ny][nx] == 0 || dist[ny][nx] > dist[y][x] + 1)) {
list.add(new int[] { nx, ny });
dist[ny][nx] = dist[y][x] + 1;
checked[ny][nx] = true;
}
} else if (inField(nx, ny)) {// どうやってもいけない場合できるだけ近くへ。
if ((dist[ny][nx] == 0 || dist[ny][nx] > dist[y][x] + 100)) {
list.add(new int[] { nx, ny });
dist[ny][nx] = dist[y][x] + 100;
checked[ny][nx] = true;
}
}
}
}
return dist;
}
private void init() {
field = new int[H][W];
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
field[i][j] = 0;
}
}
for (Car car : cars) {
field[car.y][car.x] = 1;
car.stop = false;
int val = Math.abs(car.tarX - W / 2) + Math.abs(car.tarY - H / 2);
if (car.x == car.tarX && car.y == car.tarY) {
car.finish = true;
} else if (checkStop(car)) {
car.stop = true;
} else if (val < turn / 20 + 1) {
car.goTar = true;
} else if (val > 30 - (turn / 5 + 3)) {
car.escape = true;
} else {
car.stop = true;
}
if (car.finish || car.stop) {
field[car.y][car.x] = 2; // 動く気がない
}
}
}
private boolean checkStop(Car car) {
for (int i = 0; i < dx.length; i++) {
int nx = car.x + dx[i];
int ny = car.y + dy[i];
if (inField(nx, ny) && field[ny][nx] == 0) {
return false;
}
}
return true;
}
private int calcCenterDist(int x, int y) {
return Math.abs(x - W / 2) + Math.abs(y - H / 2);
}
static final int[] dx = new int[] { 1, 0, -1, 0 };
static final int[] dy = new int[] { 0, 1, 0, -1 };
static final String[] order = new String[] { "R", "D", "L", "U" };
// static final char[] order = new char[] { 'D', 'R', 'U', 'L' };
private boolean inField(int ni, int nj) {
return (ni > -1 && ni < W && nj > -1 && nj < H);
}
private void input(Scanner in) {
H = in.nextInt(); // Y
W = in.nextInt(); // X
K = in.nextInt();
T = in.nextInt();
System.err.println(H + " " + W + " " + K + " " + T);
in.nextLine();
cars = new Car[K];
for (int i = 0; i < K; i++) {
String[] tmp = in.nextLine().split(" ");
cars[i] = new Car(Integer.parseInt(tmp[0]), Integer.parseInt(tmp[1]), Integer.parseInt(tmp[2]),
Integer.parseInt(tmp[3]));
}
System.err.println("input OK");
}
int H;
int W;
int K;
int T;
int turn;
int[][] field;
ArrayList<String> out = new ArrayList<>();
Car[] cars;
Random rnd = new Random(19881221L);
class Car {
int x;
int y;
int tarX;
int tarY;
boolean stop = false;
boolean goTar = false;
boolean escape = false;
boolean finish = false;
Car(int ri, int ci, int si, int di) {// 逆順
y = ri - 1;
x = ci - 1;
tarY = si - 1;
tarX = di - 1;
}
}
class Random {
private long seed;
private final long multiplier = 0x5DEECE66DL, addend = 0xBL, mask = (1L << 48) - 1;
Random(long seed) {
this.seed = seed;
}
int nextInt(int n) {
if (n <= 1)
return 0;
if ((n & -n) == n) {
return (int) ((n * (long) ((int) ((seed = (seed * multiplier + addend) & mask) >>> 17))) >> 31);
}
int bits, val;
do {
bits = (int) ((seed = (seed * multiplier + addend) & mask) >>> 17);
val = bits % n;
} while (bits - val + (n - 1) < 0);
return val;
}
double nextDouble() {
return nextInt(10000000) / 10000000.0;
}
}
}
Submission Info
Submission Time |
|
Task |
B - 日本橋大渋滞 |
User |
tsukammo |
Language |
Java8 (OpenJDK 1.8.0) |
Score |
3101 |
Code Size |
5851 Byte |
Status |
AC |
Exec Time |
1790 ms |
Memory |
154084 KB |
Judge Result
Set Name |
test_01 |
test_02 |
test_03 |
test_04 |
test_05 |
test_06 |
test_07 |
test_08 |
test_09 |
test_10 |
test_11 |
test_12 |
test_13 |
test_14 |
test_15 |
test_16 |
test_17 |
test_18 |
test_19 |
test_20 |
test_21 |
test_22 |
test_23 |
test_24 |
test_25 |
test_26 |
test_27 |
test_28 |
test_29 |
test_30 |
Score / Max Score |
104 / 50000 |
103 / 50000 |
105 / 50000 |
112 / 50000 |
115 / 50000 |
101 / 50000 |
102 / 50000 |
112 / 50000 |
101 / 50000 |
97 / 50000 |
104 / 50000 |
102 / 50000 |
102 / 50000 |
103 / 50000 |
103 / 50000 |
108 / 50000 |
97 / 50000 |
102 / 50000 |
104 / 50000 |
102 / 50000 |
108 / 50000 |
101 / 50000 |
101 / 50000 |
100 / 50000 |
97 / 50000 |
104 / 50000 |
108 / 50000 |
99 / 50000 |
104 / 50000 |
100 / 50000 |
Status |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Set Name |
Test Cases |
test_01 |
subtask_01_01.txt |
test_02 |
subtask_01_02.txt |
test_03 |
subtask_01_03.txt |
test_04 |
subtask_01_04.txt |
test_05 |
subtask_01_05.txt |
test_06 |
subtask_01_06.txt |
test_07 |
subtask_01_07.txt |
test_08 |
subtask_01_08.txt |
test_09 |
subtask_01_09.txt |
test_10 |
subtask_01_10.txt |
test_11 |
subtask_01_11.txt |
test_12 |
subtask_01_12.txt |
test_13 |
subtask_01_13.txt |
test_14 |
subtask_01_14.txt |
test_15 |
subtask_01_15.txt |
test_16 |
subtask_01_16.txt |
test_17 |
subtask_01_17.txt |
test_18 |
subtask_01_18.txt |
test_19 |
subtask_01_19.txt |
test_20 |
subtask_01_20.txt |
test_21 |
subtask_01_21.txt |
test_22 |
subtask_01_22.txt |
test_23 |
subtask_01_23.txt |
test_24 |
subtask_01_24.txt |
test_25 |
subtask_01_25.txt |
test_26 |
subtask_01_26.txt |
test_27 |
subtask_01_27.txt |
test_28 |
subtask_01_28.txt |
test_29 |
subtask_01_29.txt |
test_30 |
subtask_01_30.txt |
Case Name |
Status |
Exec Time |
Memory |
subtask_01_01.txt |
AC |
1543 ms |
152352 KB |
subtask_01_02.txt |
AC |
1190 ms |
143940 KB |
subtask_01_03.txt |
AC |
1062 ms |
92912 KB |
subtask_01_04.txt |
AC |
1456 ms |
148476 KB |
subtask_01_05.txt |
AC |
1615 ms |
153288 KB |
subtask_01_06.txt |
AC |
1256 ms |
120564 KB |
subtask_01_07.txt |
AC |
1339 ms |
148460 KB |
subtask_01_08.txt |
AC |
1439 ms |
147336 KB |
subtask_01_09.txt |
AC |
1211 ms |
150532 KB |
subtask_01_10.txt |
AC |
1406 ms |
148312 KB |
subtask_01_11.txt |
AC |
1260 ms |
150372 KB |
subtask_01_12.txt |
AC |
1455 ms |
147428 KB |
subtask_01_13.txt |
AC |
1360 ms |
149488 KB |
subtask_01_14.txt |
AC |
1421 ms |
147436 KB |
subtask_01_15.txt |
AC |
1232 ms |
143920 KB |
subtask_01_16.txt |
AC |
1418 ms |
150260 KB |
subtask_01_17.txt |
AC |
1338 ms |
148444 KB |
subtask_01_18.txt |
AC |
1235 ms |
150380 KB |
subtask_01_19.txt |
AC |
1650 ms |
153536 KB |
subtask_01_20.txt |
AC |
1426 ms |
150020 KB |
subtask_01_21.txt |
AC |
1790 ms |
154084 KB |
subtask_01_22.txt |
AC |
1230 ms |
150224 KB |
subtask_01_23.txt |
AC |
1547 ms |
148428 KB |
subtask_01_24.txt |
AC |
1745 ms |
151140 KB |
subtask_01_25.txt |
AC |
1124 ms |
112392 KB |
subtask_01_26.txt |
AC |
1502 ms |
148492 KB |
subtask_01_27.txt |
AC |
1306 ms |
149428 KB |
subtask_01_28.txt |
AC |
1392 ms |
147704 KB |
subtask_01_29.txt |
AC |
1416 ms |
149948 KB |
subtask_01_30.txt |
AC |
1017 ms |
91176 KB |