結果

問題 No.866 レベルKの正方形
ユーザー Kiona1018
提出日時 2019-09-14 13:56:27
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3,206 ms / 6,000 ms
コード長 2,250 bytes
コンパイル時間 1,933 ms
コンパイル使用メモリ 174,372 KB
実行使用メモリ 417,664 KB
最終ジャッジ日時 2024-07-05 21:30:46
合計ジャッジ時間 41,561 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define rep(i,n,m) for(int i = (n); i <(m); i++)
#define rrep(i,n,m) for(int i = (n) - 1; i >=(m); i--)
using namespace std;
using ll = long long;
const int INF = 1000000000;

int main()
{
    int h, w, k;
    cin >> h >> w >> k;

    string table[h];
    rep(i, 0, h)
        cin >> table[i];

    int dis[26][h+1][w+1];
    rep(c,0, 26)
        rep(i, 0, h + 1)
            rep(j, 0, w + 1)
                dis[c][i][j] = INF;

    rep(c, 0, 26)
    {
        char color = c + 'a';

        rrep(i, h, 0)
            rrep(j, w, 0)
            {
                if (table[i][j] == color)
                    dis[c][i][j] = 1;
                else
                {
                    int max_k = min(h-i, w-j);
                    if (dis[c][i+1][j] + 1 <= max_k)
                        dis[c][i][j] = min(dis[c][i][j], dis[c][i+1][j] + 1);
                    if (dis[c][i][j+1] + 1 <= max_k)
                        dis[c][i][j] = min(dis[c][i][j], dis[c][i][j+1] + 1);
                    if (dis[c][i+1][j+1] + 1 <= max_k)
                        dis[c][i][j] = min(dis[c][i][j], dis[c][i+1][j+1] + 1);
                }
                // if (color == 'd')
                //     cout << i << ' ' << j << ' ' << dis[c][i][j] << endl;
            }
    }

    ll ans = 0;
    rep(i, 0, h)
        rep(j, 0, w)
        {
            vector<int> nums;
            int max_k = min(h-i, w-j) + 1;
            nums.push_back(max_k);
            rep(c, 0, 26)
            {
                if (dis[c][i][j] == INF)
                    nums.push_back(max_k);
                else
                    nums.push_back(dis[c][i][j]);
            }
            sort(nums.begin(), nums.end());

            // rep(c, 0, 26)
            //     cout << nums[c] << " ";
            // cout << endl;
            // cout << "**********" << endl;
            // cout <<i << ' ' << j << ' ' <<  nums[k] << ' ' << nums[k-1] << endl;

            if (nums[k - 1] == INF) continue;
            if (nums[k] == INF)
                nums[k] = min(h - i, w - i);

            // cout <<i << ' ' << j << ' ' <<  nums[k] << ' ' << nums[k-1] << endl;
            ans += (ll) nums[k] - nums[k - 1];
        }
    cout << ans << endl;
    return 0;
}
0