BOJ::2589 보물섬
https://www.acmicpc.net/problem/2589
육지중 가장 먼거리의 최단경로를 찾으면 된다.
특정 포인트만 찾아내 bfs 해보려 했으나, 굳이 그럴 필요 없을거 같아 모든 육지에 대해 bfs 돌려서 최대값을 찾았다. (어차피 bfs로 탐색하면 최단경로를 찾기 때문)
<JAVA>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; import java.util.StringTokenizer; public class Main { static int n,m,x,y,ax,ay; static int map[][]=new int[50][50]; static int max=0; static int dx[]={1,-1,0,0}; static int dy[]={0,0,1,-1}; static int tmp[][]; public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); ArrayList<Node> points = new ArrayList(); n = Integer.parseInt(st.nextToken()); m = Integer.parseInt(st.nextToken()); for(int i = 0; i < n ; i++){ String s=br.readLine(); for(int j = 0 ; j < m ; j++){ map[i][j]=s.charAt(j); } } for(int i = 0 ; i < n ; i ++){ for(int j = 0 ; j < m ; j++){ if(map[i][j]=='L'){ tmp=new int[n][m]; bfs(i,j); } } } System.out.println(max); } public static void bfs(int a, int b){ Queue<Node> q = new LinkedList(); q.add(new Node(a,b)); while(!q.isEmpty()){ x = q.peek().x; y = q.poll().y; for(int i = 0 ; i < 4 ; i++){ ax = x+dx[i]; ay = y+dy[i]; if(ax>=0&&ay>=0&&ax<n&&ay<m){ if(map[ax][ay]=='L'){ if(tmp[ax][ay]==0||tmp[ax][ay]>tmp[x][y]+1){ tmp[ax][ay]=tmp[x][y]+1; if(tmp[ax][ay]>max){ max = tmp[ax][ay]; } q.add(new Node(ax,ay)); } } } } } } } class Node{ int x; int y; Node(int _x, int _y){ x=_x; y=_y; } } | cs |
<C++>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | #include <iostream> #include <queue> #include <string> using namespace std; int n, m, x, y, ax, ay,res; int dx[] = { 1,-1,0,0 }; int dy[] = { 0,0,1,-1 }; int map[50][50]; int tmp[50][50]; struct Node { int x; int y; Node(int _x,int _y) { x = _x; y = _y; } }; void bfs(int a, int b) { queue<Node> q; q.push(Node(a, b)); while (!q.empty()) { x = q.front().x; y = q.front().y; q.pop(); for (int i = 0; i < 4; i++) { ax = x + dx[i]; ay = y + dy[i]; if (x >= 0 && y >= 0 && ax < n&&ay < m) { if (map[ax][ay] == 'L') { if (tmp[ax][ay] == 0 || tmp[ax][ay] > tmp[x][y] + 1) { tmp[ax][ay] = tmp[x][y] + 1; if (res < tmp[ax][ay]) { res = tmp[ax][ay]; } q.push(Node(ax,ay)); } } } } } } int main() { cin >> n >> m; for (int i = 0; i < n; i++) { string s; cin >> s; for (int j = 0; j < m; j++) { map[i][j] = s.at(j); } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (map[i][j] == 'L') { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { tmp[i][j] = 0; } } bfs(i, j); } } } cout << res << endl; } | cs |
'BOJ::문제풀이' 카테고리의 다른 글
2668 숫자고르기 (0) | 2018.01.06 |
---|---|
2667 단지 번호 붙이기 (0) | 2018.01.05 |
2579 계단 오르기 (0) | 2018.01.04 |
2573 빙산 (0) | 2018.01.01 |
2563 색종이 (0) | 2018.01.01 |