MAGCARI

ChanParty

Feb 25th, 2023
747
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. /*
  2.     Task    : _example
  3.     Author  : Phumipat C. [MAGCARI]
  4.     Language: C++
  5.     Created : 26 February 2023 [11:15]
  6. */
  7. #include<bits/stdc++.h>
  8. using namespace std;
  9. struct A{
  10.     int i,j,w;
  11.     bool operator < (const A&o) const{
  12.         return w>o.w;
  13.     }
  14. };
  15. priority_queue<A > heap;
  16. int a[1010][1010];
  17. bool visited[1010][1010];
  18. int dir[2][4] = {{1,-1,0,0},{0,0,1,-1}};
  19. int main(){
  20.     cin.tie(0)->sync_with_stdio(0);
  21.     cin.exceptions(cin.failbit);
  22.     int q,n,sti,stj;
  23.     long long m;
  24.     cin >> q;
  25.     while(q--){
  26.         cin >> n >> m >> sti >> stj;
  27.         for(int i=1;i<=n;i++)
  28.             for(int j=1;j<=n;j++)
  29.                 cin >> a[i][j];
  30.         memset(visited,0,sizeof visited);
  31.         heap.push({sti,stj,a[sti][stj]});
  32.         visited[sti][stj] = true;
  33.         long long cnt = 0;
  34.         int mx = -1;
  35.         while(!heap.empty()){
  36.             A now = heap.top();
  37.             heap.pop();
  38.             cnt+=now.w;
  39.             mx = max(mx,now.w);
  40.             // visited[now.i][now.j] = true;
  41.             if(cnt >= m){
  42.                 cout << mx << '\n';
  43.                 break;
  44.             }
  45.             for(int k=0;k<4;k++){
  46.                 int ni = now.i + dir[0][k],nj = now.j + dir[1][k];
  47.                 if(ni < 1 || nj < 1 || ni > n || nj > n)    continue;
  48.                 if(visited[ni][nj])                         continue;
  49.                 visited[ni][nj] = true;
  50.                 heap.push({ni,nj,a[ni][nj]});
  51.             }
  52.         }
  53.         while(!heap.empty())    heap.pop();
  54.     }
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment