結果

問題 No.157 2つの空洞
ユーザー HachimoriHachimori
提出日時 2015-03-03 23:51:19
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,685 bytes
コンパイル時間 663 ms
コンパイル使用メモリ 66,564 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-06 06:38:49
合計ジャッジ時間 1,606 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 2 ms
4,384 KB
testcase_19 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<queue>
using namespace std;
const int BUF = 25;
const int INF = 1<<20;


class QData{
public:
    int r, c, cost;
    QData(){}
    QData(int r, int c, int cost):
        r(r), c(c), cost(cost){}

    bool operator< (const QData &opp) const {
        return cost > opp.cost;
    }
};


int row, col;
string b[BUF];

void read() {
    cin >> col >> row;
    for (int i = 0; i < row; ++i)
        cin >> b[i];
}


void work() {
    priority_queue<QData> Q;
    int cost[BUF][BUF];
    
    for (int i = 0; i < BUF; ++i)
        for (int j = 0; j < BUF; ++j)
            cost[i][j] = INF;
    
    for (int i = 0; i < row; ++i)
        for (int j = 0; j < col; ++j) {
            if (b[i][j] == '.') {
                cost[i][j] = 0;
                Q.push(QData(i, j, 0));
                goto _finish;
            }
        }
    
 _finish:

    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    
    while (!Q.empty()) {
        QData curr = Q.top();
        Q.pop();

        if (curr.cost > 0 && b[curr.r][curr.c] == '.') {
            cout << curr.cost << endl;
            break;
        }
        
        for (int i = 0; i < 4; ++i) {
            int nr = curr.r + dr[i];
            int nc = curr.c + dc[i];
            
            if (!(0 <= nr && nr < row && 0 <= nc && nc < col))
                continue;
            
            int nexCost = curr.cost + (b[nr][nc] != '.');
            
            if (cost[nr][nc] > nexCost) {
                cost[nr][nc] = nexCost;
                Q.push(QData(nr, nc, nexCost));
            }
        }
    }
}


int main() {
    read();
    work();
    return 0;
}
0