結果

問題 No.208 王将
ユーザー HiroakiSoftwareHiroakiSoftware
提出日時 2015-05-15 23:46:47
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,873 bytes
コンパイル時間 417 ms
コンパイル使用メモリ 47,232 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-06 04:26:00
合計ジャッジ時間 1,128 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 WA -
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 WA -
testcase_19 AC 1 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:30:15: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   30 |         scanf ( "%d %d" , &target.x , &target.y );
      |         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:31:15: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   31 |         scanf ( "%d %d" , &wall.x , &wall.y );
      |         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

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 CheckDiff ( Position pt1 , Position pt2 );
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;

	int diffcurrent;

	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;

		diffcurrent = CheckDiff ( id.pos , target );

		//左上
		idnext.pos = MakePosition ( id.pos.x - 1 , id.pos.y - 1 );
		if ( CheckDiff ( idnext.pos , target ) < diffcurrent )idq.push ( idnext );

		//上
		idnext.pos = MakePosition ( id.pos.x , id.pos.y - 1 );
		if ( CheckDiff ( idnext.pos , target ) < diffcurrent )idq.push ( idnext );

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

		//左
		idnext.pos = MakePosition ( id.pos.x - 1 , id.pos.y );
		if ( CheckDiff ( idnext.pos , target ) < diffcurrent )idq.push ( idnext );

		//右
		idnext.pos = MakePosition ( id.pos.x + 1 , id.pos.y );
		if ( CheckDiff ( idnext.pos , target ) < diffcurrent )idq.push ( idnext );

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

		//下
		idnext.pos = MakePosition ( id.pos.x , id.pos.y + 1 );
		if ( CheckDiff ( idnext.pos , target ) < diffcurrent )idq.push ( idnext );

		//右下
		idnext.pos = MakePosition ( id.pos.x + 1 , id.pos.y + 1 );
		if ( CheckDiff ( idnext.pos , target ) < diffcurrent )idq.push ( idnext );

	}

	return 0;

}

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

int CheckDiff ( Position pt1 , Position pt2 ) {
	return ( pt2.x - pt1.x ) * ( pt2.x - pt1.x ) + ( pt2.y - pt1.y ) *( pt2.y - pt1.y );
}
0