#include <stdio.h> #include <iostream> #include <queue> #include <string.h> using namespace std; char map[50][50]; int visited[50][50]; int row, col; int Max; int dx[4] = {-1, 1, 0, 0}; int dy[4] = {0, 0, -1, 1}; int safe(int x, int y) { if(x >= 0 && y >= 0 && x < row && y < col) { return 1; } else { return 0; } } void BFS(int x, int y) { queue<pair<int, int>> q; q.push({x, y}); visited[x][y] = 1; while(!q.empty()) { x = q.front().first; y = q.front().second; q.pop(); for(int i = 0; i < 4; i++) { int xpos = x+dx[i]; int ypos = y+dy[i]; if(safe(xpos, ypos) == 1 && map[xpos][ypos] == 'L' && visited[xpos][ypos] == 0) { visited[xpos][ypos] = visited[x][y] + 1; q.push({xpos, ypos}); if(Max < visited[xpos][ypos]) { Max = visited[xpos][ypos]; } } } } } int main(void) { // freopen("B2589_input.txt", "r", stdin); cin >> row >> col; for(int i = 0; i < row; i++) { string input; cin >> input; for(int j = 0; j < col; j++) { map[i][j] = input[j]; } } for(int i = 0; i < row; i++) { for(int j = 0; j < col; j++) { if(map[i][j] == 'L') { memset(visited, 0, sizeof(visited)); BFS(i, j); } } } cout << Max-1; return 0; } | cs |
'Baekjoon > BFS' 카테고리의 다른 글
[백준 4991] 로봇 청소기 (BFS, Bitmask) (C/C++) (★★★) (0) | 2020.05.21 |
---|---|
[백준 2638] 치즈 (BFS) (C/C++) (★★★) (0) | 2020.04.07 |
[백준 1938] 통나무 옮기기 (BFS) (C/C++) (★★★) (0) | 2020.02.19 |
[백준 1261] 알고스팟 (BFS) (C/C++) (★★) (0) | 2020.02.16 |
[백준 1167] 트리의 지름 (BFS, Tree) (C/C++) (★) (0) | 2020.02.16 |