結果

問題 No.367 ナイトの転身
ユーザー 37zigen37zigen
提出日時 2016-04-29 23:40:58
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,939 bytes
コンパイル時間 2,419 ms
コンパイル使用メモリ 78,036 KB
実行使用メモリ 60,004 KB
最終ジャッジ日時 2024-04-15 06:07:16
合計ジャッジ時間 8,570 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 138 ms
54,328 KB
testcase_01 AC 135 ms
54,248 KB
testcase_02 AC 136 ms
54,004 KB
testcase_03 AC 139 ms
54,132 KB
testcase_04 AC 138 ms
53,876 KB
testcase_05 AC 135 ms
53,860 KB
testcase_06 AC 169 ms
54,220 KB
testcase_07 AC 135 ms
53,992 KB
testcase_08 AC 136 ms
54,016 KB
testcase_09 WA -
testcase_10 AC 376 ms
58,880 KB
testcase_11 AC 458 ms
60,004 KB
testcase_12 AC 225 ms
56,560 KB
testcase_13 AC 233 ms
56,452 KB
testcase_14 AC 276 ms
57,944 KB
testcase_15 AC 161 ms
54,196 KB
testcase_16 AC 273 ms
58,124 KB
testcase_17 AC 186 ms
54,332 KB
testcase_18 AC 199 ms
54,400 KB
testcase_19 AC 197 ms
54,568 KB
testcase_20 AC 165 ms
54,276 KB
testcase_21 AC 197 ms
54,360 KB
testcase_22 AC 135 ms
54,260 KB
testcase_23 AC 142 ms
54,272 KB
testcase_24 AC 144 ms
54,168 KB
testcase_25 AC 142 ms
54,032 KB
testcase_26 AC 139 ms
54,092 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")){
				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;
					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