Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 4*4 어느 한점에서 폭탄이 터지면 인접한 폭탄이 점화 된다.
- 시한 폭탄이 모두 터지는 시간을 구해라.
- 각 지점에는 시간이 있고, 그 시간이 지나면 폭탄이 점화되게 된다.
- 0인 부분은 폭탄이 없기 때문에 0이 아닌지점 - 시작 폭탄 지점 부터 시작을 한다.
- check부분에 전부 max를 넣어주고,
- 큐에 들어 있는 check시간 + map의 현재 시간을 더했을 때
- 현재 check의 시간이 더 적으면, 먼저 점화 되었기 때문에 적은 시간에 맞게 폭탄이 터지게 된다!!!
- 더 작을 경우에만 큐에 넣고 돌려줘야 무한루프에 빠지지 않는다.
- 10 1 1 3 1 3 4 1 4 2 2 3 3 2 4 1 3 2 5 3 3 2 3 4 3 4 2 8 4 3 6 4 4 2 출력:22
- 9 2 1 1 2 3 1 1 2 1 3 3 1 4 6 2 2 2 2 3 5 2 4 2 3 2 3 3 3 7 4 4 2 출력:15
- */
- package ad;
- import java.awt.Point;
- import java.util.LinkedList;
- import java.util.Queue;
- import java.util.Scanner;
- public class ad20150523 {
- static int[][] map;
- static int[][] check;
- static int[][] bumb;
- static int N,M;
- static Queue<Point> q = new LinkedList<Point>();
- public static void main(String[] args) {
- Scanner sc=new Scanner(System.in);
- N=sc.nextInt();//폭탄의 갯수
- M=sc.nextInt();//점화되는 폭탄의 갯수
- map=new int[4+1][4+1];
- check=new int[4+1][4+1];
- bumb=new int[M][2];
- for(int i=0; i<M; i++){
- int x=sc.nextInt();
- int y=sc.nextInt();
- q.add(new Point(x,y));
- bumb[i][0]=x;
- bumb[i][1]=y;
- }
- for(int i=0; i<N; i++){
- int x=sc.nextInt();
- int y=sc.nextInt();
- int t=sc.nextInt();
- map[x][y]=t;
- }
- for(int i=1; i<=4; i++){
- for(int j=1; j<=4; j++){
- check[i][j]=Integer.MAX_VALUE;
- }
- }
- for(int i=0; i<M; i++){
- check[bumb[i][0]][bumb[i][1]]=map[bumb[i][0]][bumb[i][1]];
- }
- BFS();
- int max=0;
- int noB=0;
- for(int i=1; i<5; i++){
- for(int j=1; j<5; j++){
- if(check[i][j] != Integer.MAX_VALUE && map[i][j] != 0 && check[i][j] > max){
- max=check[i][j];
- }else if(check[i][j] == Integer.MAX_VALUE && map[i][j] != 0 && map[i][j] > noB){
- noB=map[i][j];
- }
- }
- }
- System.out.println(max+noB);
- }
- static int[] dx={-1,0,1,0};
- static int[] dy={0,-1,0,1};
- private static void BFS() {
- while(!q.isEmpty()){
- Point p=q.remove();
- for(int i=0; i<4; i++){
- int x=dx[i]+p.x;
- int y=dy[i]+p.y;
- if(x>=1 && y>=1 && x<=4 && y<=4 && map[x][y] != 0){
- if(check[x][y] > check[p.x][p.y]+map[x][y]){
- check[x][y]=check[p.x][p.y]+map[x][y];
- q.add(new Point(x,y));
- }
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment