結果

問題 No.971 いたずらっ子
ユーザー bond_cmprogbond_cmprog
提出日時 2020-01-17 22:09:06
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,051 ms / 2,000 ms
コード長 2,935 bytes
コンパイル時間 1,972 ms
コンパイル使用メモリ 174,352 KB
実行使用メモリ 39,552 KB
最終ジャッジ日時 2024-04-25 02:02:03
合計ジャッジ時間 10,685 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,046 ms
39,552 KB
testcase_01 AC 1,051 ms
39,424 KB
testcase_02 AC 783 ms
39,552 KB
testcase_03 AC 928 ms
39,552 KB
testcase_04 AC 851 ms
39,168 KB
testcase_05 AC 878 ms
39,552 KB
testcase_06 AC 675 ms
38,784 KB
testcase_07 AC 28 ms
35,584 KB
testcase_08 AC 236 ms
37,376 KB
testcase_09 AC 229 ms
37,504 KB
testcase_10 AC 26 ms
35,584 KB
testcase_11 AC 22 ms
35,328 KB
testcase_12 AC 22 ms
35,456 KB
testcase_13 AC 22 ms
35,328 KB
testcase_14 AC 25 ms
39,424 KB
testcase_15 AC 21 ms
35,456 KB
testcase_16 AC 22 ms
35,968 KB
testcase_17 AC 21 ms
35,328 KB
testcase_18 AC 22 ms
35,200 KB
testcase_19 AC 22 ms
35,456 KB
testcase_20 AC 21 ms
35,328 KB
testcase_21 AC 21 ms
35,456 KB
testcase_22 AC 21 ms
35,328 KB
testcase_23 AC 22 ms
35,456 KB
testcase_24 AC 22 ms
35,456 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0;i < n;i++)
#define VSORT(v) sort(v.begin(), v.end())
#define VRSORT(v) sort(v.rbegin(), v.rend())
#define ll long long
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;
//typedef vector<unsigned int>vec;
//typedef vector<ll>vec;
//typedef vector<vec> mat;
typedef vector<vector<int>> Graph;

const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INF = 1000000000;
const ll LINF = 1000000000000000000;//1e18
const ll  MOD = 1000000007;
const double PI = acos(-1.0);
const double EPS = 1e-10;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline void add(T &a, T b){a = ((a+b) % MOD + MOD) % MOD;};

char fi[2020][2020];
int dist1[2020][2020];
int dist2[2020][2020];

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int H, W;
    cin >> H >> W;
    REP(i,H) REP(j,W) cin >> fi[i][j];
    REP(i,2020) REP(j,2020){
        dist1[i][j] = INF;
        dist2[i][j] = INF;
    }
    dist1[0][0] = 0;
    dist2[0][0] = 0;
    priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int,int,int>>> q;
    q.emplace(0, 0, 0);
    while(!q.empty()){
        int h, w, d;
        tie(d, h, w) = q.top();
        q.pop();
        //cout << h << " " << w << " " << d << endl;
        REP(i,2){
            int nh = h + dx[i], nw = w + dy[i];
            if(nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
            if(dist1[nh][nw] > d + 1){
                dist1[nh][nw] = d + 1;
                q.emplace(d + 1, nh, nw);
            }
        }
    }
    q.emplace(0, 0, 0);
    while(!q.empty()){
        int h, w, d;
        tie(d, h, w) = q.top();
        q.pop();
        //cout << h << " " << w << " " << d << endl;
        REP(i,2){
            int nh = h + dx[i], nw = w + dy[i];
            if(nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
            if(fi[nh][nw] == 'k'){
                if(dist2[nh][nw] > d + 1 + dist1[nh][nw]){
                    dist2[nh][nw] =  d + 1 + dist1[nh][nw];
                    q.emplace(dist2[nh][nw], nh, nw);
                }
            }
            else{
                if(dist2[nh][nw] > d + 1){
                    dist2[nh][nw] = d + 1;
                    q.emplace(dist2[nh][nw], nh, nw);
                }
            }
        }
    }
    /*REP(i,H) REP(j,W){
        if(fi[i+1][j] == 'k') chmin(dist2[i+1][j], 2 * dist1[i][j] +1);
        else if(fi[i][j+1] == 'k') chmin(dist2[i][j+1], 2 * dist1[i][j] +1);
        else{
            chmin(dist2[i+1][j], dist2[i][j] + 1);
            chmin(dist2[i][j+1], dist2[i][j] + 1);
        }
    }
    */
    cout << dist2[H-1][W-1] << endl;
}
0