結果
| 問題 |
No.208 王将
|
| コンテスト | |
| ユーザー |
HiroakiSoftware
|
| 提出日時 | 2015-05-15 23:46:47 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.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 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 21 WA * 2 |
コンパイルメッセージ
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 );
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ソースコード
#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 );
}
HiroakiSoftware