結果

問題 No.61 リベリオン
ユーザー data9824data9824
提出日時 2015-06-18 23:40:00
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 38 ms / 5,000 ms
コード長 1,304 bytes
コンパイル時間 521 ms
コンパイル使用メモリ 59,744 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 17:19:27
合計ジャッジ時間 976 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 38 ms
5,376 KB
testcase_04 AC 38 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cmath>

using namespace std;

int gcd(int x, int y) {
	do {
		if (x < y) {
			swap(x, y);
		}
		x = x % y;
	} while (x > 0);
	return y;
}

int main() {
	int q;
	cin >> q;
	for (int i = 0; i < q; ++i) {
		int w, h, d, mx, my, hx, hy, vx, vy;
		cin >> w >> h >> d >> mx >> my >> hx >> hy >> vx >> vy;
		int divisor;
		if (vx == 0) {
			divisor = abs(vy);
		} else if (vy == 0) {
			divisor = abs(vx);
		} else {
			divisor = gcd(abs(vx), abs(vy));
		}
		vx /= divisor;
		vy /= divisor;
		int vxSign = vx >= 0 ? 1 : -1;
		int vySign = vy >= 0 ? 1 : -1;
		vx = abs(vx) % (2 * w);
		if (vx > w) {
			vx = -(2 * w - vx);
		}
		vx *= vxSign;
		vy = abs(vy) % (2 * h);
		if (vy > h) {
			vy = -(2 * h - vy);
		}
		vy *= vySign;
		int x = hx, y = hy;
		int vvx = vx, vvy = vy;
		bool hit = false;
		for (long long t = 0; t < d * divisor; ++t) {
			x += vvx;
			y += vvy;
			if (x < 0) {
				x = -x;
				vvx = -vvx;
			} else if (x > w) {
				x = w - (x - w);
				vvx = -vvx;
			}
			if (y < 0) {
				y = -y;
				vvy = -vvy;
			} else if (y > h) {
				y = h - (y - h);
				vvy = -vvy;
			}
			if (x == mx && y == my) {
				hit = true;
				break;
			}
			if (x == hx && y == hy && vvx == vx && vvy == vy) {
				break;
			}
		}
		cout << (hit ? "Hit" : "Miss") << endl;
	}
	return 0;
}
0