結果

問題 No.367 ナイトの転身
ユーザー 37zigen37zigen
提出日時 2016-04-29 23:47:55
言語 Java21
(openjdk 21)
結果
AC  
実行時間 414 ms / 2,000 ms
コード長 1,904 bytes
コンパイル時間 2,257 ms
コンパイル使用メモリ 78,408 KB
実行使用メモリ 59,440 KB
最終ジャッジ日時 2024-04-15 06:10:33
合計ジャッジ時間 7,087 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 121 ms
54,036 KB
testcase_01 AC 119 ms
54,020 KB
testcase_02 AC 119 ms
54,112 KB
testcase_03 AC 118 ms
53,928 KB
testcase_04 AC 108 ms
52,956 KB
testcase_05 AC 120 ms
54,188 KB
testcase_06 AC 148 ms
53,928 KB
testcase_07 AC 117 ms
54,108 KB
testcase_08 AC 117 ms
54,092 KB
testcase_09 AC 119 ms
54,036 KB
testcase_10 AC 324 ms
58,848 KB
testcase_11 AC 414 ms
59,440 KB
testcase_12 AC 204 ms
56,560 KB
testcase_13 AC 211 ms
56,828 KB
testcase_14 AC 240 ms
57,940 KB
testcase_15 AC 139 ms
54,304 KB
testcase_16 AC 248 ms
57,892 KB
testcase_17 AC 164 ms
54,980 KB
testcase_18 AC 164 ms
54,336 KB
testcase_19 AC 181 ms
54,252 KB
testcase_20 AC 145 ms
54,268 KB
testcase_21 AC 173 ms
54,396 KB
testcase_22 AC 110 ms
52,928 KB
testcase_23 AC 114 ms
52,936 KB
testcase_24 AC 128 ms
54,180 KB
testcase_25 AC 115 ms
53,240 KB
testcase_26 AC 126 ms
54,180 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;
import java.util.ArrayDeque;
import java.util.Scanner;
public class Main{
	public static void main(String[] args)throws Exception{
		new Main().solve();
	}
	void solve(){
		Scanner sc=new Scanner(System.in);
		int h=sc.nextInt();
		int w=sc.nextInt();
		int sx=-1,sy=-1;
		char[][] table=new char[h][w];
		for(int i=0;i<h;i++){
			table[i]=sc.next().toCharArray();
			for(int j=0;j<w;j++){
				char c=table[i][j];
				if(c=='S'){
					sx=j;
					sy=i;
				}
			}
		}
		boolean[][] arrived_Knight=new boolean[h][w];
		boolean[][] arrived_Bishop=new boolean[h][w];
		arrived_Knight[sy][sx]=true;
		ArrayDeque<P> que=new ArrayDeque<P>();
		que.add(new P(sx,sy,"Knight",0));
		while(!que.isEmpty()){
			P p=que.poll();
			if(table[p.y][p.x]=='G'){
				System.out.println(p.n);
				return;
			}else if(p.state.equals("Knight")){
				for(int i=0;i<8;i++){
					int nx=p.x+dx1[i];
					int ny=p.y+dy1[i];
					if(nx<0||ny<0||nx>=w||ny>=h)continue;
					if(arrived_Knight[ny][nx])continue;
					arrived_Knight[ny][nx]=true;
					if(table[ny][nx]=='R'){
						que.add(new P(nx,ny,"Bishop",p.n+1));
					}else{
						que.add(new P(nx,ny,"Knight",p.n+1));
					}
				}
			}else if(p.state.equals("Bishop")){
				for(int i=0;i<4;i++){
					int nx=p.x+dx2[i];
					int ny=p.y+dy2[i];
					if(nx<0||ny<0||nx>=w||ny>=h)continue;
					if(arrived_Bishop[ny][nx])continue;
					arrived_Bishop[ny][nx]=true;
					if(table[ny][nx]=='R'){
						que.add(new P(nx,ny,"Knight",p.n+1));
					}else{
						que.add(new P(nx,ny,"Bishop",p.n+1));
					}
				}
			}
		}
		System.out.println(-1);
	}

	final int[] dx1={1,1,-1,-1,2,2,-2,-2};
	final int[] dy1={2,-2,2,-2,1,-1,1,-1};
	final int[] dx2={1,1,-1,-1};
	final int[] dy2={1,-1,1,-1};

	class P{
		int x;
		int y;
		String state;//Knight,Bishop
		int n;
		P(int x,int y,String state,int n){
			this.x=x;
			this.y=y;
			this.state=state;
			this.n=n;
		}
	}
}
0