rt 代码:
#include <bits/stdc++.h>
#define INF 1005
using namespace std;
int fi[INF][INF]; //chart
int s[INF]; //sort
int cnt1; //sort x2
int cnt; // || ans
int n,m,t; //n line(s) m row(s) and time
bool cmp(int n,int m)
{
return n>m; //sort x3
}
int ab(int n) //for abs
{
if(n<0)
{
return -n;
}
return n;
}
int find(int num)
{
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
if(fi[i][j]==num)
{
return i*INF+j; //find numbers
}
}
}
}
int main()
{
// freopen("ex_data1.in","r",stdin);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>m>>t;
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
cin>>fi[i][j]; // cin
s[++cnt1]=fi[i][j];
}
}
t-=1; //<- <- <-here!!! qaq
sort(s+1,s+cnt1+1,cmp); //sort x4
for(int i=1; i<=cnt1; i++)
{
int fix,fiy; //next
int nowx,nowy; //now
if(i==1) //first ?
{
nowx=find(s[i])%INF; //look at fuction "find"
nowy=0;
}
else
{
nowx=fix;
nowy=fiy;
}
fix=find(s[i])%INF;
fiy=find(s[i])/INF;
// cout<<nowx<<' '<<nowy<<' '<<fix<<' '<<fiy<<endl; //debug
// cout<<"s[i]="<<s[i]<<endl; //debug x2
if(t-(ab(nowx-fix)+ab(nowy-fiy)+1+fix)<0) //distance
{
break;
}
else
{
cnt+=s[i];
// cout<<"cnt="<<cnt<<endl; //debug x3
// cout<<"dis="<<ab(nowx-fix)+ab(nowy-fiy)+1<<endl; //debug x4
t-=ab(nowx-fix)+ab(nowy-fiy)+1; //problem?
// cout<<"t="<<t<<endl; //debug x5
}
}
cout<<cnt<<endl;
}
/*
6 7 21
0 0 0 0 0 0 0
0 0 0 0 13 0 0
0 0 0 0 0 0 7
0 15 0 0 0 0 0
0 0 0 9 0 0 0
0 0 0 0 0 0 0
6 7 20
0 0 0 0 0 0 0
0 0 0 0 13 0 0
0 0 0 0 0 0 7 WA
0 15 0 0 0 0 0
0 0 0 9 0 0 0
0 0 0 0 0 0 0
20 20 400
0 182 0 324 64 24 0 354 0 0 156 344 0 0 0 0 346 0 34 0
0 0 0 378 0 302 0 0 276 0 16 0 218 216 326 0 306 0 270 168
0 190 0 0 22 128 0 0 296 340 348 0 376 174 102 0 0 372 18 100
0 86 286 110 274 118 0 0 30 360 94 0 10 338 0 212 240 0 210 114
2 0 150 0 0 0 0 0 186 358 0 0 308 224 396 398 0 0 244 0
180 0 56 0 0 66 122 388 172 0 0 0 0 0 0 294 0 0 0 14
268 384 0 170 266 0 0 0 0 0 0 0 0 362 0 0 6 0 0 0
394 0 0 364 0 0 0 0 60 0 0 332 0 0 0 0 0 374 0 282
226 88 134 106 0 200 196 0 0 0 32 230 0 144 214 76 46 284 0 0
140 0 0 0 72 0 96 314 250 316 0 154 0 0 206 0 0 318 0 132
90 38 0 368 160 26 0 0 44 194 280 382 138 0 334 188 242 0 238 0
0 0 0 84 158 380 258 202 0 248 4 68 0 232 80 178 28 0 246 322
0 0 0 300 0 54 0 0 0 0 0 254 0 0 236 0 0 228 204 0
0 0 0 278 304 0 164 0 312 0 0 264 62 124 0 0 320 0 0 104
208 198 0 98 0 356 0 12 0 352 126 0 0 0 262 120 0 82 0 288
148 252 0 0 74 392 0 78 0 0 330 92 192 0 0 0 50 36 298 0
0 0 0 370 366 0 0 256 184 0 386 0 116 0 112 8 290 0 176 130
70 152 58 166 162 0 142 0 272 0 0 234 0 0 0 48 328 0 350 0
0 292 0 40 0 0 222 390 0 0 0 0 260 0 310 0 0 0 220 0
0 0 136 0 342 336 42 0 0 0 108 0 146 20 0 0 0 52 0 0
*/