結果
問題 | No.367 ナイトの転身 |
ユーザー | Drafear |
提出日時 | 2016-04-29 23:04:33 |
言語 | C++11 (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 78 ms / 2,000 ms |
コード長 | 2,659 bytes |
コンパイル時間 | 1,449 ms |
コンパイル使用メモリ | 178,100 KB |
実行使用メモリ | 17,408 KB |
最終ジャッジ日時 | 2024-10-04 18:44:31 |
合計ジャッジ時間 | 2,480 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 27 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:107:41: warning: ‘gy’ may be used uninitialized in this function [-Wmaybe-uninitialized] 107 | REP(i, 2) ans = min(ans, dist[gy][gx][i]); | ^ main.cpp:107:45: warning: ‘gx’ may be used uninitialized in this function [-Wmaybe-uninitialized] 107 | REP(i, 2) ans = min(ans, dist[gy][gx][i]); | ^ main.cpp:89:69: warning: ‘sy’ may be used uninitialized in this function [-Wmaybe-uninitialized] 89 | priority_queue<Node, vector<Node>, greater<Node> > Q; Q.push({sx, sy, 0, 0}); | ~~~~~~^~~~~~~~~~~~~~~~ main.cpp:89:69: warning: ‘sx’ may be used uninitialized in this function [-Wmaybe-uninitialized]
ソースコード
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<ll, ll> P; #define EACH(i,a) for (auto& i : a) #define FOR(i,a,b) for (ll i=(a);i<(b);i++) #define RFOR(i,a,b) for (ll i=(b)-1;i>=(a);i--) #define REP(i,n) for (ll i=0;i<(n);i++) #define RREP(i,n) for (ll i=(n)-1;i>=0;i--) #define debug(x) cout<<#x<<": "<<x<<endl #define pb push_back #define ALL(a) (a).begin(),(a).end() const ll linf = 1e18; const int inf = 1e9; const double eps = 1e-12; const double pi = acos(-1); template<typename T> istream& operator>>(istream& is, vector<T>& vec) { EACH(x,vec) is >> x; return is; } /* template<class... T> ostream& operator<<(ostream& os, tuple<T...>& t) { for (size_t i = 0; i < tuple_size< tuple<T...> >::value; ++i) { if (i) os << " "; os << get<0>(t); } return os; } */ template<typename T> ostream& operator<<(ostream& os, vector<T>& vec) { REP(i,vec.size()) { if (i) os << " "; os << vec[i]; } return os; } template<typename T> ostream& operator<<(ostream& os, vector< vector<T> >& vec) { REP(i,vec.size()) { if (i) os << endl; os << vec[i]; } return os; } struct Node { int x, y, mode, cost; }; bool operator>(const Node& n1, const Node& n2) { return n1.cost > n2.cost; } int H, W; vector<string> m; bool inRange(int x, int y) { return 0 <= x && x < W && 0 <= y && y < H; } vector< vector<P> > nxt = { {{1, 2}, {2, 1}, {1, -2}, {2, -1}, {-1, 2}, {-2, 1}, {-1, -2}, {-2, -1}}, {{1, 1}, {1, -1}, {-1, 1}, {-1, -1}}, }; int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); cin >> H >> W; m.resize(H); cin >> m; int sx, sy, gx, gy; REP(y, H) REP(x, W) { if (m[y][x] == 'S') { m[y][x] = '.'; sx = x, sy = y; } else if (m[y][x] == 'G') { m[y][x] = '.'; gx = x, gy = y; } } vector< vector< vector<int> > > dist(H, vector< vector<int> >(W, vector<int>(2, inf))); priority_queue<Node, vector<Node>, greater<Node> > Q; Q.push({sx, sy, 0, 0}); while ( !Q.empty() ) { Node node = Q.top(); Q.pop(); int x = node.x, y = node.y, cost = node.cost, mode = node.mode; // cout << x << " " << y << " " << mode << " " << cost << endl; if (cost > dist[y][x][mode]) continue; REP(i, nxt[mode].size()) { int nx = x + nxt[mode][i].first; int ny = y + nxt[mode][i].second; if ( !inRange(nx, ny) ) continue; int nm = m[ny][nx] == 'R' ? !mode : mode; if ( cost+1 < dist[ny][nx][nm] ) { dist[ny][nx][nm] = cost+1; Q.push({nx, ny, nm, dist[ny][nx][nm]}); } } } int ans = inf; REP(i, 2) ans = min(ans, dist[gy][gx][i]); if (ans == inf) cout << -1 << endl; else cout << ans << endl; }