Submission #2472085


Source Code Expand

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

using namespace std;

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

    string S[H];
    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 INF = 1 << 30;
    vector<vector<int>> d(H, vector<int>(W, INF));

    queue<pair<int, int>> q;

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

    int dx[4] = {0, -1, 1, 0};
    int dy[4] = {-1, 0, 0, 1};

    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 (nx >= 0 && nx < H && ny >= 0 && ny < W) {
                if (S[nx][ny] == '.' && d[x][y] + 1 < d[nx][ny]) {
                    d[nx][ny] = d[x][y] + 1;
                    q.push({nx, ny});
                }
            }
        }
    }
  
    if (d[H - 1][W - 1] == INF) {
        // 訪れなかった=ゴール不可能
        cout << -1 << endl;
    } else {
        cout << wnum - d[H - 1][W - 1] << endl;
    }

    return 0;
}

Submission Info

Submission Time
Task D - Grid Repainting
User Misteer
Language C++14 (GCC 5.4.1)
Score 400
Code Size 1324 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