답안 #433730

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
433730 2021-06-20T10:04:06 Z Lam_lai_cuoc_doi K번째 경로 (IZhO11_kthpath) C++17
100 / 100
2 ms 332 KB
#include <bits/stdc++.h>
 
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
 
constexpr bool typetest = 0;
constexpr int N = 30 + 2;
int m, n;
string s[N];
ll dp[N][N], k, g[N][N];
 
void Read()
{
    cin >> m >> n;
 
    for (int i = 1; i <= m; ++i)
    {
        cin >> s[i];
        s[i] = " " + s[i];
    }
 
    cin >> k;
}
 
void BFS(int x, int y)
{
    queue<pair<int, int>> s;
    dp[x][y] = 1;
    s.emplace(x, y);
 
    while (s.size())
    {
        auto c = s.front();
        s.pop();
 
        if (c.first > 1)
        {
            if (!dp[c.first - 1][c.second])
                s.emplace(c.first - 1, c.second);
            dp[c.first - 1][c.second] += dp[c.first][c.second];
 
            if (dp[c.first - 1][c.second] > 1e18)
                dp[c.first - 1][c.second] = 2e18;
        }
        if (c.second > 1)
        {
            if (!dp[c.first][c.second - 1])
                s.emplace(c.first, c.second - 1);
            dp[c.first][c.second - 1] += dp[c.first][c.second];
 
            if (dp[c.first][c.second - 1] > 1e18)
                dp[c.first][c.second - 1] = 2e18;
        }
    }
}
 
ll f(int x, int y, const string &ans)
{
   	if(s[x][y] != ans[x + y - 2]) return g[x][y] = 0;
    if (g[x][y] != -1)
        return g[x][y];
 
    if (x + y - 1 == ans.size() || ans[x + y - 1] == '*')
        return dp[x][y];
 
    g[x][y] = 0;
 
    if (x < m && ans[x + y - 1] == s[x + 1][y])
        g[x][y] += f(x + 1, y, ans);
    if (y < n && ans[x + y - 1] == s[x][y + 1])
        g[x][y] += f(x, y + 1, ans);
 
    return g[x][y];
}
 
void Solve()
{
    BFS(m, n);
 
    string ans(m + n - 1, '*');
 
    for (int i = 0; i < (int)ans.size(); ++i)
        for (int j = 0; j < 26; ++j)
        {
            ans[i] = j + 'a';
 
            memset(g, -1, sizeof g);
 
            ll cnt = f(1, 1, ans);
 
            if (cnt >= k)
                break;
 
            k -= cnt;
        }
 
    cout << ans;
}
 
int32_t main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t(1);
    if (typetest)
        cin >> t;
    for (int _ = 1; _ <= t; ++_)
    {
        Read();
        Solve();
    }
}

Compilation message

kthpath.cpp: In function 'll f(int, int, const string&)':
kthpath.cpp:65:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |     if (x + y - 1 == ans.size() || ans[x + y - 1] == '*')
      |         ~~~~~~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 312 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 2 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 316 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 316 KB Output is correct
16 Correct 2 ms 204 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 312 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct