답안 #131468

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
131468 2019-07-17T07:35:48 Z dragonslayerit Mecho (IOI09_mecho) C++14
100 / 100
230 ms 7804 KB
#include <cstdio>
#include <queue>
#include <tuple>

const int INF=1e9+7;

const int dx[4]={0,1,0,-1};
const int dy[4]={1,0,-1,0};

char grid[1005][1005];
int danger[1005][1005];

int dist[1005][1005];
int N,S;

void dump(int vs[1005][1005]){
  for(int x=0;x<N;x++){
    for(int y=0;y<N;y++){
      if(vs[x][y]!=INF){
	printf("%d",vs[x][y]);
      }else{
	printf("-");
      }
    }
    printf("\n");
  }
  printf("\n");
}

bool check(int start){
  //printf("check(%d)\n",start);
  for(int x=0;x<N;x++){
    for(int y=0;y<N;y++){
      dist[x][y]=INF;
    }
  }
  std::queue<std::pair<int,int> > mecho;
  for(int x=0;x<N;x++){
    for(int y=0;y<N;y++){
      if(grid[x][y]=='M'){
	dist[x][y]=0;
	mecho.push({x,y});
      }
    }
  }
  while(!mecho.empty()){
    int x,y;
    std::tie(x,y)=mecho.front();
    mecho.pop();
    if(dist[x][y]/S+start>=danger[x][y]) continue;
    if(grid[x][y]=='D'){
      //dump(dist);
      return true;
    }
    for(int d=0;d<4;d++){
      int x2=x+dx[d],y2=y+dy[d];
      if((grid[x2][y2]=='G'||grid[x2][y2]=='D')&&dist[x2][y2]==INF){
	dist[x2][y2]=dist[x][y]+1;
	mecho.push({x2,y2});
      }
    }
  }
  //dump(dist);
  return false;
}

int main(){
  scanf("%d %d",&N,&S);
  for(int x=0;x<N;x++){
    scanf("%s",grid[x]);
  }
  for(int x=0;x<N;x++){
    for(int y=0;y<N;y++){
      danger[x][y]=INF;
    }
  }
  std::queue<std::pair<int,int> > bees;
  for(int x=0;x<N;x++){
    for(int y=0;y<N;y++){
      if(grid[x][y]=='H'){
	danger[x][y]=0;
	bees.push({x,y});
      }
    }
  }
  while(!bees.empty()){
    int x,y;
    std::tie(x,y)=bees.front();
    bees.pop();
    for(int d=0;d<4;d++){
      int x2=x+dx[d],y2=y+dy[d];
      if((grid[x2][y2]=='G'||grid[x2][y2]=='M')&&danger[x2][y2]==INF){
	danger[x2][y2]=danger[x][y]+1;
	bees.push({x2,y2});
      }
    }
  }
  //dump(danger);
  int low=-1,high=N*N;
  while(high-low>1){
    int mid=(low+high)/2;
    if(check(mid)){
      low=mid;
    }else{
      high=mid;
    }
  }
  printf("%d\n",low);
  return 0;
}

Compilation message

mecho.cpp: In function 'int main()':
mecho.cpp:68:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&N,&S);
   ~~~~~^~~~~~~~~~~~~~~
mecho.cpp:70:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%s",grid[x]);
     ~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 380 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 111 ms 7544 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 504 KB Output is correct
