結果
| 問題 |
No.124 門松列(3)
|
| コンテスト | |
| ユーザー |
tubo28
|
| 提出日時 | 2015-01-12 01:29:17 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 5 ms / 5,000 ms |
| コード長 | 2,459 bytes |
| コンパイル時間 | 910 ms |
| コンパイル使用メモリ | 94,928 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-06-22 04:26:13 |
| 合計ジャッジ時間 | 1,868 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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,pre
typedef tuple<int,int,int> State;
typedef tuple<int,int> ns;
map<ns,int> dp;
int solve(){
dp.clear();
dp[mt(0,-1)] = 0;
priority_queue<State> q;
q.emplace(0,0,-1);
while(q.size()){
int d,v,p;
tie(d,v,p) = q.top(); q.pop();
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,p)) && dp[tie(v,p)] < d) continue;
rep(i,4){
int nx = v%W+dx[i], ny = v/W+dy[i];
int x = v%W, y = v/W;
int px = p%W, py = p/W;
if(nx < 0 || nx >= W || ny < 0 || ny >= H) continue;
if(p==-1){
if(M[y][x]==M[ny][nx]){
continue;
}
} else {
if(M[py][px] == M[ny][nx]) continue;
if(!((M[py][px] < M[y][x] && M[y][x] > M[ny][nx]) ||
(M[py][px] > M[y][x] && M[y][x] < M[ny][nx]))){
continue;
}
}
int nv = ny*W+nx;
if(dp.count(tie(nv,v))==0 || dp[tie(nv,v)] > d+1){
dp[tie(nv,v)] = d+1;
q.emplace(-(d+1),nv,v);
}
}
}
return -1;
}
int main(){
while(cin>>W>>H && W){
rep(i,H)rep(j,W) cin >> M[i][j];
cout << solve() << endl;
}
}
tubo28