結果

問題 No.367 ナイトの転身
ユーザー めうめう🎒めうめう🎒
提出日時 2016-05-11 17:23:15
言語 Java21
(openjdk 21)
結果
AC  
実行時間 602 ms / 2,000 ms
コード長 2,994 bytes
コンパイル時間 2,535 ms
コンパイル使用メモリ 78,624 KB
実行使用メモリ 61,348 KB
最終ジャッジ日時 2024-04-15 15:10:57
合計ジャッジ時間 11,150 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 200 ms
53,384 KB
testcase_01 AC 198 ms
53,348 KB
testcase_02 AC 219 ms
53,328 KB
testcase_03 AC 202 ms
53,508 KB
testcase_04 AC 200 ms
53,248 KB
testcase_05 AC 212 ms
53,176 KB
testcase_06 AC 230 ms
53,168 KB
testcase_07 AC 213 ms
53,508 KB
testcase_08 AC 208 ms
53,504 KB
testcase_09 AC 216 ms
53,328 KB
testcase_10 AC 512 ms
59,176 KB
testcase_11 AC 602 ms
61,348 KB
testcase_12 AC 311 ms
54,540 KB
testcase_13 AC 325 ms
54,944 KB
testcase_14 AC 439 ms
56,408 KB
testcase_15 AC 228 ms
53,480 KB
testcase_16 AC 419 ms
57,480 KB
testcase_17 AC 261 ms
54,744 KB
testcase_18 AC 272 ms
54,680 KB
testcase_19 AC 295 ms
54,672 KB
testcase_20 AC 249 ms
53,448 KB
testcase_21 AC 294 ms
54,508 KB
testcase_22 AC 217 ms
53,468 KB
testcase_23 AC 216 ms
53,640 KB
testcase_24 AC 217 ms
53,496 KB
testcase_25 AC 225 ms
53,124 KB
testcase_26 AC 211 ms
53,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Queue;
import java.util.Scanner;

class State{
	public int nx;
	public int ny;
	public int how;
	public boolean jo;
	State(int how,int nx,int ny,boolean jo){
		this.how = how;
		this.nx = nx;
		this.ny = ny;
		this.jo = jo;
	}
}

public class Main {
	private int[] DIRX1 = {1,1,2,2,-1,-1,-2,-2};
	private int[] DIRY1 = {2,-2,1,-1,2,-2,1,-1};
	private int[] DIRX2 = {1,1,-1,-1};
	private int[] DIRY2 = {1,-1,1,-1};
	
	private int[][] hyo;
	private int h;
	private int w;
	private int sx;
	private int sy;
	private int gx;
	private int gy;
	
	Main(){
		hyo = new int[501][501];
		for(int i = 0;i < 501;i++){
			for(int j = 0;j < 501;j++){
				hyo[j][i] = 0;
			}
		}
	}
	
	public void setHyo(int i,int j,int num){
		this.hyo[j][i] = num;
	}
	
	public void setSx(int sx){
		this.sx = sx;
	}
	
	public void setSy(int sy){
		this.sy = sy;
	}
	
	public void setGx(int gx){
		this.gx = gx;
	}
	
	public void setGy(int gy){
		this.gy = gy;
	}
	
	public void setH(int h){
		this.h= h;
	}
	
	public void setW(int w){
		this.w = w;
	}
	
	public int getSx(){
		return this.sx;
	}
	
	public int getSy(){
		return this.sy;
	}
	
	public int dfs(int sx,int sy,boolean sjo){
		Queue<State> que = new ArrayDeque<State>();
		State f = new State(0,sx,sy,false);
		boolean[][][] used = new boolean[501][501][2];
		for(int i = 0;i < 501;i++){
			for(int j = 0;j < 501;j++){
				for(int k = 0;k < 2;k++){
					used[j][i][k] = false;
				}
			}
		}
		que.add(f);
		while(que.peek() != null){
			State now = que.poll();
			int how = now.how;
			int nx = now.nx;
			int ny = now.ny;
			boolean jo = now.jo;
			
			if(nx == gx && ny == gy){
				return how;
			}
			
			if(used[nx][ny][jo ? 1 : 0] == true) continue;
			used[nx][ny][jo ? 1 : 0] = true;
			
			if(jo){
				for(int i = 0;i < 4;i++){
					int tx = nx + DIRX2[i],ty = ny + DIRY2[i];
					if(tx < 0 || tx >= w || ty < 0 || ty >= h) continue;
					if(hyo[tx][ty] == 1){
						que.add(new State(how + 1,tx,ty,false));
					}else{
						que.add(new State(how + 1,tx,ty,true));
					}
				}
			}else{
				for(int i = 0;i < 8;i++){
					int tx = nx + DIRX1[i],ty = ny + DIRY1[i];
					if(tx < 0 || tx >= w || ty < 0 || ty >= h) continue;
					if(hyo[tx][ty] == 1){
						que.add(new State(how + 1,tx,ty,true));
					}else{
						que.add(new State(how + 1,tx,ty,false));
					}
				}
			}
		}
		return -1;
	}
	
	public void run(){
		System.out.println(dfs(this.getSx(),this.getSy(),false));
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Main m = new Main();
		int h = sc.nextInt();
		int w = sc.nextInt();
		m.setH(h);
		m.setW(w);
		String str;
		for(int i = 0;i < h;i++){
			str = sc.next();
			char[] s = str.toCharArray();
			for(int j = 0;j < w;j++){
				if(s[j] == 'R'){
					m.setHyo(i, j, 1);
				}else if(s[j] == 'S'){
					m.setSx(j);
					m.setSy(i);
				}else if(s[j] == 'G'){
					m.setGx(j);
					m.setGy(i);
				}
			}
		}
		m.run();
	}
}
0