12 Correct 2 ms 888 KB Output is correct
13 Correct 2 ms 760 KB Output is correct
14 Correct 3 ms 936 KB Output is correct
15 Correct 2 ms 504 KB Output is correct
16 Correct 2 ms 504 KB Output is correct
17 Correct 2 ms 504 KB Output is correct
18 Correct 2 ms 504 KB Output is correct
19 Correct 1 ms 632 KB Output is correct
20 Correct 2 ms 632 KB Output is correct
21 Correct 2 ms 632 KB Output is correct
22 Correct 2 ms 632 KB Output is correct
23 Correct 3 ms 764 KB Output is correct
24 Correct 2 ms 684 KB Output is correct
25 Correct 2 ms 888 KB Output is correct
26 Correct 2 ms 760 KB Output is correct
27 Correct 2 ms 888 KB Output is correct
28 Correct 2 ms 888 KB Output is correct
29 Correct 3 ms 888 KB Output is correct
30 Correct 3 ms 888 KB Output is correct
31 Correct 3 ms 1016 KB Output is correct
32 Correct 3 ms 1016 KB Output is correct
33 Correct 9 ms 3448 KB Output is correct
34 Correct 9 ms 3448 KB Output is correct
35 Correct 25 ms 3448 KB Output is correct
36 Correct 11 ms 3832 KB Output is correct
37 Correct 12 ms 3832 KB Output is correct
38 Correct 34 ms 3960 KB Output is correct
39 Correct 14 ms 4344 KB Output is correct
40 Correct 13 ms 4348 KB Output is correct
41 Correct 53 ms 4348 KB Output is correct
42 Correct 16 ms 4728 KB Output is correct
43 Correct 15 ms 4728 KB Output is correct
44 Correct 57 ms 4728 KB Output is correct
45 Correct 18 ms 5112 KB Output is correct
46 Correct 18 ms 5140 KB Output is correct
47 Correct 63 ms 5112 KB Output is correct
48 Correct 21 ms 5624 KB Output is correct
49 Correct 21 ms 5624 KB Output is correct
50 Correct 80 ms 5624 KB Output is correct
51 Correct 24 ms 6136 KB Output is correct
52 Correct 24 ms 6008 KB Output is correct
53 Correct 119 ms 6148 KB Output is correct
54 Correct 27 ms 6520 KB Output is correct
55 Correct 27 ms 6520 KB Output is correct
56 Correct 127 ms 6524 KB Output is correct
57 Correct 31 ms 7024 KB Output is correct
58 Correct 30 ms 6904 KB Output is correct
59 Correct 144 ms 7004 KB Output is correct
60 Correct 39 ms 7416 KB Output is correct
61 Correct 37 ms 7416 KB Output is correct
62 Correct 166 ms 7544 KB Output is correct
63 Correct 125 ms 7416 KB Output is correct
64 Correct 230 ms 7416 KB Output is correct
65 Correct 173 ms 7416 KB Output is correct
66 Correct 144 ms 7416 KB Output is correct
67 Correct 142 ms 7444 KB Output is correct
68 Correct 67 ms 7544 KB Output is correct
69 Correct 66 ms 7388 KB Output is correct
70 Correct 60 ms 7416 KB Output is correct
71 Correct 61 ms 7544 KB Output is correct
72 Correct 48 ms 7464 KB Output is correct
73 Correct 53 ms 7672 KB Output is correct
74 Correct 78 ms 7804 KB Output is correct
75 Correct 94 ms 7672 KB Output is correct
76 Correct 82 ms 7644 KB Output is correct
77 Correct 82 ms 7672 KB Output is correct
78 Correct 97 ms 7672 KB Output is correct
79 Correct 66 ms 7672 KB Output is correct
80 Correct 78 ms 7672 KB Output is correct
81 Correct 89 ms 7612 KB Output is correct
82 Correct 78 ms 7672 KB Output is correct
83 Correct 122 ms 7656 KB Output is correct
84 Correct 98 ms 7672 KB Output is correct
85 Correct 114 ms 7676 KB Output is correct
86 Correct 110 ms 7672 KB Output is correct
87 Correct 128 ms 7800 KB Output is correct
88 Correct 134 ms 7544 KB Output is correct
89 Correct 139 ms 7544 KB Output is correct
90 Correct 116 ms 7544 KB Output is correct
91 Correct 126 ms 7596 KB Output is correct
92 Correct 149 ms 7672 KB Output is correct