結果
| 問題 |
No.367 ナイトの転身
|
| コンテスト | |
| ユーザー |
siguma323
|
| 提出日時 | 2016-04-30 12:36:55 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 25 ms / 2,000 ms |
| コード長 | 2,720 bytes |
| コンパイル時間 | 882 ms |
| コンパイル使用メモリ | 89,332 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-10-11 00:22:11 |
| 合計ジャッジ時間 | 1,860 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 27 |
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:66:21: warning: 'start_y' may be used uninitialized [-Wmaybe-uninitialized]
66 | q.push(make_pair(start_x,start_y));
| ~~~~~~~~~^~~~~~~~~~~~~~~~~
main.cpp:52:17: note: 'start_y' was declared here
52 | int start_x,start_y;
| ^~~~~~~
main.cpp:66:21: warning: 'start_x' may be used uninitialized [-Wmaybe-uninitialized]
66 | q.push(make_pair(start_x,start_y));
| ~~~~~~~~~^~~~~~~~~~~~~~~~~
main.cpp:52:9: note: 'start_x' was declared here
52 | int start_x,start_y;
| ^~~~~~~
ソースコード
#include <algorithm>
#include <numeric>
#include <string>
#include <vector>
#include <iostream>
#include <queue>
#include <utility>
#include <cmath>
#include <bitset>
#include <cstring>
using namespace std;
using ull = unsigned long long;
bool used[2][500][500];
int ans = 1000000;
int night_dx[8] = {2,1,1,2,-1,-1,-2,-2};
int night_dy[8] = {1,2,-2,-1,2,-2,-1,1};
int dx[4] = {-1,1,-1,1};
int dy[4] = {1,-1,-1,1};
int h,w;
bool InBounds(pair<int,int> test)
{
if(test.first >=0 && test.second >=0 && test.first < w && test.second < h)
return true;
else return false;
}
int main()
{
cin >> h >> w;
vector<string> board(h);
queue<pair<int,int>> q;
vector<pair<int,int>> cash;
queue<pair<int,int>> counter;
int dp[500][500];
for(auto&& x: dp)
for(auto&& y : x)
y = 100000;
for(auto&& x : board)
{
cin >> x;
}
int start_x,start_y;
for(int y = 0; y < h; ++y)
{
for(int x= 0; x < w; ++x)
{
if(board.at(y).at(x) == 'S')
{
start_x = x;
start_y = y;
break;
}
}
}
q.push(make_pair(start_x,start_y));
counter.push(make_pair(0,true));
while(!q.empty())
{
pair<int,int> test = q.front();
q.pop();
int num = counter.front().first;
bool is_night = counter.front().second;
counter.pop();
if(board.at(test.second).at(test.first) == 'G')
{
ans = min(ans,num);
continue;
}
if(board.at(test.second).at(test.first) == 'R')
{
is_night ^= true;
}
if(is_night)
{
for(int i =0; i < 8; ++i)
{
pair<int,int> tmp = make_pair(test.first + night_dx[i],test.second + night_dy[i]);
if(InBounds(tmp) && !used[is_night][tmp.second][tmp.first])
{
q.push(tmp);
counter.push(make_pair(num+1,is_night));
used[is_night][tmp.second][tmp.first] = true;
}
}
}
else
{
for(int i =0; i < 4; ++i)
{
pair<int,int> tmp = make_pair(test.first + dx[i],test.second + dy[i]);
if(InBounds(tmp) && !used[is_night][tmp.second][tmp.first])
{
q.push(tmp);
counter.push(make_pair(num+1,is_night));
used[is_night][tmp.second][tmp.first] = true;
}
}
}
}
if(ans != 1000000)
cout << ans << endl;
else
cout << -1 << endl;
}
siguma323