Submission #1175616


Source Code Expand

import java.awt.Point;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.Random;

//B問題
public class Main {
	static final long C =  1000000007;
	static final int CY = 1000000000;
	static final int INF = Integer.MAX_VALUE/2;
	StringBuilder sb;
	//セット
	class Set<F,S> {
		F f;
		S s;
		Set(F f, S s) {this.f = f;this.s = s;}
	}
	// エサ
	static class Car {
		public int r, c;
		public int tr, tc;
		public int i;
		public Car(int r, int c, int tr, int tc,int i) {
			this.r = r;
			this.c = c;
			this.tr = tr;
			this.tc = tc;
			this.i = i;
		}
		int dist () {
			return Math.abs(r -tr) + Math.abs(c- tc);
		}



	}
	int[] a,b,c;
	int MA, MB;
	static final int S = 30;
	static final int K = 450;
	static final char U = 'U';
	static final char D = 'D';
	static final char L = 'L';
	static final char R = 'R';
	static final int T = 500;
	static final char _ = '-';
	static final int[][] DIR = new int[][]{{0,-1}, {0,1},{1,0},{-1,0}};
	static final char[] DIR_C = new char[]{L, R,D,U};
	Random rand = new Random();


	//-のことは考えない
	public void calc() {
		sb = new StringBuilder();



		IO sc = new IO();

		sc.nextInt();
		sc.nextInt();
		sc.nextInt();
		sc.nextInt();


		Car[] cars = new Car[K];

		for (int i=0; i < K; i++) {
			int a = sc.nextInt()-1;
			int b = sc.nextInt()-1;
			int c = sc.nextInt()-1;
			int d = sc.nextInt()-1;
			cars[i] = new Car(a,b,c,d,i);
		}
		//ゴールが外側にあるやつを前にソート
		Arrays.sort(cars,(a,b)-> -(Math.abs(a.tr - 14) + Math.abs(a.tc - 14)) + (Math.abs(b.tr - 14) + Math.abs(b.tc - 14)));
		String ans = null;
		boolean isBef = false;
		for (int ti = 0; ti < T; ti++) {
			boolean[][] map = new boolean[S][S];
			//t=0
			for (int i=0; i < K; i++) {
				Car c = cars[i];
				map[c.r][c.c] = true;
			}
			boolean isNone = true;
			char[] mes = new char[K];
			//移動
			for (int i=0; i < K; i++) {
				Car c = cars[i];
				int[][] dmap = getDistMap(c.tr, c.tc, map);
				char ac = _;
				int min = dmap[c.r][c.c];
				int nt = -1;

				for (int t=0; t < 4; t++) {
					int dx = c.r + DIR[t][0];
					int dy = c.c + DIR[t][1];
					if (isIn(dx, dy) && dmap[dx][dy] < min) {
						min = dmap[dx][dy];
						nt = t;

					}
				}
				if (nt != -1) {
					int nr = c.r+ DIR[nt][0], nc = c.c + DIR[nt][1];
					if (!map[nr][nc]) {
						ac = DIR_C[nt];
						map[nr][nc] = true;	
						c.r = nr;
						c.c = nc;
					}
				}else if ((c.dist() != 0  || ti / 150  %2== 0) && rand.nextDouble() < 0.5){//ランダムに動かす
					int nnt = -1;
					for (int t=0; t < 4; t++) {
						int nr = c.r+ DIR[t][0], nc = c.c + DIR[t][1];
						if (isIn(nr, nc) && !map[nr][nc]) {
							if (nnt == -1|| rand.nextDouble() < 0.5) {
								nnt = t;
							}
						}
					}
					if (nnt != -1) {
						ac = DIR_C[nnt];
						int nr = c.r+ DIR[nnt][0], nc = c.c + DIR[nnt][1];
						map[nr][nc] = true;	
						c.r = nr;
						c.c = nc;
					}
				}


				//ゴールに動かす
				if (ac == _) {
					if (c.c < c.tc) {
						int nr = c.r, nc = c.c+1;
						if (!map[nr][nc]) {
							ac = R;
							map[nr][nc] = true;	
							c.r = nr;
							c.c = nc;
						}
					}else if (c.c > c.tc) {
						int nr = c.r, nc = c.c-1;
						if (!map[nr][nc]) {
							ac = L;
							map[nr][nc] = true;	
							c.r = nr;
							c.c = nc;
						}
					}
				}

				if (ac == _) {
					if (c.r < c.tr) {
						int nr = c.r+1, nc = c.c;
						if (!map[nr][nc]) {
							ac = D;
							map[nr][nc] = true;	
							c.r = nr;
							c.c = nc;
						}
					}else if (c.r > c.tr) {
						int nr = c.r-1, nc = c.c;
						if (!map[nr][nc]) {
							ac = U;
							map[nr][nc] = true;	
							c.r = nr;
							c.c = nc;
						}
					}

				}
				if (ac != _) isNone = false;
				mes[c.i] = ac;
			}

			if (isNone) {
				ans = ti + "\n" + sb;

				break;
			}
			sb.append(mes);
			sb.append("\n");
		}
		if (ans == null) {
			ans = T + "\n" + sb;

		}

		System.out.println(ans);
		/*try {
			densan.s.game.text.TextIO.writeOutside("out.txt", ans);
		} catch (IOException e) {
			e.printStackTrace();
		}*/
	}
	static boolean isIn(int x, int y) {
		return 0 <= x && x < S && 0 <= y && y < S;
	}

