結果

問題 No.971 いたずらっ子
ユーザー koyoprokoyopro
提出日時 2020-02-05 00:24:40
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 2,005 bytes
コンパイル時間 1,954 ms
コンパイル使用メモリ 165,416 KB
実行使用メモリ 110,628 KB
最終ジャッジ日時 2023-10-21 06:42:53
合計ジャッジ時間 8,911 ms
ジャッジサーバーID
(参考情報)
judge9 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
権限があれば一括ダウンロードができます

ソースコード

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 5
#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;

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;
    queue<pos> que;
    que.push(pos(0,0));
    while(!que.empty()) {
        pos p = que.front();
        que.pop();
        REP(d,4) {
            int h = p.first;
            int w = p.second;
            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);
            if (trickCost + routeCost < C[nh][nw][0]+C[nh][nw][1]) {
                C[nh][nw][0] = routeCost;
                C[nh][nw][1] = trickCost;
                que.push(pos(nh,nw));
            }
        }
    }
    
    cout << C[H-1][W-1][0]+C[H-1][W-1][1] << endl;
}
0