結果

問題 No.208 王将
コンテスト
ユーザー HiroakiSoftware
提出日時 2015-05-15 23:11:17
言語 C++11
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
TLE  
実行時間 -
コード長 2,192 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 351 ms
コンパイル使用メモリ 67,828 KB
実行使用メモリ 12,800 KB
最終ジャッジ日時 2026-03-27 14:45:54
合計ジャッジ時間 2,913 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 9 TLE * 1 -- * 13
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <queue>
#include <vector>
#include <cstdio>

using namespace std;

struct Position {
	int x;
	int y;
};

struct InnerData {

	Position pos;
	int cost;

};

typedef queue<InnerData> InnerDataQueue;
typedef vector<Position> PositionVector;

Position MakePosition ( int x , int y );

int main ( void ) {


	Position target;
	Position wall;

	scanf ( "%d %d" , &target.x , &target.y );
	scanf ( "%d %d" , &wall.x , &wall.y );

	InnerData idst;

	idst.pos.x = 0;
	idst.pos.y = 0;
	idst.cost = 0;

	PositionVector history;
	InnerDataQueue idq;

	InnerData id , idnext;
	idq.push ( idst );

	int AlreadyScanned;

	while ( idq.empty() == false ) {

		id = idq.front ();

		idq.pop ();


		if ( ( id.pos.x == wall.x ) && ( id.pos.y == wall.y ) ) continue;
		if ( ( id.pos.x == target.x ) && ( id.pos.y == target.y ) ) {
			printf ( "%d\n" , id.cost );
			break;
		}

		AlreadyScanned = 0;

		if ( history.empty () == false ) {
			for ( unsigned int i = history.size () - 1; i >= 1; i-- ) {
				if ( ( history [ i ].x == id.pos.x ) && ( history [ i ].y == id.pos.y ) ) {
					AlreadyScanned = 1;
					break;
				}
			}

			if ( ( history [ 0 ].x == id.pos.x ) && ( history [ 0 ].y == id.pos.y ) ) {
				AlreadyScanned = 1;
			}
		}

		if ( AlreadyScanned == 1 )continue;


		history.push_back ( id.pos );

		idnext.cost = id.cost + 1;

		//左上
		idnext.pos = MakePosition ( id.pos.x - 1 , id.pos.y - 1 );
		idq.push ( idnext );

		//上
		idnext.pos = MakePosition ( id.pos.x  , id.pos.y - 1 );
		idq.push ( idnext );

		//右上
		idnext.pos = MakePosition ( id.pos.x + 1 , id.pos.y - 1 );
		idq.push ( idnext );

		//左
		idnext.pos = MakePosition ( id.pos.x - 1 , id.pos.y );
		idq.push ( idnext );

		//右
		idnext.pos = MakePosition ( id.pos.x + 1 , id.pos.y );
		idq.push ( idnext );

		//左下
		idnext.pos = MakePosition ( id.pos.x - 1 , id.pos.y + 1 );
		idq.push ( idnext );

		//下
		idnext.pos = MakePosition ( id.pos.x   , id.pos.y + 1 );
		idq.push ( idnext );

		//右下
		idnext.pos = MakePosition ( id.pos.x + 1 , id.pos.y + 1 );
		idq.push ( idnext );
	}

	return 0;

}

Position MakePosition ( int x , int y ) {
	Position pos;
	pos.x = x;
	pos.y = y;
	return pos;
}
0