結果

問題 No.2064 Smallest Sequence on Grid
ユーザー koryoukoryou
提出日時 2022-09-02 22:13:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,329 bytes
コンパイル時間 1,054 ms
コンパイル使用メモリ 112,104 KB
実行使用メモリ 294,824 KB
最終ジャッジ日時 2024-04-27 21:48:18
合計ジャッジ時間 11,434 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,636 KB
testcase_01 AC 2 ms
7,632 KB
testcase_02 AC 2 ms
7,760 KB
testcase_03 AC 2 ms
7,628 KB
testcase_04 AC 2 ms
7,648 KB
testcase_05 AC 2 ms
7,624 KB
testcase_06 AC 2 ms
7,636 KB
testcase_07 AC 1 ms
7,628 KB
testcase_08 AC 3 ms
11,856 KB
testcase_09 WA -
testcase_10 AC 3 ms
12,072 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 605 ms
294,508 KB
testcase_21 AC 626 ms
294,508 KB
testcase_22 AC 611 ms
294,504 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 594 ms
294,252 KB
testcase_27 WA -
testcase_28 AC 594 ms
294,512 KB
testcase_29 AC 498 ms
272,028 KB
testcase_30 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <string>
#include <list>
#include <cassert>
#include <numeric>
#include <cstdint>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <random>
#include <bitset>
// #include <atcoder/all>
 
using ll = long long;
using ld = long double;
using namespace std;
// using namespace atcoder;
using P = pair<ll, ll>;
using Graph = vector<vector<int>>;
using Priority = priority_queue<ll, vector<ll>, greater<ll>>;// 昇順
using Priority_pair = priority_queue<P, vector<P>, greater<P>>;
// using mint_17 = modint1000000007;
// using mint = modint998244353;

#define mod 1000000007
#define MAX_WIDTH 60
#define MAX_HEIGHT 60
#define INF 1e18
#define MOD 998244353
#define PI 3.141592653589793
#define rep(i, a, b) for(ll i=(a);i<(b);i++)
#define rrep(i, a, b) for(ll i=(a);i>=(b);i--)
#define fore(i, a) for(auto &i: a)
#define all(v) (v).begin(), (v).end()
#define YES(a) cout << ((a) ? "YES" : "NO") << endl
#define Yes(a) cout << ((a) ? "Yes" : "No") << endl
#define pb(a) push_back(a)
#define vi vector<int>
#define vll vector<ll>
#define vs vector<string>
#define vc vector<char>
#define vp vector<pair<ll, ll>>
#define mll map<ll, ll>
#define msl map<string, ll>
#define COUT(n) cout << (n) << endl
#define isInGrid(a, b, h, w) (0 <= (a) && (a) < (h) && 0 <= (b) && (b) < (w))
template<typename T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<typename T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
int vx[] = {1, 0, -1, 0}, vy[] = {0, 1, 0, -1};
struct edge {
    ll to, cost;
};

ll H, W, G[3010][3010];
ll dp[3010][3010];
char C[3010][3010];
int main() {
    cin >> H >> W;
    rep(i,0,H) {
        rep(j,0,W) {
            cin >> C[i][j];
            ll n = C[i][j]-'a'+1;
            G[i][j] = n;
        }
    }

    rep(i,0,H+1) rep(j,0,W+1) dp[i][j] = INF;

    vector<vector<pair<ll, ll>>> prev(H, vector<pair<ll, ll>>(W, make_pair(0, 0)));
    dp[H-1][W-1] = G[H-1][W-1];
    for(int i=H-1;i>=0;i--) {
        for(int j=W-1;j>=0;j--) {
            if(i-1 >= 0 && chmin(dp[i-1][j], dp[i][j]+G[i-1][j])) {
                if(j+1 < W) {
                    if(G[i][j] <= G[i-1][j+1]) {
                        prev[i-1][j] = make_pair(i, j);
                    } else {
                        prev[i-1][j] = make_pair(i-1, j+1);
                    }
                } else {
                    prev[i-1][j] = make_pair(i, j);
                }
            }

            if(j-1 >= 0 && chmin(dp[i][j-1], dp[i][j]+G[i][j-1])) {
                if(i+1 < H) {
                    if(G[i][j] <= G[i+1][j-1]) {
                        prev[i][j-1] = make_pair(i, j);
                    } else {
                        prev[i][j-1] = make_pair(i+1, j-1);
                    }
                } else {
                    prev[i][j-1] = make_pair(i, j);
                }
            }
        }
    }

    ll now_h = 0, now_w = 0;
    string ans;
    while(1) {
        ans = ans + C[now_h][now_w];
        if(now_h == H-1 && now_w == W-1) {
            break;
        }
        pair<ll, ll> p = prev[now_h][now_w];
        now_h = p.first, now_w = p.second;
    }
    COUT(ans);
   
    return 0;
}
0