結果

問題 No.124 門松列(3)
ユーザー nmnmnmnmnmnmnmnmnmnmnmnmnmnm
提出日時 2015-01-12 00:03:34
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 7 ms / 5,000 ms
コード長 2,151 bytes
コンパイル時間 1,738 ms
コンパイル使用メモリ 95,612 KB
実行使用メモリ 7,552 KB
最終ジャッジ日時 2023-09-04 04:35:02
合計ジャッジ時間 2,256 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
7,272 KB
testcase_01 AC 5 ms
7,224 KB
testcase_02 AC 4 ms
7,220 KB
testcase_03 AC 7 ms
7,272 KB
testcase_04 AC 5 ms
7,376 KB
testcase_05 AC 6 ms
7,348 KB
testcase_06 AC 4 ms
7,268 KB
testcase_07 AC 5 ms
7,312 KB
testcase_08 AC 5 ms
7,244 KB
testcase_09 AC 5 ms
7,288 KB
testcase_10 AC 5 ms
7,336 KB
testcase_11 AC 6 ms
7,288 KB
testcase_12 AC 5 ms
7,512 KB
testcase_13 AC 5 ms
7,236 KB
testcase_14 AC 5 ms
7,344 KB
testcase_15 AC 5 ms
7,276 KB
testcase_16 AC 5 ms
7,256 KB
testcase_17 AC 5 ms
7,508 KB
testcase_18 AC 4 ms
7,276 KB
testcase_19 AC 5 ms
7,288 KB
testcase_20 AC 4 ms
7,528 KB
testcase_21 AC 5 ms
7,288 KB
testcase_22 AC 5 ms
7,552 KB
testcase_23 AC 4 ms
7,464 KB
testcase_24 AC 5 ms
7,320 KB
testcase_25 AC 6 ms
7,248 KB
testcase_26 AC 5 ms
7,340 KB
testcase_27 AC 5 ms
7,236 KB
testcase_28 AC 5 ms
7,508 KB
testcase_29 AC 5 ms
7,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
 
using namespace std;
 
#define sz size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(c) (c).begin(), (c).end()
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define clr(a, b) memset((a), (b) ,sizeof(a))
 
#define MOD 1000000009

int dp[100][100][10][10];
int d[101][101];

int isK(int a, int b, int c){
  if(a==b||b==c||a==c)return 0;
  int d = b;
  vector<int> v;
  v.pb(a);v.pb(b);v.pb(c);
  sort(all(v));
  if(d==v[0]||d==v[2])return 1;
  return 0;
}

int main(){
  static int dx[4] = {-1, 0, 0, 1};
  static int dy[4] = {0, -1, 1, 0};
  int w,h;
  cin>>w>>h;
  rep(y,0,h){
    rep(x,0,w){
      char a;
      cin>>a;
      d[y][x] = a-'0';
    }
  }
  clr(dp,0);
  queue<int> qy,qx;
  queue<int> a1,a2;
  queue<int> qc;
  qy.push(0);
  qx.push(0);
  a1.push(d[0][0]);
  a2.push(0);
  qc.push(0);
  while(!qy.empty()){
    int yy = qy.front();qy.pop();
    int xx = qx.front();qx.pop();
    int aa1 = a1.front();a1.pop();
    int aa2 = a2.front();a2.pop();
    int cc = qc.front();qc.pop();
    rep(i,0,4){
      int cy = yy+dy[i];
      int cx = xx+dx[i];
      if(0>cy||cy>=h)continue;
      if(0>cx||cx>=w)continue;
      if(aa2==0){
        if(dp[cy][cx][aa1][aa2]!=0)continue;
        dp[cy][cx][aa1][aa2]=1;
        qy.push(cy);
        qx.push(cx);
        a1.push(d[cy][cx]);
        a2.push(aa1);
        qc.push(cc+1);
      }
      else{
        if(dp[cy][cx][aa1][aa2]!=0)continue;
        dp[cy][cx][aa1][aa2]=1;
        if(isK(d[cy][cx],aa1,aa2) == 1){
          if(cy==h-1&&cx==w-1){
            cout << cc+1 << endl;
            return 0;
          }
          qy.push(cy);
          qx.push(cx);
          a1.push(d[cy][cx]);
          a2.push(aa1);
          qc.push(cc+1);
        }
      }
    }
  }
  cout << -1 << endl;
  return 0;
}
0