結果
問題 | No.331 CodeRunnerでやれ |
ユーザー |
![]() |
提出日時 | 2015-12-24 01:19:48 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 608 ms / 5,000 ms |
コード長 | 2,926 bytes |
コンパイル時間 | 3,201 ms |
コンパイル使用メモリ | 79,408 KB |
実行使用メモリ | 77,684 KB |
平均クエリ数 | 211.76 |
最終ジャッジ日時 | 2024-07-16 22:32:21 |
合計ジャッジ時間 | 11,358 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 16 |
ソースコード
package no331;import java.util.ArrayDeque;import java.util.Arrays;import java.util.Queue;import java.util.Scanner;public class Main {public static final int N = 50;public static final int[] DI = {0,-1,0,1};public static final int[] DJ = {1,0,-1,0};public static final int[] DIRS = {0,1,3,2};//足立法・やばいケースがあるかもpublic static void main(String[] args) {Scanner sc = new Scanner(System.in);int[][] map = new int[N][N]; //-1:不明 0:空き 1:壁for(int i=0;i<N;i++) {Arrays.fill(map[i], -1);}int ci = 25, cj = 25, dir = 1;while(true) {String s = sc.nextLine();if (s.contains("Christmas")) {break;}int wd = Integer.parseInt(s);//情報を更新for(int i=1;i<=wd+1;i++) {int wi = ci + DI[dir] * i;int wj = cj + DJ[dir] * i;if (wi < 0 || wi >= N || wj < 0 || wj >= N) {break;}if (i <= wd) {map[wi][wj] = 0;}else{map[wi][wj] = 1;}}//距離を計算int[][] dist = new int[N][N];for(int i=0;i<N;i++) {Arrays.fill(dist[i], 1<<29);}Queue<Integer> q = new ArrayDeque<Integer>();dist[0][0] = 0;q.offer(0);LOOP: while(!q.isEmpty()) {int bb = q.poll();// System.out.println(bb);int bi = bb >> 8 & 0xff;int bj = bb & 0xff;int bd = bb >> 16;for(int k=0;k<4;k++) {int ni = bi + DI[k];int nj = bj + DJ[k];if (ni < 0 || ni >= N || nj < 0 || nj >= N || map[ni][nj] == 1 || dist[ni][nj] <= bd + 1) {continue;}dist[ni][nj] = bd + 1;if (ni == ci && nj == cj) {break LOOP;}q.offer((bd+1) << 16 | ni << 8 | nj);}}// debug(ci,cj,dir,map);//現在の情報で近づくように移動・Bは用いないint turn = 0;LOOP: for(int k=0;k<4;k++) {int dir2 = (dir + DIRS[k]) % 4;int ni = ci + DI[dir2];int nj = cj + DJ[dir2];if (dist[ni][nj] < dist[ci][cj]) {if (k == 0) {turn = 0;}else if(k == 1) {turn = -1;}else{turn = 1;}break LOOP;}}if (turn == 0) {System.out.println("F");ci += DI[dir];cj += DJ[dir];}else if(turn == -1) {System.out.println("L");dir = (dir + 1) % 4;}else{System.out.println("R");dir = (dir + 3) % 4;}}}public static void debug(int ci,int cj,int dir,int[][] map) {StringBuilder sb = new StringBuilder();for(int i=0;i<N;i++) {for(int j=0;j<N;j++) {if (i == ci && j == cj) {if (dir == 0) {sb.append('>');}else if(dir == 1) {sb.append('^');}else if(dir == 2) {sb.append('<');}else{sb.append('v');}continue;}if (map[i][j] == -1) {sb.append('.');}else if(map[i][j] == 0) {sb.append(' ');}else{sb.append('#');}}sb.append('\n');}System.out.println(sb);}}