結果

問題 No.61 リベリオン
ユーザー scachescache
提出日時 2014-11-10 01:42:13
言語 Java21
(openjdk 21)
結果
AC  
実行時間 687 ms / 5,000 ms
コード長 1,720 bytes
コンパイル時間 5,634 ms
コンパイル使用メモリ 77,488 KB
実行使用メモリ 51,544 KB
最終ジャッジ日時 2024-04-21 17:18:08
合計ジャッジ時間 7,681 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 132 ms
41,368 KB
testcase_01 AC 126 ms
41,272 KB
testcase_02 AC 138 ms
41,392 KB
testcase_03 AC 687 ms
51,544 KB
testcase_04 AC 667 ms
50,360 KB
testcase_05 AC 132 ms
41,360 KB
権限があれば一括ダウンロードができます

ソースコード

diff #


import java.util.Scanner;

public class Main61 {
	public static void main(String[] args) {
		Main61 p = new Main61();
	}

	public Main61() {
		Scanner sc = new Scanner(System.in);
		
		int q = sc.nextInt();
		for(int i=0;i<q;i++){
			int w = sc.nextInt();
			int h = sc.nextInt();
			int d = sc.nextInt();
			int mx = sc.nextInt();
			int my = sc.nextInt();
			int hx = sc.nextInt();
			int hy = sc.nextInt();
			int vx = sc.nextInt();
			int vy = sc.nextInt();
			
			if(solve(w, h, d, mx, my, hx, hy, vx, vy)){
				System.out.println("Hit");
			}else{
				System.out.println("Miss");
			}
		}
	}

	public boolean solve(int w, int h, int d, int mx, int my, int hx, int hy, int vx, int vy) {
		int statex = 1;
		int statey = 1;
		
		if(vx <0){
			vx = -vx;
			statex = -1;
		}
		
		if(vy <0){
			vy = -vy;
			statey = -1;
		}
		// なるほどわからん
		int g = gcd(Math.max(vx, vy), Math.min(vx, vy));
		vx /= g;
		vy /= g;
		int curx = hx;
		int cury = hy;
		int m = Math.min(d*g, 4*w*h);
		for(int i=0;i<m;i++){
			curx += statex * vx;
			cury += statey * vy;
			
			while(curx > w || curx < 0){
				if(curx > w){
					statex = -1;
					curx = 2*w - curx;
				}else if(curx < 0){
					statex = 1;
					curx = - curx;
				}
			}
			while(cury >h || cury < 0){
				if(cury > h){
					statey = -1;
					cury = 2*h - cury;
				}else if(cury < 0){
					statey = 1;
					cury = - cury;
				}
			}
//			System.out.println(curx +" "+ cury);
			if(curx==mx && cury==my)
				return true;
		}
		
		return false;
	}

	private int gcd(int a, int b) {
		while(b != 0){
			int r = a;
			a = b;
			b = r%b;
		}
		return a;
	}

	
	
}
0