본문 바로가기

Programming/BOJ

[백준/C++] 2589번: 보물섬

이문제는 딱히 까다롭지 않고 모든 시작점에 대해서 bfs를 진행해서 모든 depth를 담고 max value를 print했다. 끝~

 

#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;

int N, M;
string map[51];
int visit[51][51] = { 0 };
int temp[51][51] = { 0 };
vector<int> findMax;

int dx[] = { 1,0,0,-1 };
int dy[] = { 0,1,-1,0 };
void bfs(int x, int y) {

	int out;
	queue <pair<pair<int, int>, int>> q;
	q.push(make_pair(make_pair(x, y), 0));

	while (!q.empty()) {
		int cx = q.front().first.first;
		int cy = q.front().first.second;
		int depth = q.front().second;
		out = depth;
		q.pop();

		for (int i = 0; i < 4; i++) {
			int nx = cx + dx[i];
			int ny = cy + dy[i];
			if (nx < 0 || nx >= N || ny < 0 || ny >= M)continue;
			if (map[nx][ny] == 'L' && visit[nx][ny] == 0) {
				visit[nx][ny] = 1;
				q.push(make_pair(make_pair(nx, ny), depth+1));
			}
		}

	}

	findMax.push_back(out);
	return;
}
int main() {
	cin >> N >> M;
	for (int i = 0; i < N; i++) {
		cin >> map[i];
	}

	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			if (map[i][j] == 'L' && temp[i][j]==0) {
				temp[i][j] = 1;
				//cout << i << "," << j << endl;
				visit[i][j] = 1;
				bfs(i, j);
				memset(visit, 0, sizeof(visit));
			}
		}
	}


	cout << *max_element(findMax.begin(), findMax.end());

	return 0;
}