結果

問題 No.971 いたずらっ子
ユーザー koyoprokoyopro
提出日時 2020-02-05 01:12:53
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,374 bytes
コンパイル時間 2,809 ms
コンパイル使用メモリ 164,632 KB
実行使用メモリ 98,628 KB
最終ジャッジ日時 2023-10-21 06:43:46
合計ジャッジ時間 8,724 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 578 ms
98,576 KB
testcase_04 AC 539 ms
98,576 KB
testcase_05 AC 570 ms
98,576 KB
testcase_06 AC 440 ms
98,576 KB
testcase_07 WA -
testcase_08 AC 154 ms
86,088 KB
testcase_09 AC 148 ms
86,080 KB
testcase_10 AC 23 ms
69,372 KB
testcase_11 AC 20 ms
69,100 KB
testcase_12 AC 20 ms
69,104 KB
testcase_13 AC 20 ms
69,120 KB
testcase_14 WA -
testcase_15 AC 20 ms
69,120 KB
testcase_16 AC 21 ms
73,368 KB
testcase_17 AC 20 ms
69,104 KB
testcase_18 AC 20 ms
69,120 KB
testcase_19 AC 20 ms
69,120 KB
testcase_20 AC 20 ms
69,112 KB
testcase_21 AC 20 ms
69,180 KB
testcase_22 AC 21 ms
69,164 KB
testcase_23 AC 20 ms
69,104 KB
testcase_24 AC 20 ms
69,128 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define RFOR(i, a, b) for(int i=(b-1);i>=(a);i--)
#define REP(i, n) for(int i=0; i<(n); i++)
#define RREP(i, n) for(int i=(n-1); i>=0; i--)
#define ALL(a) (a).begin(),(a).end()
#define UNIQUE_SORT(l) sort(ALL(l)); l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define array2(type, x, y) array<array<type, y>, x>
#define vector2(type) vector<vector<type> >
#define out(...) printf(__VA_ARGS__)

int dxy[] = {0, 1, 0, -1, 0};

void solve();
signed main()
{
#if DEBUG
    std::ifstream in("input.txt");
    std::cin.rdbuf(in.rdbuf());
#endif
    cin.tie(0);
    ios::sync_with_stdio(false);
    solve();
    return 0;
}

/*================================*/
#if DEBUG
#define SIZE 2000
#else
#define SIZE 2000
#endif

int H,W;
// 合計コスト(route, trick)
int C[SIZE][SIZE][2];
// トリックコスト(route, trick)
int T[SIZE][SIZE][2];
int K[SIZE][SIZE];
//typedef pair<int,int> pos;
struct Pos{
    int h, w, c;
    Pos(): h(0),w(0),c(0){};
    Pos(int h, int w, int c): h(h),w(w),c(c){};
    
    bool operator < (const Pos &q) const {
        return q.c < c;
    };
};

void solve() {
    cin>>H>>W;
    char c;
    REP(h,H)REP(w,W) {
        cin>>c;
        K[h][w]=(c=='k');
    }
    REP(h,SIZE)REP(w,SIZE)REP(i,2) C[h][w][i]=INT_MAX;
    
    REP(i,2) C[0][0][i]=0;
    REP(i,2) T[0][0][i]=0;
    priority_queue<Pos> que;
    que.push(Pos(0,0,0));
    while(!que.empty()) {
        Pos p = que.top();
        que.pop();
        int h = p.h;
        int w = p.w;
        if (p.c > C[h][w][0] + C[h][w][1]) continue;
        REP(d,4) {
            int nh = h + dxy[d];
            int nw = w + dxy[d+1];
            if (nh<0||nw<0||nh>=H||nw>=W) continue;
            // 合計コスト
            int routeCost = C[h][w][0] + 1;
            int trickCost = C[h][w][1] + (K[nh][nw] ? routeCost : 0);
            int nCost = trickCost + routeCost;
            int cCost = C[nh][nw][0]+C[nh][nw][1];
            if (nCost < cCost || (nCost == cCost && routeCost < C[nh][nw][0])) {
                C[nh][nw][0] = routeCost;
                C[nh][nw][1] = trickCost;
                que.push(Pos(nh,nw, routeCost+trickCost));
            }
        }
    }
    
    cout << C[H-1][W-1][0]+C[H-1][W-1][1] << endl;
}
0