結果

問題 No.367 ナイトの転身
ユーザー 37zigen37zigen
提出日時 2016-04-29 23:32:15
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,906 bytes
コンパイル時間 4,159 ms
コンパイル使用メモリ 78,568 KB
実行使用メモリ 54,488 KB
最終ジャッジ日時 2024-04-15 05:59:45
合計ジャッジ時間 8,380 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 134 ms
54,316 KB
testcase_01 AC 134 ms
53,924 KB
testcase_02 AC 122 ms
52,740 KB
testcase_03 AC 134 ms
54,016 KB
testcase_04 AC 135 ms
53,868 KB
testcase_05 AC 133 ms
54,268 KB
testcase_06 AC 155 ms
54,424 KB
testcase_07 AC 130 ms
54,060 KB
testcase_08 AC 134 ms
54,408 KB
testcase_09 WA -
testcase_10 TLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
権限があれば一括ダウンロードができます

ソースコード

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")){
				arrived_Knight[p.y][p.x]=true;
				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;
					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")){
				arrived_Bishop[p.y][p.x]=true;
				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;
					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