on
[SWEA] 1953:탈주범 검거 (Java 자바)
[SWEA] 1953:탈주범 검거 (Java 자바)
반응형
풀이 및 소스코드
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.Queue; import java.util.StringTokenizer; public class Solution { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb = new StringBuilder(); StringTokenizer st; int t = Integer.parseInt(br.readLine()); for (int tt = 1; tt <= t; tt++) { st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); int r = Integer.parseInt(st.nextToken()); int c = Integer.parseInt(st.nextToken()); int l = Integer.parseInt(st.nextToken()); int[][] map = new int[n][m]; boolean[][] v = new boolean[n][m]; for(int i=0;i
"); continue; } int[][] dx = {{0}, {0, 0, -1, 1}, {-1, 1}, {0, 0}, {-1,0}, {1, 0}, {1, 0}, {-1, 0}}; int[][] dy = {{0}, {-1, 1, 0, 0}, {0, 0}, {-1, 1}, {0, 1}, {0, 1}, {0, -1}, {0, -1}}; Queue q = new LinkedList(); v[r][c] = true; q.add(new node(r, c)); int time = 1; //경과하는 시간을 담는 변수 int cnt =1; //탈주범이 위치할 수 있는 장소의 개수를 담는 변수 while(!q.isEmpty()) { int s = q.size(); //bfs로 돌리기 위해. 현재 시간에 봐야할 터널의 개수 for(int ss=0;ssnowx||0>nowy||n<=nowx||m<=nowy) continue; //범위를 벗어나면 패스 if(map[nowx][nowy]==0) continue; //터널이 없으면 패스 if(!v[nowx][nowy]) { boolean check = false; //연결된 터널인지 확인하는 것 if(map[nowx][nowy]==1) { check= true; }else { for(int j=0;jnx||0>ny||n<=nx||m<=ny) continue; if(nx == x && ny==y) { check = true; break; } } } if(check) { v[nowx][nowy]= true; q.add(new node(nowx, nowy)); cnt++; } // for(int a=0;a
"); } System.out.println(sb); } } class node{ int x,y; public node(int x, int y) { super(); this.x = x; this.y = y; } }
반응형
from http://jainn.tistory.com/288 by ccl(A) rewrite - 2021-09-30 19:27:16