結果
| 問題 |
No.367 ナイトの転身
|
| コンテスト | |
| ユーザー |
めうめう🎒
|
| 提出日時 | 2016-05-11 17:23:15 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 461 ms / 2,000 ms |
| コード長 | 2,994 bytes |
| コンパイル時間 | 1,944 ms |
| コンパイル使用メモリ | 78,088 KB |
| 実行使用メモリ | 61,344 KB |
| 最終ジャッジ日時 | 2024-10-05 13:44:19 |
| 合計ジャッジ時間 | 8,936 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 27 |
ソースコード
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();
}
}
めうめう🎒