結果
問題 | No.124 門松列(3) |
ユーザー |
![]() |
提出日時 | 2015-01-12 00:06:53 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 5 ms / 5,000 ms |
コード長 | 2,545 bytes |
コンパイル時間 | 982 ms |
コンパイル使用メモリ | 97,872 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-06-22 04:06:34 |
合計ジャッジ時間 | 1,949 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 26 |
ソースコード
#define _CRT_SECURE_NO_WARNINGS #include <deque> #include <map> #include <queue> #include <iostream> #include <cstdio> #include <cstring> #include <ctime> #include <string> #include <vector> #include <algorithm> #include <cmath> #include <sstream> #include <utility> using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int,int> pii; #define all(c) (c).begin(), (c).end() #define loop(i,a,b) for(ll i=a; i<ll(b); i++) #define rep(i,b) loop(i,0,b) #define each(e,c) for(auto&e:c) #define pb push_back #define eb emplace_back #define mp make_pair #define mt make_tuple #define lb lower_bound #define ub upper_bound #ifdef DEBUG #define dump(...) (cerr<<#__VA_ARGS__<<" = "<<(DUMP(),__VA_ARGS__).str()<<" ["<<__LINE__<<"]"<<endl) struct DUMP:ostringstream{template<class T>DUMP &operator,(const T&t){if(this->tellp())*this<<", ";*this<<t;return *this;}}; #else #define dump(...) #endif template<class T> ostream& operator<<(ostream& os, vector<T> const& v){ rep(i,v.size()) os << v[i] << (i+1==v.size()?"":" "); return os; } int W,H; int M[111][111]; // d,v,pre1,pre2 typedef tuple<int,int,int,int> State; typedef tuple<int,int,int> ns; map<ns,int> dp; int solve(){ dp.clear(); dp[mt(0,-1,-1)] = 0; priority_queue<State> q; q.emplace(0,0,-1,-1); while(q.size()){ int d,v,p1,p2; tie(d,v,p1,p2) = q.top(); q.pop(); //dump(d,v,p1,p2); d = -d; if(v==W*H-1) return d; int dx[]={0,1,-1,0}, dy[]={1,0,0,-1}; if(dp.count(tie(v,p1,p2)) && dp[tie(v,p1,p2)] < d) continue; rep(i,4){ int nx = v%W+dx[i], ny = v/W+dy[i]; int x = v%W, y = v/W; int p1x = p1%W, p1y = p1/W; if(nx < 0 || nx >= W || ny < 0 || ny >= H) continue; if(p1==-1){ if(M[y][x]==M[ny][nx]){ continue; } } else { if(M[p1y][p1x] == M[ny][nx]) continue; if(!((M[p1y][p1x] < M[y][x] && M[y][x] > M[ny][nx]) || (M[p1y][p1x] > M[y][x] && M[y][x] < M[ny][nx]))){ continue; } } int nv = ny*W+nx; if(dp.count(tie(nv,v,p1))==0 || dp[tie(nv,v,p1)] > d+1){ dp[tie(nv,v,p1)] = d+1; q.emplace(-(d+1),nv,v,p1); } } } return -1; } int main(){ while(cin>>W>>H && W){ rep(i,H)rep(j,W) cin >> M[i][j]; cout << solve() << endl; } }