結果
| 問題 |
No.157 2つの空洞
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2015-03-03 23:51:19 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 1,685 bytes |
| コンパイル時間 | 574 ms |
| コンパイル使用メモリ | 66,328 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-06-24 01:18:31 |
| 合計ジャッジ時間 | 1,331 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 16 |
ソースコード
#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;
}