Guest User

Untitled

a guest
Oct 16th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. /*
  2. 4*4 어느 한점에서 폭탄이 터지면 인접한 폭탄이 점화 된다.
  3. 시한 폭탄이 모두 터지는 시간을 구해라.
  4.  
  5. 각 지점에는 시간이 있고, 그 시간이 지나면 폭탄이 점화되게 된다.
  6.  
  7. 0인 부분은 폭탄이 없기 때문에 0이 아닌지점 - 시작 폭탄 지점 부터 시작을 한다.
  8. check부분에 전부 max를 넣어주고,
  9. 큐에 들어 있는 check시간 + map의 현재 시간을 더했을 때
  10. 현재 check의 시간이 더 적으면, 먼저 점화 되었기 때문에 적은 시간에 맞게 폭탄이 터지게 된다!!!
  11.  
  12. 더 작을 경우에만 큐에 넣고 돌려줘야 무한루프에 빠지지 않는다.
  13.  
  14.  
  15. 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
  16. 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
  17. */
  18.  
  19. package ad;
  20.  
  21. import java.awt.Point;
  22. import java.util.LinkedList;
  23. import java.util.Queue;
  24. import java.util.Scanner;
  25.  
  26. public class ad20150523 {
  27. static int[][] map;
  28. static int[][] check;
  29. static int[][] bumb;
  30. static int N,M;
  31. static Queue<Point> q = new LinkedList<Point>();
  32.  
  33. public static void main(String[] args) {
  34. Scanner sc=new Scanner(System.in);
  35.  
  36. N=sc.nextInt();//폭탄의 갯수
  37. M=sc.nextInt();//점화되는 폭탄의 갯수
  38.  
  39. map=new int[4+1][4+1];
  40. check=new int[4+1][4+1];
  41. bumb=new int[M][2];
  42.  
  43. for(int i=0; i<M; i++){
  44. int x=sc.nextInt();
  45. int y=sc.nextInt();
  46. q.add(new Point(x,y));
  47.  
  48. bumb[i][0]=x;
  49. bumb[i][1]=y;
  50. }
  51.  
  52. for(int i=0; i<N; i++){
  53. int x=sc.nextInt();
  54. int y=sc.nextInt();
  55. int t=sc.nextInt();
  56.  
  57. map[x][y]=t;
  58. }
  59. for(int i=1; i<=4; i++){
  60. for(int j=1; j<=4; j++){
  61. check[i][j]=Integer.MAX_VALUE;
  62. }
  63. }
  64. for(int i=0; i<M; i++){
  65. check[bumb[i][0]][bumb[i][1]]=map[bumb[i][0]][bumb[i][1]];
  66. }
  67.  
  68. BFS();
  69.  
  70. int max=0;
  71. int noB=0;
  72. for(int i=1; i<5; i++){
  73. for(int j=1; j<5; j++){
  74. if(check[i][j] != Integer.MAX_VALUE && map[i][j] != 0 && check[i][j] > max){
  75. max=check[i][j];
  76. }else if(check[i][j] == Integer.MAX_VALUE && map[i][j] != 0 && map[i][j] > noB){
  77. noB=map[i][j];
  78. }
  79. }
  80. }
  81.  
  82. System.out.println(max+noB);
  83. }
  84. static int[] dx={-1,0,1,0};
  85. static int[] dy={0,-1,0,1};
  86. private static void BFS() {
  87. while(!q.isEmpty()){
  88. Point p=q.remove();
  89. for(int i=0; i<4; i++){
  90. int x=dx[i]+p.x;
  91. int y=dy[i]+p.y;
  92.  
  93. if(x>=1 && y>=1 && x<=4 && y<=4 && map[x][y] != 0){
  94. if(check[x][y] > check[p.x][p.y]+map[x][y]){
  95. check[x][y]=check[p.x][p.y]+map[x][y];
  96.  
  97. q.add(new Point(x,y));
  98. }
  99. }
  100. }
  101. }
  102. }
  103. }
Add Comment
Please, Sign In to add comment