結果

問題 No.331 CodeRunnerでやれ
ユーザー uwiuwi
提出日時 2015-12-19 18:19:13
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,881 ms / 5,000 ms
コード長 4,473 bytes
コンパイル時間 3,907 ms
コンパイル使用メモリ 81,636 KB
実行使用メモリ 89,220 KB
平均クエリ数 236.59
最終ジャッジ日時 2024-07-16 22:13:18
合計ジャッジ時間 25,477 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 347 ms
71,380 KB
testcase_01 AC 465 ms
78,196 KB
testcase_02 AC 793 ms
82,260 KB
testcase_03 AC 675 ms
84,024 KB
testcase_04 AC 826 ms
84,256 KB
testcase_05 AC 785 ms
82,528 KB
testcase_06 AC 1,372 ms
87,716 KB
testcase_07 AC 1,148 ms
87,656 KB
testcase_08 AC 975 ms
86,812 KB
testcase_09 AC 1,734 ms
86,812 KB
testcase_10 AC 1,402 ms
88,088 KB
testcase_11 AC 973 ms
86,460 KB
testcase_12 AC 1,273 ms
89,220 KB
testcase_13 AC 1,142 ms
87,904 KB
testcase_14 AC 1,733 ms
87,960 KB
testcase_15 AC 1,674 ms
88,620 KB
testcase_16 AC 1,881 ms
87,780 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package writer;
import java.io.PrintWriter;
import java.time.LocalDateTime;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Queue;
import java.util.Scanner;

public class FromClosestSubmission {
	static Scanner in;
	static PrintWriter out;
	static String INPUT = "";
	static int n = 115, m = 115;
	static int[] dr = { 1, 0, -1, 0 };
	static int[] dc = { 0, 1, 0, -1 };
	
	static void solve()
	{
		char[][] map = new char[n][m];
		int r = 57, c = 57;
		map[r][c] = '.';
		int dir = 0;
		
		while(true){
			System.err.printf("when:%s, r:%d c:%d dir:%d\n", LocalDateTime.now(), r, c, dir);
			int dist = read();
			if(dist == -1){
				System.err.printf("when:%s, DIST=-1\n", LocalDateTime.now());
				return;
			}
			if(dist == 20151224){
				System.err.printf("when:%s, DIST=X\n", LocalDateTime.now());
				// found the goal
				while(dist != -1){
					System.err.printf("when:%s, WHILE", LocalDateTime.now());
					out.println("F"); out.flush();
					dist = read();
					if(dist == -1){
						System.err.printf("when:%s, DIST=-1\n", LocalDateTime.now());
						return;
					}
				}
				return;
			}
			for(int i = 0, vr = r, vc = c;i <= dist;i++, vr += dr[dir], vc += dc[dir]){
				map[vr][vc] = '.';
			}
			map[r+(dist+1)*dr[dir]][c+(dist+1)*dc[dir]] = '#';
			
			int[] closest = closestUnknown(r, c, dir, map);
			if(closest == null)throw new RuntimeException("closest not found");
			if(closest[2] == 1){
				r += dr[dir]; c += dc[dir];
			}else if(closest[2] == 2){
				r -= dr[dir]; c -= dc[dir];
			}else if(closest[2] == 3){
				dir = dir+1&3;
			}else if(closest[2] == 4){
				dir = dir+3&3;
			}
			System.err.printf("when:%s, closeest=%s\n", LocalDateTime.now(), Arrays.toString(closest));
			out.println("FBLR".charAt(closest[2]-1));
			out.flush();
		}
	}
	
	static int read()
	{
		String z = in.next();//Line().trim();
		System.err.printf("when:%s, z:%d:%s\n", LocalDateTime.now(), z.length(), z);
		if(z.charAt(0) >= '0' && z.charAt(0) <= '9')return Integer.parseInt(z);
		return -1;
	}
	
	static int[] closestUnknown(int sr, int sc, int sdir, char[][] map)
	{
		int[][][] d = new int[n][m][4];
		int[][][] prev = new int[n][m][4];
		Queue<int[]> q = new ArrayDeque<>();
		int I = 99999999;
		for(int i = 0;i < n;i++){
			for(int j = 0;j < m;j++){
				for(int k = 0;k < 4;k++){
					d[i][j][k] = I;
					prev[i][j][k] = -1;
				}
			}
		}
		q.add(new int[]{sr, sc, sdir});
		d[sr][sc][sdir] = 0;
		
		while(!q.isEmpty()){
			int[] cur = q.poll();
			int r = cur[0], c = cur[1], dir = cur[2];
			if(map[r][c] == 0){
				int vr = r, vc = c, vdir = dir;
				int last = -2;
				while(prev[vr][vc][vdir] != -1){
					last = prev[vr][vc][vdir];
					if(prev[vr][vc][vdir] == 1){
						vr -= dr[vdir]; vc -= dc[vdir];
					}else if(prev[vr][vc][vdir] == 2){
						vr += dr[vdir]; vc += dc[vdir];
					}else if(prev[vr][vc][vdir] == 3){
						vdir = vdir+3&3;
					}else if(prev[vr][vc][vdir] == 4){
						vdir = vdir+1&3;
					}
				}
				return new int[]{r, c, last};
			}
			// forward
			{
				int nr = r + dr[dir], nc = c + dc[dir], ndir = dir;
				if(d[nr][nc][ndir] > d[r][c][dir] + 1 && map[nr][nc] != '#'){
					d[nr][nc][ndir] = d[r][c][dir] + 1;
					prev[nr][nc][ndir] = 1;
					q.add(new int[]{nr, nc, ndir});
				}
			}
//			// backward
//			{
//				int nr = r - dr[dir], nc = c - dc[dir], ndir = dir;
//				if(d[nr][nc][ndir] > d[r][c][dir] + 1){
//					d[nr][nc][ndir] = d[r][c][dir] + 1;
//					prev[nr][nc][ndir] = 2;
//					q.add(new int[]{nr, nc, ndir});
//				}
//			}
			// rotate left
			{
				int nr = r, nc = c, ndir = dir+1&3;
				if(d[nr][nc][ndir] > d[r][c][dir] + 1){
					d[nr][nc][ndir] = d[r][c][dir] + 1;
					prev[nr][nc][ndir] = 3;
					q.add(new int[]{nr, nc, ndir});
				}
			}
			// rotate right
			{
				int nr = r, nc = c, ndir = dir+3&3;
				if(d[nr][nc][ndir] > d[r][c][dir] + 1){
					d[nr][nc][ndir] = d[r][c][dir] + 1;
					prev[nr][nc][ndir] = 4;
					q.add(new int[]{nr, nc, ndir});
				}
			}
		}
		return null;
	}
	
	public static void main(String[] args) throws Exception
	{
		in = INPUT.isEmpty() ? new Scanner(System.in) : new Scanner(INPUT);
		out = new PrintWriter(System.out);
		
		solve();
		out.flush();
	}
	
	static int ni() { return Integer.parseInt(in.next()); }
	static long nl() { return Long.parseLong(in.next()); }
	static double nd() { return Double.parseDouble(in.next()); }
	static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); }
}
0