結果

問題 No.124 門松列(3)
ユーザー nmnmnmnmnmnmnmnmnmnmnmnmnmnm
提出日時 2015-01-11 23:54:46
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,070 bytes
コンパイル時間 1,006 ms
コンパイル使用メモリ 96,252 KB
実行使用メモリ 7,512 KB
最終ジャッジ日時 2023-09-04 04:28:44
合計ジャッジ時間 2,729 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
7,292 KB
testcase_01 WA -
testcase_02 AC 5 ms
7,252 KB
testcase_03 AC 11 ms
7,412 KB
testcase_04 AC 12 ms
7,300 KB
testcase_05 AC 5 ms
7,344 KB
testcase_06 AC 5 ms
7,328 KB
testcase_07 AC 4 ms
7,308 KB
testcase_08 AC 5 ms
7,240 KB
testcase_09 AC 4 ms
7,312 KB
testcase_10 AC 4 ms
7,272 KB
testcase_11 AC 5 ms
7,512 KB
testcase_12 AC 5 ms
7,236 KB
testcase_13 AC 5 ms
7,292 KB
testcase_14 AC 5 ms
7,316 KB
testcase_15 AC 5 ms
7,264 KB
testcase_16 WA -
testcase_17 AC 5 ms
7,384 KB
testcase_18 AC 5 ms
7,432 KB
testcase_19 AC 5 ms
7,464 KB
testcase_20 WA -
testcase_21 AC 5 ms
7,240 KB
testcase_22 AC 5 ms
7,312 KB
testcase_23 AC 5 ms
7,356 KB
testcase_24 AC 5 ms
7,296 KB
testcase_25 AC 5 ms
7,280 KB
testcase_26 AC 5 ms
7,236 KB
testcase_27 AC 5 ms
7,324 KB
testcase_28 AC 5 ms
7,456 KB
testcase_29 AC 6 ms
7,368 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)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(-1);
  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==-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