	public static int[][] getDistMap(int r1, int c1, boolean[][] map) {
		int[][] distMap = new int[S][S];
		for (int[] i: distMap) {
			Arrays.fill(i, INF);
		}
		//if (map[r1][c1]) return distMap;
		ArrayList<Point> list = new ArrayList<Point>();
		list.add(new Point(r1,c1));
		distMap[r1][c1] = 0;
		int index = 1;
		//System.err.println("dist");
		while (!list.isEmpty()) {
			//if (debug) Main.printMap(distMap);
			ArrayList<Point> nlist = new ArrayList<Point>();

			for (Point p: list) {
				//distMap[p.x][p.y] = index;

				for (int i=0; i < 4; i++) {
					int nr = p.x + DIR[i][0], nc = p.y + DIR[i][1];
					if (isIn(nr, nc) && !map[nr][nc]
							&&distMap[nr][nc] == INF) {
						nlist.add(new Point(nr, nc));
						distMap[nr][nc] = index;

					}
				}
			}
			//System.err.println();
			//後処理
			list = nlist;
			index++;
		}

		return distMap;
	}

	//char型のコピー
	public static char[][] copyMap(char[][] map) {
		char[][] copy = new char[map.length][];
		for (int i=0; i < map.length; i++) {
			copy[i] = Arrays.copyOf(map[i], map[i].length);
		}
		return copy;
	}

	public static void main(String[] args) {
		Main main = new Main();
		main.calc();

	}
}
class IO extends PrintWriter {
	private final InputStream in;
	private final byte[] buffer = new byte[1024];
	private int ptr = 0;
	private int buflen = 0;

