結果

問題 No.124 門松列(3)
ユーザー imulanimulan
提出日時 2016-07-20 03:15:49
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 6 ms / 5,000 ms
コード長 1,880 bytes
コンパイル時間 1,737 ms
コンパイル使用メモリ 170,164 KB
実行使用メモリ 7,680 KB
最終ジャッジ日時 2024-04-23 16:57:24
合計ジャッジ時間 2,684 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
7,444 KB
testcase_01 AC 4 ms
7,680 KB
testcase_02 AC 4 ms
7,428 KB
testcase_03 AC 5 ms
7,552 KB
testcase_04 AC 6 ms
7,484 KB
testcase_05 AC 5 ms
7,552 KB
testcase_06 AC 4 ms
7,552 KB
testcase_07 AC 4 ms
7,400 KB
testcase_08 AC 4 ms
7,480 KB
testcase_09 AC 3 ms
7,428 KB
testcase_10 AC 3 ms
7,424 KB
testcase_11 AC 4 ms
7,424 KB
testcase_12 AC 4 ms
7,552 KB
testcase_13 AC 4 ms
7,480 KB
testcase_14 AC 4 ms
7,348 KB
testcase_15 AC 5 ms
7,424 KB
testcase_16 AC 5 ms
7,552 KB
testcase_17 AC 4 ms
7,344 KB
testcase_18 AC 3 ms
7,512 KB
testcase_19 AC 4 ms
7,460 KB
testcase_20 AC 4 ms
7,424 KB
testcase_21 AC 4 ms
7,492 KB
testcase_22 AC 5 ms
7,552 KB
testcase_23 AC 4 ms
7,424 KB
testcase_24 AC 4 ms
7,424 KB
testcase_25 AC 5 ms
7,548 KB
testcase_26 AC 4 ms
7,424 KB
testcase_27 AC 4 ms
7,424 KB
testcase_28 AC 5 ms
7,424 KB
testcase_29 AC 6 ms
7,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second

typedef pair<int,int> pi;
typedef pair<pi,pi> P;

inline bool check(int x, int y, int z)
{
    //まだ3つ揃っていない
    if(x==0) return true;
    //判定
    if(x==y || y==z || z==x) return false;
    if((x<y&&y>z)||(x>y&&y<z)) return true;
    return false;
}

const int INF=12345678;
int cost[100][100][10][10];

int dx[4]={1,-1,0,0}, dy[4]={0,0,1,-1};

int main()
{
    int w,h;
    int m[100][100];

    cin >>w >>h;
    rep(i,h)rep(j,w) cin >>m[i][j];

    rep(a,100)rep(b,100)rep(c,10)rep(d,10) cost[a][b][c][d]=INF;
    cost[0][0][0][m[0][0]]=0;

    queue<P> que;
    que.push(P(pi(0,0),pi(0,m[0][0])));
    while(!que.empty())
    {
        P p=que.front();
        que.pop();

        //現在位置
        pi now=p.fi;
        //(2つ前,1つ前)
        pi prev=p.se;

        // printf("now=%d,%d ::: prev=%d,%d\n", now.fi,now.se,prev.fi,prev.se);

        //4近傍
        rep(i,4)
        {
            int nx=now.fi+dx[i], ny=now.se+dy[i];
            if(0<=nx&&nx<w && 0<=ny&&ny<h)
            {
                pi nxprev(prev.se,m[ny][nx]);

                if(check(prev.fi,prev.se,m[ny][nx]) && cost[ny][nx][nxprev.fi][nxprev.se] > cost[now.se][now.fi][prev.fi][prev.se]+1)
                {
                    cost[ny][nx][nxprev.fi][nxprev.se] = cost[now.se][now.fi][prev.fi][prev.se]+1;
                    que.push(P(pi(nx,ny),nxprev));
                }
            }
        }
    }

    int ans=INF;
    rep(i,10)rep(j,10) ans=min(ans,cost[h-1][w-1][i][j]);
    if(ans==INF) ans=-1;
    cout << ans << endl;
    return 0;
}
0