Submission #2109769


Source Code Expand

Copy
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
/*---------------------------------------------------------------------------------------------------
            ∧_∧  
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     
    /   \     | |     
    /   / ̄ ̄ ̄ ̄/  |  
  __(__ニつ/     _/ .| .|____  
     \/____/ (u ⊃  
---------------------------------------------------------------------------------------------------*/





int H, W;
string B[50];
int dx[4] = { 0, 1, 0, -1 }, dy[4] = { -1, 0, 1, 0 };
//---------------------------------------------------------------------------------------------------
int vis[55][55];
int dp[55][55];
int check() {
    queue<pair<int, int>> que;
    que.push({ 0, 0 });
    vis[0][0] = 1;
    while (!que.empty()) {
        auto q = que.front(); que.pop();
        int x, y;
        tie(x, y) = q;

        if (x == W - 1 and y == H - 1) return dp[y][x];

        rep(d, 0, 4) {
            int xx = x + dx[d];
            int yy = y + dy[d];
            if (0 <= xx and xx < W and 0 <= yy and yy < H) {
                if (B[yy][xx] == '.' and vis[yy][xx] == 0) {
                    vis[yy][xx] = 1;
                    dp[yy][xx] = dp[y][x] + 1;
                    que.push({ xx, yy });
                }
            }
        }
    }

    return 0;
}
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> H >> W;
    rep(y, 0, H) cin >> B[y];

    int ans, res = check();
    if (!res) ans = -1;
    else {
        ans = H * W - res - 1;
        rep(y, 0, H) rep(x, 0, W) if (B[y][x] == '#') ans--;
    }

    printf("%d\n", ans);
}

Submission Info

Submission Time
Task D - Grid Repainting
User hamayanhamayan
Language C++14 (GCC 5.4.1)
Score 400
Code Size 2518 Byte
Status AC
Exec Time 1 ms
Memory 256 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 2
AC × 20
Set Name Test Cases
Sample s1.txt, s2.txt
All in01.txt, in02.txt, in03.txt, in04.txt, in05.txt, in06.txt, in07.txt, in08.txt, in09.txt, in10.txt, in11.txt, in12.txt, in13.txt, in14.txt, in15.txt, in16.txt, in17.txt, in18.txt, s1.txt, s2.txt
Case Name Status Exec Time Memory
in01.txt AC 1 ms 256 KB
in02.txt AC 1 ms 256 KB
in03.txt AC 1 ms 256 KB
in04.txt AC 1 ms 256 KB
in05.txt AC 1 ms 256 KB
in06.txt AC 1 ms 256 KB
in07.txt AC 1 ms 256 KB
in08.txt AC 1 ms 256 KB
in09.txt AC 1 ms 256 KB
in10.txt AC 1 ms 256 KB
in11.txt AC 1 ms 256 KB
in12.txt AC 1 ms 256 KB
in13.txt AC 1 ms 256 KB
in14.txt AC 1 ms 256 KB
in15.txt AC 1 ms 256 KB
in16.txt AC 1 ms 256 KB
in17.txt AC 1 ms 256 KB
in18.txt AC 1 ms 256 KB
s1.txt AC 1 ms 256 KB
s2.txt AC 1 ms 256 KB