답안 #584030

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
584030 2022-06-26T16:56:57 Z PikaChu999 Mecho (IOI09_mecho) Java 11
49 / 100
779 ms 43492 KB
/*
7 3
TTTTTTT
TGGGGGT
TGGGGGT
MGGGGGD
TGGGGGT
TGGGGGT
THHHHHT

1. Find shortest time it takes for the bees to reach every cell, and the shortest time it takes for Mecho to reach every cell (in minutes, take ceiling(steps it took for Mecho to get there (divided by) steps Mecho can take in a minute))
2. Binary search on the largest # of minutes that Mecho can eat honey for
*/
import java.util.*;
import java.io.*;

public class mecho{
  public static int n; 
  public static long s; 
  public static char[][] grid; //'T' = tree, 'G' = grass, 'M' = mecho, 'H' = beehive, 'D' = mecho's house
  //Neither bees or mecho can enter the tree cells
  //Mecho can travel s steps in 1 minute --> can reach a certain cell in ceil(steps it took to get to that cell/s) minutes, bees travel every minute 
  //Given that Mecho waited for x minutes, Mecho can visit a cell iff mecho[x][y] + x < bee[x][y] 
  public static long max = 1000000000; 
  public static int denx; 
  public static int deny; 
  public static int mechox; 
  public static int mechoy; 

  public static long[][] bees; 
  public static long[][] mecho; 

  public static int[] xc = {0,0,1,-1};
  public static int[] yc = {1,-1,0,0};
  public static void main(String[] args) throws IOException{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    StringTokenizer details = new StringTokenizer(br.readLine());
    n = Integer.parseInt(details.nextToken());
    s = Long.parseLong(details.nextToken());
    grid = new char[n][n]; 

    bees = new long[n][n]; //bees[a][b] = min # of minutes it takes for a bee to reach cell a,b
    for(long[] row : bees) Arrays.fill(row, max);
    mecho = new long[n][n]; //mecho[a][b] = before division, min # of steps it takes for mecho to reach cell a,b, after division min # of minutes (ceiling)
    for(long[] row : mecho) Arrays.fill(row, max);
    ArrayDeque<State> bq = new ArrayDeque<State>(); //bee states
    ArrayDeque<State> mq = new ArrayDeque<State>(); //mecho states

    for(int a = 0; a < n; a++){
      String row = br.readLine(); 
      for(int b = 0; b < n; b++){
        char cur = row.charAt(b);
        if(cur == 'T') grid[a][b] = 'T'; //tree
        else grid[a][b] = 'G'; //grass

        if(cur == 'M'){
          mq.add(new State(a,b,0));
          mechox = a; 
          mechoy = b; 
        }
        else if(cur == 'H') bq.add(new State(a,b,0));
        else if(cur == 'D'){
          denx = a; 
          deny = b; 
        }
      }
    }

    while(!bq.isEmpty()){
      State cur = bq.poll(); 
      if(bees[cur.x][cur.y] == max){
        bees[cur.x][cur.y] = cur.cost; 
        for(int a = 0; a < 4; a++){
          int xcoor = xc[a] + cur.x; 
          int ycoor = yc[a] + cur.y; 
          if(xcoor < 0 || ycoor < 0 || xcoor >= n || ycoor >= n || grid[xcoor][ycoor] == 'T' || bees[xcoor][ycoor] != max) continue; 
          bq.add(new State(xcoor, ycoor, cur.cost + 1));
        }
      }
    }
    while(!mq.isEmpty()){
      State cur = mq.poll(); 
      if(mecho[cur.x][cur.y] == max){
        mecho[cur.x][cur.y] = cur.cost/s; 
        for(int a = 0; a < 4; a++){
          int xcoor = xc[a] + cur.x; 
          int ycoor = yc[a] + cur.y; 
          if(xcoor < 0 || ycoor < 0 || xcoor >= n || ycoor >= n || grid[xcoor][ycoor] == 'T' || mecho[xcoor][ycoor] != max) continue; 
          mq.add(new State(xcoor, ycoor, cur.cost + 1));
        }
      }
    }

    //for(long[] row : mecho) System.out.println(Arrays.toString(row));
    //System.out.println("mecho ^ bees v");
    //for(long[] row : bees) System.out.println(Arrays.toString(row));

    long min = 0; 
    long max = n*n; //best case it takes the bees n*m time to reach Mecho/Mecho is able to evade the bees for n*m time
    while(min < max){
      long mid = (min + max + 1)/2; 
      //System.out.println(min + " " + mid + " " + max);
      if(valid(mid)) min = mid; 
      else max = mid - 1; //want to maximize answer
    }      
    if(min > 0 || valid(min)) System.out.println(min);
    else System.out.println(-1);
    br.close();
  }
  public static boolean valid(long x){
    boolean[][] seen = new boolean[n][n]; 
    ArrayDeque<Point> ad = new ArrayDeque<Point>(); 
    ad.add(new Point(mechox, mechoy));
    while(!ad.isEmpty()){
      Point cur = ad.poll(); 
      if(!seen[cur.x][cur.y]){
        seen[cur.x][cur.y] = true; 
        for(int a = 0; a < 4; a++){
          int xcoor = xc[a] + cur.x; 
          int ycoor = yc[a] + cur.y; 
          if(xcoor < 0 || ycoor < 0 || xcoor >= n || ycoor >= n || grid[xcoor][ycoor] == 'T' || mecho[xcoor][ycoor] + x >= bees[xcoor][ycoor]) continue; 
          ad.add(new Point(xcoor, ycoor));
        }
      }
    }
    return seen[denx][deny];
  }
}

