Submission #2472133


Source Code Expand

Copy
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int H, W;
string S[50];

const int INF = 1 << 30;
const int dx[4] = {0, -1, 1, 0};
const int dy[4] = {-1, 0, 0, 1};

// 境界判定用関数
bool inarea(int x, int y) {
    return x >= 0 && x < H && y >= 0 && y < W;
}

// 幅優先探索部分を抽出
int bfs() {
    vector<vector<int>> d(H, vector<int>(W, INF));
    queue<pair<int, int>> q;

    d[0][0] = 1;
    q.push({0, 0});

    while (!q.empty()) {
        pair<int, int> p = q.front();
        int x = p.first;
        int y = p.second;

        q.pop();

        for (int i = 0; i < 4; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];

            if (!inarea(nx, ny)) continue;
            // ネストを減らす

            if (S[nx][ny] == '.' && d[x][y] + 1 < d[nx][ny]) {
                d[nx][ny] = d[x][y] + 1;
                q.push({nx, ny});
            }
        }
    }

    return d[H - 1][W - 1];
}

int main() {
    cin >> H >> W;

    int wnum = 0;
    for (int i = 0; i < H; i++) {
        cin >> S[i];
        for (int j = 0; j < W; j++) {
            if (S[i][j] == '.') wnum++;
        }
    }

    int d = bfs();

    if (d == INF) {
        cout << -1 << endl;
    } else {
        cout << wnum - d << endl;
    }

    return 0;
}

Submission Info

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

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 s1.txt, s2.txt
All 400 / 400 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 1 ms 256 KB
in02.txt 1 ms 256 KB
in03.txt 1 ms 256 KB
in04.txt 1 ms 256 KB
in05.txt 1 ms 256 KB
in06.txt 1 ms 256 KB
in07.txt 1 ms 256 KB
in08.txt 1 ms 256 KB
in09.txt 1 ms 256 KB
in10.txt 1 ms 256 KB
in11.txt 1 ms 256 KB
in12.txt 1 ms 256 KB
in13.txt 1 ms 256 KB
in14.txt 1 ms 256 KB
in15.txt 1 ms 256 KB
in16.txt 1 ms 256 KB
in17.txt 1 ms 256 KB
in18.txt 1 ms 256 KB
s1.txt 1 ms 256 KB
s2.txt 1 ms 256 KB