답안 #892982

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
892982 2023-12-26T09:34:31 Z ilef Mecho (IOI09_mecho) C++14
100 / 100
156 ms 7600 KB
#include<bits/stdc++.h>
using namespace std;
 int n,s;
 bool safe(int i,int j){
     return i>=0 && i<n && j>=0 && j<n;
 }
 int di[4]={0,0,-1,1};
 int dj[4]={1,-1,0,0};
int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  
    cin>>n>>s;
    char mat[n][n];
    int dist1[n][n];
    int dist2[n][n];
    int homex,homey;
    int honx,hony;
    queue<pair<int,int>>q;
    bool vis1[n][n];
    bool vis2[n][n];
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            vis1[i][j]=false;
            vis2[i][j]=false;
            dist1[i][j]=0;
            dist2[i][j]=0;
        }
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cin>>mat[i][j];
            if(mat[i][j]=='M'){
                honx=i;
                hony=j;
            }
            if(mat[i][j]=='D'){
                homex=i;
                homey=j;
            }
            if(mat[i][j]=='H'){
                q.push({i,j});
                dist1[i][j]=0;
                vis1[i][j]=true;
            }
        }
    }
    while(!q.empty()){
        const auto &[i,j]=q.front();
        q.pop();
        for(int l=0;l<4;l++){
            int x=i+di[l];
            int y=j+dj[l];
            if(safe(x,y) && !vis1[x][y] && (mat[x][y]=='G'||mat[x][y]=='M')){
                dist1[x][y]=dist1[i][j]+1;
                vis1[x][y]=true;
                q.push({x,y});
            }
        }
    }
    //bool reached=false;
    int ll=0,rr=n*n;
    int ans=-1;
    while(ll<=rr){
        int mid=(ll+rr)/2;
          for(int i=0;i<n;i++){
          for(int j=0;j<n;j++){
            vis2[i][j]=false;
             dist2[i][j]=0;
            
        }
    }
       
        if(mid<dist1[honx][hony]){
            dist2[honx][hony]=0;
            vis2[honx][hony]=true;
            q.push({honx,hony});
        }
        while(!q.empty()){
        const auto &[i,j]=q.front();
        q.pop();
        for(int l=0;l<4;l++){
            int x=i+di[l];
            int y=j+dj[l];
                    if(safe(x,y) && (mat[x][y]=='G'|| mat[x][y]=='M')&& !vis2[x][y] &&((dist2[i][j]+1)/s<(dist1[x][y]-mid))){
                        dist2[x][y]=dist2[i][j]+1;
                        vis2[x][y]=true;
                        q.push({x,y});
                    }
                }
             
                }
    bool reached=false;
    for(int i=0;i<4;i++){
        int x=homex+di[i];
        int y=homey+dj[i];
          
        if(safe(x,y) && vis2[x][y]  && (mat[x][y]=='G'|| mat[x][y]=='M') && dist2[x][y]/s<dist1[x][y]-mid){
            reached=true;
        }
    }
            
    if(reached){
        ans=mid;
        ll=mid+1;
    }
    else{
        //ans=mid;
        rr=mid-1;
    }
    }
       cout<<ans<<'\n';
}

Compilation message

mecho.cpp: In function 'int32_t main()':
mecho.cpp:50:21: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   50 |         const auto &[i,j]=q.front();
      |                     ^
mecho.cpp:81:21: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   81 |         const auto &[i,j]=q.front();
      |                     ^
mecho.cpp:76:30: warning: 'hony' may be used uninitialized in this function [-Wmaybe-uninitialized]
   76 |             dist2[honx][hony]=0;
      |             ~~~~~~~~~~~~~~~~~^~
mecho.cpp:97:13: warning: 'homey' may be used uninitialized in this function [-Wmaybe-uninitialized]
   97 |         int y=homey+dj[i];
      |             ^
mecho.cpp:76:30: warning: 'honx' may be used uninitialized in this function [-Wmaybe-uninitialized]
   76 |             dist2[honx][hony]=0;
      |             ~~~~~~~~~~~~~~~~~^~
mecho.cpp:96:13: warning: 'homex' may be used uninitialized in this function [-Wmaybe-uninitialized]
   96 |         int x=homex+di[i];
      |             ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 67 ms 7432 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 404 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 4 ms 1628 KB Output is correct
34 Correct 4 ms 1628 KB Output is correct
35 Correct 17 ms 1780 KB Output is correct
36 Correct 5 ms 2136 KB Output is correct
37 Correct 5 ms 2140 KB Output is correct
38 Correct 26 ms 2140 KB Output is correct
39 Correct 6 ms 2392 KB Output is correct
40 Correct 7 ms 2636 KB Output is correct
41 Correct 30 ms 2392 KB Output is correct
42 Correct 9 ms 2908 KB Output is correct
43 Correct 10 ms 2908 KB Output is correct
44 Correct 41 ms 2908 KB Output is correct
45 Correct 10 ms 3720 KB Output is correct
46 Correct 10 ms 3700 KB Output is correct
47 Correct 42 ms 3692 KB Output is correct
48 Correct 11 ms 4188 KB Output is correct
49 Correct 12 ms 4184 KB Output is correct
50 Correct 50 ms 4184 KB Output is correct
51 Correct 15 ms 5000 KB Output is correct
52 Correct 15 ms 4988 KB Output is correct
53 Correct 65 ms 4984 KB Output is correct
54 Correct 15 ms 5724 KB Output is correct
55 Correct 16 ms 5724 KB Output is correct
56 Correct 85 ms 5632 KB Output is correct
57 Correct 24 ms 6508 KB Output is correct
58 Correct 20 ms 6492 KB Output is correct
59 Correct 86 ms 6492 KB Output is correct
60 Correct 19 ms 7260 KB Output is correct
61 Correct 22 ms 7340 KB Output is correct
62 Correct 96 ms 7344 KB Output is correct
63 Correct 85 ms 7256 KB Output is correct
64 Correct 156 ms 7320 KB Output is correct
65 Correct 122 ms 7256 KB Output is correct
66 Correct 105 ms 7324 KB Output is correct
67 Correct 91 ms 7256 KB Output is correct
68 Correct 41 ms 7256 KB Output is correct
69 Correct 39 ms 7260 KB Output is correct
70 Correct 30 ms 7260 KB Output is correct
71 Correct 34 ms 7352 KB Output is correct
72 Correct 26 ms 7256 KB Output is correct
73 Correct 28 ms 7516 KB Output is correct
74 Correct 47 ms 7516 KB Output is correct
75 Correct 57 ms 7516 KB Output is correct
76 Correct 51 ms 7516 KB Output is correct
77 Correct 50 ms 7600 KB Output is correct
78 Correct 60 ms 7512 KB Output is correct
79 Correct 40 ms 7516 KB Output is correct
80 Correct 51 ms 7516 KB Output is correct
81 Correct 59 ms 7568 KB Output is correct
82 Correct 53 ms 7584 KB Output is correct
83 Correct 66 ms 7516 KB Output is correct
84 Correct 56 ms 7516 KB Output is correct
85 Correct 60 ms 7532 KB Output is correct
86 Correct 59 ms 7512 KB Output is correct
87 Correct 68 ms 7552 KB Output is correct
88 Correct 70 ms 7480 KB Output is correct
89 Correct 63 ms 7260 KB Output is correct
90 Correct 69 ms 7480 KB Output is correct
91 Correct 65 ms 7256 KB Output is correct
92 Correct 70 ms 7512 KB Output is correct