	public IO() { this(System.in);}
	public IO(InputStream source) { super(System.out); this.in = source;}
	private boolean hasNextByte() {
		if (ptr < buflen) {
			return true;
		}else{
			ptr = 0;
			try {
				buflen = in.read(buffer);
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (buflen <= 0) {
				return false;
			}
		}
		return true;
	}
	private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
	private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
	private static boolean isNewLine(int c) { return c == '\n' || c == '\r';}
	public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
	public boolean hasNextLine() { while(hasNextByte() && isNewLine(buffer[ptr])) ptr++; return hasNextByte();}
	public String next() {
		if (!hasNext()) {
			throw new NoSuchElementException();
		}
		StringBuilder sb = new StringBuilder();
		int b = readByte();
		while(isPrintableChar(b)) {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	public char[] nextCharArray(int len) {
		if (!hasNext()) {
			throw new NoSuchElementException();
		}
		char[] s = new char[len];
		int i = 0;
		int b = readByte();
		while(isPrintableChar(b)) {
			if (i == len) {
				throw new InputMismatchException();
			}
			s[i++] = (char) b;
			b = readByte();
		}
		return s;
	}
	public String nextLine() {
		if (!hasNextLine()) {
			throw new NoSuchElementException();
		}
		StringBuilder sb = new StringBuilder();
		int b = readByte();
		while(!isNewLine(b)) {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	public long nextLong() {
		if (!hasNext()) {
			throw new NoSuchElementException();
		}
		long n = 0;
		boolean minus = false;
		int b = readByte();
		if (b == '-') {
			minus = true;
			b = readByte();
		}
		if (b < '0' || '9' < b) {
			throw new NumberFormatException();
		}
		while(true){
			if ('0' <= b && b <= '9') {
				n *= 10;
				n += b - '0';
			}else if(b == -1 || !isPrintableChar(b)){
				return minus ? -n : n;
			}else{
				throw new NumberFormatException();
			}
			b = readByte();
		}
	}
	public int nextInt() {
		long nl = nextLong();
		if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) {
			throw new NumberFormatException();
		}
		return (int) nl;
	}
	public char nextChar() {
		if (!hasNext()) {
			throw new NoSuchElementException();
		}
		return (char) readByte();
	}
	public double nextDouble() { return Double.parseDouble(next());}
	public int[] nextIntArray(int n) { int[] a = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a;}
	public long[] nextLongArray(int n) { long[] a = new long[n]; for(int i=0;i<n;i++) a[i] = nextLong(); return a;}
	public double[] nextDoubleArray(int n) { double[] a = new double[n]; for(int i=0;i<n;i++) a[i] = nextDouble(); return a;}
	public void nextIntArrays(int[]... a) { for(int i=0;i<a[0].length;i++) for(int j=0;j<a.length;j++) a[j][i] = nextInt();}
	public int[][] nextIntMatrix(int n,int m) { int[][] a = new int[n][]; for(int i=0;i<n;i++) a[i] = nextIntArray(m); return a;}
	public char[][] nextCharMap(int n,int m) { char[][] a = new char[n][]; for(int i=0;i<n;i++) a[i] = nextCharArray(m); return a;}
	public void close() { super.close(); try {in.close();} catch (IOException e) {}}
}

Submission Info

Submission Time
Task B - 日本橋大渋滞
User kiki33
Language Java8 (OpenJDK 1.8.0)
Score 31061
Code Size 9374 Byte
Status AC
Exec Time 1162 ms
Memory 347664 KB

Compile Error

./Main.java:51: warning: '_' used as an identifier
	static final char _ = '-';
	                  ^
  (use of '_' as an identifier might not be supported in releases after Java SE 8)
./Main.java:97: warning: '_' used as an identifier
				char ac = _;
				          ^
  (use of '_' as an identifier might not be supported in releases after Java SE 8)
./Main.java:139: warning: '_' used as an identifier
				if (ac == _) {
				          ^
  (use of '_' as an identifier might not be supported in releases after Java SE 8)
./Main.java:159: warning: '_' used as an identifier
				if (ac == _) {
				          ^
  (use of '_' as an identifier might not be supported in releases after Java SE 8)
./Main.java:179: warning: '_' used as an identifier
				if (ac != _) isNone = false;
				          ^
  (use of '_' as an identifier might not be supported in releases after Java SE 8)
5 warnings

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 1015 / 50000 1036 / 50000 1115 / 50000 1060 / 50000 1028 / 50000 792 / 50000 839 / 50000 1134 / 50000 974 / 50000 1174 / 50000 881 / 50000 999 / 50000 1210 / 50000 1071 / 50000 1085 / 50000 1401 / 50000 1102 / 50000 772 / 50000 507 / 50000 1079 / 50000 1367 / 50000 1003 / 50000 726 / 50000 700 / 50000 1148 / 50000 979 / 50000 1334 / 50000 934 / 50000 1008 / 50000 1588 / 50000
Status
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
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 1162 ms 329268 KB
subtask_01_02.txt AC 1104 ms 340584 KB
subtask_01_03.txt AC 1073 ms 310132 KB
subtask_01_04.txt AC 1079 ms 339492 KB
subtask_01_05.txt AC 1009 ms 278712 KB
subtask_01_06.txt AC 1052 ms 340408 KB
subtask_01_07.txt AC 1143 ms 346360 KB
subtask_01_08.txt AC 1055 ms 342068 KB
subtask_01_09.txt AC 1083 ms 285756 KB
subtask_01_10.txt AC 1091 ms 341312 KB
subtask_01_11.txt AC 1121 ms 339024 KB
subtask_01_12.txt AC 1123 ms 347120 KB
subtask_01_13.txt AC 1096 ms 347664 KB
subtask_01_14.txt AC 1134 ms 341548 KB
subtask_01_15.txt AC 1106 ms 330920 KB
subtask_01_16.txt AC 1044 ms 292040 KB
subtask_01_17.txt AC 1043 ms 308728 KB
subtask_01_18.txt AC 1071 ms 341896 KB
subtask_01_19.txt AC 1073 ms 340656 KB
subtask_01_20.txt AC 1076 ms 310220 KB
subtask_01_21.txt AC 1140 ms 286244 KB
subtask_01_22.txt AC 1029 ms 327764 KB
subtask_01_23.txt AC 964 ms 313512 KB
subtask_01_24.txt AC 1061 ms 328564 KB
subtask_01_25.txt AC 1014 ms 342628 KB
subtask_01_26.txt AC 1135 ms 339480 KB
subtask_01_27.txt AC 1098 ms 345524 KB
subtask_01_28.txt AC 1048 ms 340328 KB
subtask_01_29.txt AC 1034 ms 298412 KB
subtask_01_30.txt AC 1151 ms 344236 KB