結果

問題 No.157 2つの空洞
ユーザー hiyokko2hiyokko2
提出日時 2015-02-27 00:03:54
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,305 bytes
コンパイル時間 765 ms
コンパイル使用メモリ 91,476 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-06 02:57:42
合計ジャッジ時間 1,714 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int bfs()’:
main.cpp:87:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^

ソースコード

diff #

#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));
}
0