結果

問題 No.208 王将
ユーザー furafyfurafy
提出日時 2015-05-23 20:01:40
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 2,210 bytes
コンパイル時間 342 ms
コンパイル使用メモリ 52,180 KB
実行使用メモリ 8,756 KB
最終ジャッジ日時 2023-09-20 10:55:07
合計ジャッジ時間 2,996 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,756 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <iostream>
using namespace std;

bool safe();

struct Point {
	int x;
	int y;
};

Point now = { 0, 0 };
Point goal;
Point obst;

int dirx;
int diry;

int main(int argc, char *argv[]) {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	cin >> goal.x >> goal.y >> obst.x >> obst.y;
	int flag;
	int cnt = 0;
	while (1) {
		dirx = goal.x - now.x; //dirx>0 で右に進むべき
		diry = goal.y - now.y; //diry>0 で左に進むべき
		flag = (abs(dirx) > abs(diry));
		if (dirx > 0) {
			if (diry > 0) { // 右上
				if (safe()) {
					now.x++;
					now.y++;
				}
				else {
					now.x += flag;
					now.y += !flag;
				}
			}
			else if (diry < 0) { // 右下
				if (safe()) {
					now.x++;
					now.y--;
				}
				else {
					now.x += flag;
					now.y += !flag;
				}
			}
			else if (diry == 0) { // 右
				now.x++;
			}
		}
		else if (dirx < 0) {
			if (diry > 0) { // 左上
				if (safe()) {
					now.x--;
					now.y++;
				}
				else {
					now.x += flag;
					now.y += !flag;
				}
			}
			else if (diry < 0) { // 左下
				if (safe()) {
					now.x--;
					now.y--;
				}
				else {
					now.x += flag;
					now.y += !flag;
				}
			}
			else if (diry == 0) { // 左
				now.x--;
			}
		}
		else if (dirx == 0) {
			if (diry > 0) { // 上
				now.y++;
			}
			else if (diry < 0) { // 下
				now.y--;
			}
			else if (diry == 0) { // 到着
				break;
			}
		}
		cnt++;
	}
	cout << cnt << endl;
	return 0;
}

bool safe() {
	bool toRight = (dirx > 0);
	bool toLeft = !toRight;
	bool toUp = (diry > 0);
	bool toBottom = !toUp;
	bool safe;
	bool goalNext;
	if (toRight && toUp) {
		safe = !((now.x + 2 == obst.x) && (now.y + 2 == obst.y));
		goalNext = (now.x + 1 == goal.x) && (now.y + 1 == goal.y);
	}
	else if (toRight && toBottom) {
		safe = !((now.x + 2 == obst.x) && (now.y - 2 == obst.y));
		goalNext = (now.x + 1 == goal.x) && (now.y -1 == goal.y);
	}
	else if (toLeft && toUp) {
		safe = !((now.x - 2 == obst.x) && (now.y + 2 == obst.y));
		goalNext = (now.x - 1 == goal.x) && (now.y + 1 == goal.y);
	}
	else {
		safe = !((now.x - 2 == obst.x) && (now.y - 2 == obst.y));
		goalNext = (now.x - 1 == goal.x) && (now.y - 1 == goal.y);
	}
	return safe || goalNext;
}
0