|
| 1 | + |
| 2 | +#include <bits/stdc++.h> |
| 3 | +#define PRECISION 0.0000000000001 |
| 4 | +#define EPSILON 0.000000000001 |
| 5 | +using namespace std; |
| 6 | + |
| 7 | +struct myPoint { |
| 8 | + int r, c; |
| 9 | + long long int n; |
| 10 | + double distance; |
| 11 | + vector<pair<int,int> > path; |
| 12 | + set<pair<int,int> > gems; |
| 13 | + long long int gemtotal; |
| 14 | + bool power, powerused; |
| 15 | + myPoint(int h, int k, double d, long long int number, vector<pair<int,int> > p, set<pair<int,int> > g ,long long int gemt, bool pw, bool pu) : r(h), c(k), n(number), distance(d), path(p), gems(g), gemtotal(gemt), power(pw), powerused(pu) {} |
| 16 | + bool operator< (const myPoint &rhs) const{ |
| 17 | + return (rhs.distance-this->distance>=PRECISION); |
| 18 | + } |
| 19 | +}; |
| 20 | + |
| 21 | +int main() |
| 22 | +{ |
| 23 | + int t, m, n, eeta, alpha, a, b; |
| 24 | + cin>>t; |
| 25 | + while(t--) { |
| 26 | + cin>>m>>n>>eeta>>alpha; |
| 27 | + set<myPoint> myset; |
| 28 | + long long int number = 0; |
| 29 | + vector<vector<int> > grid(m, vector<int>(n)); |
| 30 | + vector<pair<int,int> > arg1; |
| 31 | + set<pair<int,int> >arg2; |
| 32 | + map<pair<pair<pair<int,int>,set<pair<int,int> > >, bool>, double> minDistance; |
| 33 | + for(int i = 0; i<m; i++) { |
| 34 | + for(int j = 0; j<n; j++) { |
| 35 | + cin>> grid[i][j]; |
| 36 | + } |
| 37 | + } |
| 38 | + cin>> a >> b; |
| 39 | + arg1.push_back(pair<int,int>(a,b)); |
| 40 | + if(grid[a][b]>0) { |
| 41 | + arg2.insert(pair<int,int>(a,b)); |
| 42 | + myset.insert(myPoint(a,b,alpha,0,arg1,arg2,grid[a][b],false,false)); |
| 43 | + minDistance[make_pair(make_pair(pair<int,int>(a,b),arg2),false)] = alpha; |
| 44 | + } else { |
| 45 | + if(grid[a][b]==-2) { |
| 46 | + myset.insert(myPoint(a,b,0,0,arg1,arg2, 0, true,false)); |
| 47 | + minDistance[make_pair(make_pair(pair<int,int>(a,b),arg2),true)] = 0; |
| 48 | + } |
| 49 | + else { |
| 50 | + myset.insert(myPoint(a,b,0,0,arg1,arg2, 0, false,false)); |
| 51 | + minDistance[make_pair(make_pair(pair<int,int>(a,b),arg2),false)] = 0; |
| 52 | + } |
| 53 | + } |
| 54 | + while(!myset.empty()) { |
| 55 | + myPoint curr = *(myset.begin()); |
| 56 | + myset.erase(myset.begin()); |
| 57 | + /*cout << curr.r << " " << curr.c << " " << curr.distance<<" "; |
| 58 | + if(curr.path.size()>1){ |
| 59 | + cout << curr.path[curr.path.size()-2].first << " " << curr.path[curr.path.size()-2].second; |
| 60 | + } |
| 61 | + cout << endl;*/ |
| 62 | + if(curr.gemtotal >= eeta) { |
| 63 | + for(int i = 0; i<curr.path.size(); i++) { |
| 64 | + cout << curr.path[i].first << " " << curr.path[i].second << " "; |
| 65 | + } |
| 66 | + cout << endl; |
| 67 | + break; |
| 68 | + } |
| 69 | + if(curr.c+1 <n && grid[curr.r][curr.c+1] != -1) { |
| 70 | + ++number; |
| 71 | + myPoint temp(curr.r, curr.c+1, curr.distance+1+number*EPSILON, number, curr.path, curr.gems, curr.gemtotal,curr.power, curr.powerused); |
| 72 | + if(grid[temp.r][temp.c]==-2 && !temp.powerused) temp.power = true; |
| 73 | + temp.path.push_back(pair<int, int>(temp.r, temp.c)); |
| 74 | + if(grid[temp.r][temp.c]>0 && temp.gems.find(pair<int,int>(temp.r, temp.c))== temp.gems.end()) { |
| 75 | + temp.gemtotal += grid[temp.r][temp.c]; |
| 76 | + temp.gems.insert(pair<int,int>(temp.r, temp.c)); |
| 77 | + temp.distance += alpha; |
| 78 | + } |
| 79 | + if(minDistance.find(make_pair(make_pair(pair<int,int>(temp.r,temp.c), temp.gems),temp.power))==minDistance.end() || |
| 80 | + minDistance[make_pair(make_pair(pair<int,int>(temp.r,temp.c),temp.gems),temp.power)]-temp.distance >= PRECISION) { |
| 81 | + minDistance[make_pair(make_pair(pair<int,int>(temp.r,temp.c),temp.gems),temp.power)] = temp.distance; |
| 82 | + myset.insert(temp); |
| 83 | + } |
| 84 | + } |
| 85 | + if(curr.r+1 <m && curr.c+1<n && grid[curr.r+1][curr.c+1]!=-1 ) { |
| 86 | + ++number; |
| 87 | + myPoint temp(curr.r+1, curr.c+1, curr.distance+sqrt(2)+number*EPSILON, number, curr.path, curr.gems, curr.gemtotal,curr.power, curr.powerused); |
| 88 | + if(grid[temp.r][temp.c]==-2&& !temp.powerused) temp.power = true; |
| 89 | + temp.path.push_back(pair<int, int>(temp.r, temp.c)); |
| 90 | + if(grid[temp.r][temp.c]>0 && temp.gems.find(pair<int,int>(temp.r, temp.c))== temp.gems.end()) { |
| 91 | + temp.gemtotal += grid[temp.r][temp.c]; |
| 92 | + temp.gems.insert(pair<int,int>(temp.r, temp.c)); |
| 93 | + temp.distance += alpha; |
| 94 | + } |
| 95 | + if(minDistance.find(make_pair(make_pair(pair<int,int>(temp.r,temp.c), temp.gems),temp.power))==minDistance.end() || |
| 96 | + minDistance[make_pair(make_pair(pair<int,int>(temp.r,temp.c),temp.gems),temp.power)]-temp.distance >= PRECISION) { |
| 97 | + minDistance[make_pair(make_pair(pair<int,int>(temp.r,temp.c),temp.gems),temp.power)] = temp.distance; |
| 98 | + myset.insert(temp); |
| 99 | + } |
| 100 | + } |
| 101 | + if(curr.r+1<m && grid[curr.r+1][curr.c] != -1 ) { |
| 102 | + ++number; |
| 103 | + myPoint temp(curr.r+1, curr.c, curr.distance+1+number*EPSILON, number, curr.path, curr.gems, curr.gemtotal,curr.power, curr.powerused); |
| 104 | + if(grid[temp.r][temp.c]==-2&& !temp.powerused) temp.power = true; |
| 105 | + temp.path.push_back(pair<int, int>(temp.r, temp.c)); |
| 106 | + if(grid[temp.r][temp.c]>0 && temp.gems.find(pair<int,int>(temp.r, temp.c))== temp.gems.end()) { |
| 107 | + temp.gemtotal += grid[temp.r][temp.c]; |
| 108 | + temp.gems.insert(pair<int,int>(temp.r, temp.c)); |
| 109 | + temp.distance += alpha; |
| 110 | + } |
| 111 | + if(minDistance.find(make_pair(make_pair(pair<int,int>(temp.r,temp.c), temp.gems),temp.power))==minDistance.end() || |
| 112 | + minDistance[make_pair(make_pair(pair<int,int>(temp.r,temp.c),temp.gems),temp.power)]-temp.distance >= PRECISION) { |
| 113 | + minDistance[make_pair(make_pair(pair<int,int>(temp.r,temp.c),temp.gems),temp.power)] = temp.distance; |
| 114 | + myset.insert(temp); |
| 115 | + } |
| 116 | + } |
| 117 | + if(curr.r+1<m && curr.c-1>=0 && grid[curr.r+1][curr.c-1]!= -1) { |
| 118 | + ++number; |
| 119 | + myPoint temp(curr.r+1, curr.c-1, curr.distance+sqrt(2)+number*EPSILON, number, curr.path, curr.gems, curr.gemtotal,curr.power, curr.powerused); |
| 120 | + if(grid[temp.r][temp.c]==-2 && !temp.powerused) temp.power = true; |
| 121 | + temp.path.push_back(pair<int, int>(temp.r, temp.c)); |
| 122 | + if(grid[temp.r][temp.c]>0 && temp.gems.find(pair<int,int>(temp.r, temp.c))== temp.gems.end()) { |
| 123 | + temp.gemtotal += grid[temp.r][temp.c]; |
| 124 | + temp.gems.insert(pair<int,int>(temp.r, temp.c)); |
| 125 | + temp.distance += alpha; |
| 126 | + } |
| 127 | + if(minDistance.find(make_pair(make_pair(pair<int,int>(temp.r,temp.c), temp.gems),temp.power))==minDistance.end() || |
| 128 | + minDistance[make_pair(make_pair(pair<int,int>(temp.r,temp.c),temp.gems),temp.power)]-temp.distance >= PRECISION) { |
| 129 | + minDistance[make_pair(make_pair(pair<int,int>(temp.r,temp.c),temp.gems),temp.power)] = temp.distance; |
| 130 | + myset.insert(temp); |
| 131 | + } |
| 132 | + } |
| 133 | + if(curr.c-1>=0 && grid[curr.r][curr.c-1]!=-1 ) { |
| 134 | + ++number; |
| 135 | + myPoint temp(curr.r, curr.c-1, curr.distance+1+number*EPSILON, number, curr.path, curr.gems, curr.gemtotal,curr.power, curr.powerused); |
| 136 | + if(grid[temp.r][temp.c]==-2 && !temp.powerused) temp.power = true; |
| 137 | + temp.path.push_back(pair<int, int>(temp.r, temp.c)); |
| 138 | + if(grid[temp.r][temp.c]>0 && temp.gems.find(pair<int,int>(temp.r, temp.c))== temp.gems.end()) { |
| 139 | + temp.gemtotal += grid[temp.r][temp.c]; |
| 140 | + temp.gems.insert(pair<int,int>(temp.r, temp.c)); |
| 141 | + temp.distance += alpha; |
| 142 | + } |
| 143 | + if(minDistance.find(make_pair(make_pair(pair<int,int>(temp.r,temp.c), temp.gems),temp.power))==minDistance.end() || |
| 144 | + minDistance[make_pair(make_pair(pair<int,int>(temp.r,temp.c),temp.gems),temp.power)]-temp.distance >= PRECISION) { |
| 145 | + minDistance[make_pair(make_pair(pair<int,int>(temp.r,temp.c),temp.gems),temp.power)] = temp.distance; |
| 146 | + myset.insert(temp); |
| 147 | + } |
| 148 | + } |
| 149 | + if(curr.r-1>=0 && curr.c-1>=0 && grid[curr.r-1][curr.c-1]!=-1 ) { |
| 150 | + ++number; |
| 151 | + myPoint temp(curr.r-1, curr.c-1, curr.distance+sqrt(2)+number*EPSILON, number, curr.path, curr.gems, curr.gemtotal,curr.power, curr.powerused); |
| 152 | + if(grid[temp.r][temp.c]==-2 && !temp.powerused) temp.power = true; |
| 153 | + temp.path.push_back(pair<int, int>(temp.r, temp.c)); |
| 154 | + if(grid[temp.r][temp.c]>0 && temp.gems.find(pair<int,int>(temp.r, temp.c))== temp.gems.end()) { |
| 155 | + temp.gemtotal += grid[temp.r][temp.c]; |
| 156 | + temp.gems.insert(pair<int,int>(temp.r, temp.c)); |
| 157 | + temp.distance += alpha; |
| 158 | + } |
| 159 | + if(minDistance.find(make_pair(make_pair(pair<int,int>(temp.r,temp.c), temp.gems),temp.power))==minDistance.end() || |
| 160 | + minDistance[make_pair(make_pair(pair<int,int>(temp.r,temp.c),temp.gems),temp.power)]-temp.distance >= PRECISION) { |
| 161 | + minDistance[make_pair(make_pair(pair<int,int>(temp.r,temp.c),temp.gems),temp.power)] = temp.distance; |
| 162 | + myset.insert(temp); |
| 163 | + } |
| 164 | + } |
| 165 | + if(curr.r-1>=0 && grid[curr.r-1][curr.c]!=-1 ) { |
| 166 | + ++number; |
| 167 | + myPoint temp(curr.r-1, curr.c, curr.distance+1+number*EPSILON, number, curr.path, curr.gems, curr.gemtotal,curr.power, curr.powerused); |
| 168 | + if(grid[temp.r][temp.c]==-2 && !temp.powerused) temp.power = true; |
| 169 | + temp.path.push_back(pair<int, int>(temp.r, temp.c)); |
| 170 | + if(grid[temp.r][temp.c]>0 && temp.gems.find(pair<int,int>(temp.r, temp.c))== temp.gems.end()) { |
| 171 | + temp.gemtotal += grid[temp.r][temp.c]; |
| 172 | + temp.gems.insert(pair<int,int>(temp.r, temp.c)); |
| 173 | + temp.distance += alpha; |
| 174 | + } |
| 175 | + if(minDistance.find(make_pair(make_pair(pair<int,int>(temp.r,temp.c), temp.gems),temp.power))==minDistance.end() || |
| 176 | + minDistance[make_pair(make_pair(pair<int,int>(temp.r,temp.c),temp.gems),temp.power)]-temp.distance >= PRECISION) { |
| 177 | + minDistance[make_pair(make_pair(pair<int,int>(temp.r,temp.c),temp.gems),temp.power)] = temp.distance; |
| 178 | + myset.insert(temp); |
| 179 | + } |
| 180 | + } |
| 181 | + if(curr.r-1>=0 && curr.c+1<n && grid[curr.r-1][curr.c+1]!=-1 ) { |
| 182 | + ++number; |
| 183 | + myPoint temp(curr.r-1, curr.c+1, curr.distance+sqrt(2)+number*EPSILON, number, curr.path, curr.gems, curr.gemtotal,curr.power, curr.powerused); |
| 184 | + if(grid[temp.r][temp.c]==-2 && !temp.powerused) temp.power = true; |
| 185 | + temp.path.push_back(pair<int, int>(temp.r, temp.c)); |
| 186 | + if(grid[temp.r][temp.c]>0 && temp.gems.find(pair<int,int>(temp.r, temp.c))== temp.gems.end()) { |
| 187 | + temp.gemtotal += grid[temp.r][temp.c]; |
| 188 | + temp.gems.insert(pair<int,int>(temp.r, temp.c)); |
| 189 | + temp.distance += alpha; |
| 190 | + } |
| 191 | + if(minDistance.find(make_pair(make_pair(pair<int,int>(temp.r,temp.c), temp.gems),temp.power))==minDistance.end() || |
| 192 | + minDistance[make_pair(make_pair(pair<int,int>(temp.r,temp.c),temp.gems),temp.power)]-temp.distance >= PRECISION) { |
| 193 | + minDistance[make_pair(make_pair(pair<int,int>(temp.r,temp.c),temp.gems),temp.power)] = temp.distance; |
| 194 | + myset.insert(temp); |
| 195 | + } |
| 196 | + } |
| 197 | + if(curr.power && !curr.powerused) { |
| 198 | + for(int ir = curr.r-5; ir<=curr.r+5; ir++) { |
| 199 | + for(int ic = curr.c-5; ic<=curr.c+5; ic++) { |
| 200 | + if((ir-curr.r)*(ir-curr.r)+(ic-curr.c)*(ic-curr.c) <= 25) { |
| 201 | + if(ir>=0 && ir<m && ic>=0 && ic<n && grid[ir][ic]!=-1) { |
| 202 | + ++number; |
| 203 | + myPoint temp(ir,ic,curr.distance+1+number*EPSILON,number,curr.path,curr.gems,curr.gemtotal,false,true); |
| 204 | + if(grid[temp.r][temp.c]==-2 && !temp.powerused) temp.power = true; |
| 205 | + temp.path.push_back(pair<int,int>(temp.r,temp.c)); |
| 206 | + if(grid[temp.r][temp.c]>0 && temp.gems.find(pair<int,int>(temp.r, temp.c))== temp.gems.end()) { |
| 207 | + temp.gemtotal += grid[temp.r][temp.c]; |
| 208 | + temp.gems.insert(pair<int,int>(temp.r, temp.c)); |
| 209 | + temp.distance += alpha; |
| 210 | + } |
| 211 | + if(minDistance.find(make_pair(make_pair(pair<int,int>(temp.r,temp.c), temp.gems),temp.power))==minDistance.end() || |
| 212 | + minDistance[make_pair(make_pair(pair<int,int>(temp.r,temp.c),temp.gems),temp.power)]-temp.distance >= PRECISION) { |
| 213 | + minDistance[make_pair(make_pair(pair<int,int>(temp.r,temp.c),temp.gems),temp.power)] = temp.distance; |
| 214 | + myset.insert(temp); |
| 215 | + } |
| 216 | + } |
| 217 | + } |
| 218 | + } |
| 219 | + } |
| 220 | + } |
| 221 | + } |
| 222 | + } |
| 223 | + return 0; |
| 224 | +} |
0 commit comments