結果

問題 No.367 ナイトの転身
ユーザー jp_stejp_ste
提出日時 2016-05-12 18:18:58
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,558 bytes
コンパイル時間 4,198 ms
コンパイル使用メモリ 79,016 KB
実行使用メモリ 60,520 KB
最終ジャッジ日時 2024-04-15 15:19:15
合計ジャッジ時間 10,668 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 148 ms
53,996 KB
testcase_02 WA -
testcase_03 AC 147 ms
53,676 KB
testcase_04 AC 149 ms
54,084 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 148 ms
54,196 KB
testcase_09 WA -
testcase_10 AC 440 ms
60,172 KB
testcase_11 AC 398 ms
60,520 KB
testcase_12 AC 250 ms
57,280 KB
testcase_13 AC 266 ms
57,624 KB
testcase_14 AC 290 ms
57,880 KB
testcase_15 AC 176 ms
54,360 KB
testcase_16 AC 295 ms
58,380 KB
testcase_17 AC 208 ms
56,400 KB
testcase_18 AC 213 ms
56,416 KB
testcase_19 AC 220 ms
56,856 KB
testcase_20 AC 186 ms
54,672 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 151 ms
54,052 KB
testcase_24 AC 156 ms
54,432 KB
testcase_25 AC 160 ms
54,372 KB
testcase_26 AC 158 ms
54,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayDeque;
import java.util.Scanner;

public class Main2 {
    static int h,w;
    static char list[][];
    static int dp[][];
    static int goalX, goalY;
    
    static final int NIGHT = 1;
    static final int BISHOP = 2;
    static int nDirX[] = { 1,  1, -1, -1, 2,  2, -2, -2 };
    static int nDirY[] = { 2, -2,  2, -2, 1, -1,  1, -1 };
    static int bDirX[] = { 1,  1, -1, -1 };
    static int bDirY[] = { 1, -1,  1, -1 };
    
    public static void main(String[] args) {
        try (Scanner scan = new Scanner(System.in)) {
            h = Integer.parseInt(scan.next());
            w = Integer.parseInt(scan.next());
            list = new char[h][w];
            dp = new int[h][w];
            int startX = 0, startY = 0;
            
            for(int i=0; i<h; i++) {
                String line = scan.next();
                for(int j=0; j<w; j++) {
                    char ch = line.charAt(j);
                    list[i][j] = ch;
                    if(ch == 'G') {
                        goalX = j;
                        goalY = i;
                    } else if(ch == 'S') {
                        startX = j;
                        startY = i;
                    }
                    dp[i][j] = Integer.MAX_VALUE;
                }   
            }
            solve(startX, startY);
        }
    }
    
    static void solve(int startX, int startY) {
        
        ArrayDeque<Node> q = new ArrayDeque<>();
        Node s = new Node(startX, startY, 0, NIGHT);
        q.addFirst(s);
        
        while(!q.isEmpty()) {
            
            Node now = q.pollFirst();
            
            if(now.x == goalX && now.y == goalY) {
                System.out.println(now.steps);
                break;
            }
            
            if(dp[now.y][now.x] > now.steps) {
                dp[now.y][now.x] = now.steps;
            } else {
                continue;
            }
            
            if(now.state == NIGHT) {
              
                for(int i=0; i<8; i++) {
                    int nextX = now.x + nDirX[i];
                    int nextY = now.y + nDirY[i];
                    if(nextX >= 0 && nextX <= w-1 && nextY >= 0 && nextY <= h-1 ) {
                      
                        int nextState = now.state;
                        if(list[nextY][nextX] == 'R') {
                            nextState = BISHOP;
                        }
                      
                        Node next = new Node(nextX, nextY, now.steps+1, nextState);
                        q.addLast(next);
                    }
                }
              
            } else if(now.state == BISHOP) {
              
                for(int i=0; i<4; i++) {
                    int nextX = now.x + bDirX[i];
                    int nextY = now.y + bDirY[i];
                    if(nextX >= 0 && nextX <= w-1 && nextY >= 0 && nextY <= h-1 ) {
                      
                        int nextState = now.state;
                        if(list[nextY][nextX] == 'R') {
                            nextState = NIGHT;
                        }
                      
                        Node next = new Node(nextX, nextY, now.steps+1, nextState);
                        q.addLast(next);
                    }
                }
            }
        }
    }
}

class Node {
    int x, y;
    int steps;
    int state;
    Node(int x, int y, int steps, int state) {
        this.x = x;
        this.y = y;
        this.steps = steps;;
        this.state = state;
    }
}
0