結果

問題 No.240 ナイト散歩
ユーザー 37zigen
提出日時 2016-05-01 16:03:01
言語 Java
(openjdk 23)
結果
AC  
実行時間 128 ms / 2,000 ms
コード長 998 bytes
コンパイル時間 2,111 ms
コンパイル使用メモリ 77,900 KB
実行使用メモリ 54,556 KB
最終ジャッジ日時 2024-10-07 21:24:39
合計ジャッジ時間 7,154 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Scanner;
public class Main{
	int[] dx={2,2,-2,-2,1,-1,1,-1};
	int[] dy={1,-1,1,-1,2,2,-2,-2};
	public static void main(String[] args)throws Exception{
		new Main().solve();
	}
	void solve(){
		Scanner sc=new Scanner(System.in);
		int x=sc.nextInt()+500;
		int y=sc.nextInt()+500;
		Queue<Coordinate> q=new ArrayDeque<Coordinate>();
		boolean[][] arrived=new boolean[1000][1000];
		
		q.add(new Coordinate(500,500,0));
		arrived[500][500]=true;
		while(!q.isEmpty()){
			Coordinate c=q.poll();
			if(c.x==x&&c.y==y){
				System.out.println("YES");
				return;
			}
			if(c.n==3)continue;
			for(int i=0;i<8;i++){
				int nx=c.x+dx[i];
				int ny=c.y+dy[i];
				if(arrived[ny][nx])continue;
				arrived[ny][nx]=true;
				q.add(new Coordinate(nx, ny,c.n+1));
			}
		}
		System.out.println("NO");
	}
	class Coordinate{
		int x,y,n;
		public Coordinate(int x,int y,int n) {
			this.x=x;this.y=y;this.n=n;
		}
	}
}
0