結果
| 問題 |
No.208 王将
|
| コンテスト | |
| ユーザー |
HiroakiSoftware
|
| 提出日時 | 2015-05-15 23:11:17 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,192 bytes |
| コンパイル時間 | 368 ms |
| コンパイル使用メモリ | 46,336 KB |
| 実行使用メモリ | 10,624 KB |
| 最終ジャッジ日時 | 2024-07-06 04:14:19 |
| 合計ジャッジ時間 | 2,860 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 9 TLE * 1 -- * 13 |
コンパイルメッセージ
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 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;
}
HiroakiSoftware