class Point{
  int x; 
  int y; 
  public Point(int xc, int yc){
    x = xc; 
    y = yc; 
  }
  public String toString(){
    return x + " " + y; 
  }
}

class State{
  int x; 
  int y; 
  long cost; 
  public State(int xc, int yc, long c){
    x = xc;
    y = yc; 
    cost = c; 
  }
  public String toString(){
    return x + " " + y + " " + cost; 
  }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 8528 KB Output is correct
2 Incorrect 60 ms 8332 KB Output isn't correct
3 Correct 71 ms 8296 KB Output is correct
4 Incorrect 64 ms 8116 KB Output isn't correct
5 Correct 66 ms 8252 KB Output is correct
6 Incorrect 60 ms 8468 KB Output isn't correct
7 Incorrect 701 ms 39072 KB Output isn't correct
8 Correct 59 ms 8404 KB Output is correct
9 Correct 63 ms 8372 KB Output is correct
10 Correct 61 ms 8432 KB Output is correct
11 Correct 62 ms 8352 KB Output is correct
12 Incorrect 70 ms 8552 KB Output isn't correct
13 Incorrect 80 ms 8636 KB Output isn't correct
14 Correct 118 ms 11204 KB Output is correct
15 Correct 59 ms 8416 KB Output is correct
16 Correct 65 ms 8392 KB Output is correct
17 Correct 63 ms 8280 KB Output is correct
18 Correct 70 ms 8384 KB Output is correct
19 Correct 62 ms 8368 KB Output is correct
20 Correct 65 ms 8336 KB Output is correct
21 Correct 63 ms 8452 KB Output is correct
22 Correct 64 ms 8636 KB Output is correct
23 Correct 74 ms 8576 KB Output is correct
24 Correct 85 ms 8232 KB Output is correct
25 Correct 76 ms 8552 KB Output is correct
26 Correct 88 ms 10520 KB Output is correct
27 Correct 73 ms 8924 KB Output is correct
28 Correct 93 ms 10588 KB Output is correct
29 Correct 92 ms 10556 KB Output is correct
30 Correct 105 ms 10616 KB Output is correct
31 Correct 121 ms 10768 KB Output is correct
32 Correct 102 ms 10768 KB Output is correct
33 Correct 251 ms 17192 KB Output is correct
34 Correct 275 ms 17452 KB Output is correct
35 Incorrect 341 ms 18404 KB Output isn't correct
36 Correct 271 ms 17944 KB Output is correct
37 Correct 282 ms 18140 KB Output is correct
38 Incorrect 396 ms 19472 KB Output isn't correct
39 Correct 266 ms 18944 KB Output is correct
40 Correct 261 ms 18960 KB Output is correct
41 Incorrect 460 ms 20964 KB Output isn't correct
42 Correct 265 ms 19640 KB Output is correct
43 Correct 293 ms 19848 KB Output is correct
44 Incorrect 441 ms 23616 KB Output isn't correct
45 Correct 278 ms 22712 KB Output is correct
46 Correct 290 ms 22736 KB Output is correct
47 Incorrect 537 ms 25020 KB Output isn't correct
48 Correct 278 ms 24072 KB Output is correct
49 Correct 279 ms 24192 KB Output is correct
50 Incorrect 515 ms 26940 KB Output isn't correct
51 Correct 296 ms 24712 KB Output is correct
52 Correct 321 ms 25156 KB Output is correct
53 Incorrect 610 ms 29536 KB Output isn't correct
54 Correct 288 ms 26200 KB Output is correct
55 Correct 295 ms 26484 KB Output is correct
56 Incorrect 630 ms 32912 KB Output isn't correct
57 Correct 297 ms 30664 KB Output is correct
58 Correct 320 ms 31104 KB Output is correct
59 Incorrect 702 ms 35928 KB Output isn't correct
60 Correct 312 ms 33024 KB Output is correct
61 Correct 351 ms 33268 KB Output is correct
62 Incorrect 756 ms 38744 KB Output isn't correct
63 Correct 609 ms 33192 KB Output is correct
64 Correct 716 ms 38332 KB Output is correct
65 Correct 646 ms 34788 KB Output is correct
66 Correct 616 ms 34748 KB Output is correct
67 Correct 658 ms 34072 KB Output is correct
68 Correct 455 ms 33088 KB Output is correct
69 Correct 454 ms 32984 KB Output is correct
70 Correct 463 ms 33096 KB Output is correct
71 Correct 451 ms 33096 KB Output is correct
72 Incorrect 410 ms 32064 KB Output isn't correct
73 Incorrect 430 ms 38292 KB Output isn't correct
74 Correct 537 ms 41292 KB Output is correct
75 Correct 645 ms 43492 KB Output is correct
76 Correct 643 ms 42800 KB Output is correct
77 Correct 631 ms 43412 KB Output is correct
78 Correct 617 ms 41328 KB Output is correct
79 Correct 495 ms 38996 KB Output is correct
80 Correct 560 ms 40072 KB Output is correct
81 Correct 577 ms 41012 KB Output is correct
82 Correct 630 ms 41360 KB Output is correct
83 Correct 685 ms 42420 KB Output is correct
84 Correct 633 ms 43332 KB Output is correct
85 Correct 683 ms 42584 KB Output is correct
86 Correct 721 ms 42628 KB Output is correct
87 Correct 691 ms 42388 KB Output is correct
88 Correct 679 ms 40524 KB Output is correct
89 Correct 779 ms 40492 KB Output is correct
90 Correct 751 ms 40784 KB Output is correct
91 Correct 709 ms 39748 KB Output is correct
92 Correct 673 ms 39916 KB Output is correct