#include<bits/stdc++.h>
#define f first
#define s second
#define int long long
#define pii pair< pair<int,pair<int,int > > ,pair<int,int> >
using namespace std;
const int N=1e3+5,INF=1e13;
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(){
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=0;b1<=1;b1++){
for(int b2=0;b2<=1;b2++){
if(c1>b1 || c2>b2) continue;
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:15:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
15 | 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 |
3 ms |
844 KB |
Output is correct |
2 |
Correct |
3 ms |
844 KB |
Output is correct |
3 |
Correct |
3 ms |
844 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 |
7 ms |
1576 KB |
Output is correct |
10 |
Correct |
8 ms |
1576 KB |
Output is correct |
11 |
Correct |
10 ms |
1576 KB |
Output is correct |
12 |
Correct |
8 ms |
1576 KB |
Output is correct |
13 |
Correct |
8 ms |
1576 KB |
Output is correct |
14 |
Correct |
7 ms |
1704 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 |
3 ms |
844 KB |
Output is correct |
10 |
Correct |
3 ms |
844 KB |
Output is correct |
11 |
Correct |
3 ms |
844 KB |
Output is correct |
12 |
Correct |
7 ms |
1576 KB |
Output is correct |
13 |
Correct |
8 ms |
1576 KB |
Output is correct |
14 |
Correct |
10 ms |
1576 KB |
Output is correct |
15 |
Correct |
8 ms |
1576 KB |
Output is correct |
16 |
Correct |
8 ms |
1576 KB |
Output is correct |
17 |
Correct |
7 ms |
1704 KB |
Output is correct |
18 |
Execution timed out |
1091 ms |
376820 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |