結果

問題 No.208 王将
ユーザー HiroakiSoftwareHiroakiSoftware
提出日時 2015-05-15 23:01:15
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 2,031 bytes
コンパイル時間 361 ms
コンパイル使用メモリ 47,396 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-09-20 08:28:28
合計ジャッジ時間 4,162 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 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,500 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 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 <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;

		for (unsigned int i = 0; i < history.size (); i++ ) {
			if ( ( history [ i ].x == id.pos.x ) && ( history [ i ].y == id.pos.y ) ) {
				AlreadyScanned = 1;
				break;
			}
		}


		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