#include<bits/stdc++.h>
#define f first
#define s second
#define pii pair< pair<int,pair<int,int > > ,pair<int,int> >
using namespace std;
const int N=1e3+5,INF=2e9;
int n,m,dist[N][N][2][2],k,i,ans,a[N][N];
int a1[]={0,0,-1,1},a2[]={-1,1,0,0};
priority_queue<pii,vector<pii>,greater<pii> > q;
bool ok(int x,int y){
if(min(x,y)<1 || x>n || y>m ) return 0;
return 1;
}
main(){
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>n>>m;
for(k=1;k<=n;k++){
for(int i=1;i<=m;i++){
cin>>a[k][i];
for(int b1=0;b1<=1;b1++){
for(int b2=0;b2<=1;b2++){
int cur = 0;
if(0!=b1) cur += a[k][i];
if(0!=b2) cur -= a[k][i];
dist[k][i][b1][b2] = cur;
q.push({{cur,{k,i}},{b1,b2}});
}
}
}
}
ans = INF;
while(q.size()){
int k=q.top().f.s.f;
int i=q.top().f.s.s;
int d=q.top().f.f;
int c1=q.top().s.f;
int c2=q.top().s.s;
q.pop();
if(dist[k][i][c1][c2] < d) continue;
if(c1+c2==2 ) ans=min(ans,d);
for(int j=0;j<4;j++){
if(ok(k+a1[j],i+a2[j])){
for(int b1=c1;b1<=1;b1++){
for(int b2=c2;b2<=1;b2++){
int cur = d + 1;
if(c1!=b1) cur += a[k+a1[j]][i+a2[j]];
if(b2!=c2) cur -= a[k+a1[j]][i+a2[j]];
if(dist[k+a1[j]][i+a2[j]][b1][b2] > cur){
dist[k+a1[j]][i+a2[j]][b1][b2] = cur;
q.push({{cur,{k+a1[j],i+a2[j]}},{b1,b2}});
}
}
}
}
}
}
cout<<-ans-1;
}
Compilation message
maxcomp.cpp:14:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
14 | main(){
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
620 KB |
Output is correct |
2 |
Correct |
2 ms |
620 KB |
Output is correct |
3 |
Correct |
2 ms |
620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
5 ms |
1192 KB |
Output is correct |
10 |
Correct |
8 ms |
1192 KB |
Output is correct |
11 |
Correct |
6 ms |
1192 KB |
Output is correct |
12 |
Correct |
6 ms |
1212 KB |
Output is correct |
13 |
Correct |
6 ms |
1192 KB |
Output is correct |
14 |
Correct |
6 ms |
1192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
2 ms |
620 KB |
Output is correct |
10 |
Correct |
2 ms |
620 KB |
Output is correct |
11 |
Correct |
2 ms |
620 KB |
Output is correct |
12 |
Correct |
5 ms |
1192 KB |
Output is correct |
13 |
Correct |
8 ms |
1192 KB |
Output is correct |
14 |
Correct |
6 ms |
1192 KB |
Output is correct |
15 |
Correct |
6 ms |
1212 KB |
Output is correct |
16 |
Correct |
6 ms |
1192 KB |
Output is correct |
17 |
Correct |
6 ms |
1192 KB |
Output is correct |
18 |
Execution timed out |
1090 ms |
184268 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |