Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Task : _example
- Author : Phumipat C. [MAGCARI]
- Language: C++
- Created : 26 February 2023 [11:15]
- */
- #include<bits/stdc++.h>
- using namespace std;
- struct A{
- int i,j,w;
- bool operator < (const A&o) const{
- return w>o.w;
- }
- };
- priority_queue<A > heap;
- int a[1010][1010];
- bool visited[1010][1010];
- int dir[2][4] = {{1,-1,0,0},{0,0,1,-1}};
- int main(){
- cin.tie(0)->sync_with_stdio(0);
- cin.exceptions(cin.failbit);
- int q,n,sti,stj;
- long long m;
- cin >> q;
- while(q--){
- cin >> n >> m >> sti >> stj;
- for(int i=1;i<=n;i++)
- for(int j=1;j<=n;j++)
- cin >> a[i][j];
- memset(visited,0,sizeof visited);
- heap.push({sti,stj,a[sti][stj]});
- visited[sti][stj] = true;
- long long cnt = 0;
- int mx = -1;
- while(!heap.empty()){
- A now = heap.top();
- heap.pop();
- cnt+=now.w;
- mx = max(mx,now.w);
- // visited[now.i][now.j] = true;
- if(cnt >= m){
- cout << mx << '\n';
- break;
- }
- for(int k=0;k<4;k++){
- int ni = now.i + dir[0][k],nj = now.j + dir[1][k];
- if(ni < 1 || nj < 1 || ni > n || nj > n) continue;
- if(visited[ni][nj]) continue;
- visited[ni][nj] = true;
- heap.push({ni,nj,a[ni][nj]});
- }
- }
- while(!heap.empty()) heap.pop();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment