結果
| 問題 |
No.157 2つの空洞
|
| コンテスト | |
| ユーザー |
hiyokko2
|
| 提出日時 | 2015-02-27 00:03:54 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 2,305 bytes |
| コンパイル時間 | 924 ms |
| コンパイル使用メモリ | 92,188 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-06-23 21:50:07 |
| 合計ジャッジ時間 | 1,617 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 16 |
コンパイルメッセージ
main.cpp: In function ‘int bfs()’:
main.cpp:87:1: warning: control reaches end of non-void function [-Wreturn-type]
87 | }
| ^
ソースコード
#include <iostream>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <math.h>
#include <map>
#include <functional>
#include <iomanip>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <typeinfo>
#define PI 3.14159265359
#define INF 99999999
#define rep(i, n) for(int i=0; i<n; i++)
#define REP(n) rep(i, n)
#define EPS 1e-10
#define pb push_back
#define mp make_pair
typedef long long ll;
using namespace std;
typedef pair<int, int> P;
bool isLeap(int y);
ll gcd(ll a, ll b);
ll lcm(ll a, ll b);
double distanceAB(double xa, double ya, double xb, double yb);
void trace(int A[], int N);
/*
class Target
{
public:
vector <string> draw(int n)
{
}
};
*/
int W, H;
queue<P> que;
char C[20][20];
int Ci[20][20];
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
void dfs(int h, int w)
{
C[h][w] = '*';
que.push(P(h, w));
Ci[h][w] = 0;
rep(i, 4)
{
int nx = w + dx[i];
int ny = h + dy[i];
if (0 <= nx && nx < W && 0 <= ny && ny < H && C[ny][nx] == '.')
{
dfs(ny, nx);
}
}
}
int bfs()
{
while (que.size())
{
P p = que.front(); que.pop();
rep(i, 4)
{
int nx = p.second + dx[i];
int ny = p.first + dy[i];
if (C[ny][nx] == '.')
{
return Ci[p.first][p.second];
}
if (0 <= nx && nx < W && 0 <= ny && ny < H && C[ny][nx] == '#' && Ci[ny][nx] == INF)
{
que.push(P(ny, nx));
Ci[ny][nx] = Ci[p.first][p.second] + 1;
}
}
}
}
int main()
{
cin >> W >> H;
rep(i, H) rep(j, W) cin >> C[i][j];
rep(i, H) rep(j, W) Ci[i][j] = INF;
bool flag = false;
rep(i, H)
{
rep(j, W)
{
if (C[i][j] == '.')
{
dfs(i, j);
flag = true;
break;
}
}
if (flag) break;
}
int ans = bfs();
cout << ans << endl;
/*
rep(i, H)
{
rep(j, W)
{
cout << C[i][j];
}
cout << endl;
}
*/
return 0;
}
bool isLeap(int y)
{
return y % 400 == 0
? true : y % 100 == 0
? false : y % 4 == 0
? true : false;
}
ll gcd(ll a, ll b)
{
return (b > 0) ? gcd(b, a % b) : a;
}
ll lcm(ll a, ll b)
{
return a / gcd(a, b) * b;
}
void trace(int A[], int N)
{
REP(N)
{
if (i > 0) cout << " ";
cout << A[i];
}
cout << endl;
}
double distanceAB(double xa, double ya, double xb, double yb)
{
return sqrt((xb-xa)*(xb-xa) + (yb-ya)*(yb-ya));
}
hiyokko2