forked from MoncefMhasni/AMPC-2015
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathE.cpp
More file actions
53 lines (52 loc) · 1.3 KB
/
Copy pathE.cpp
File metadata and controls
53 lines (52 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<bits/stdc++.h>
#define INF INT_MAX/2
using namespace std;
void solve(vector<vector<int> >&g)
{
queue<pair<int,int>> Q;
int M=g.size(),N=g[0].size();
vector<vector<int> > visited(M);visited.clear();
for(int i=0;i<M;i++)visited[i].resize(N);
visited[M-1][0]=1;
Q.push(make_pair(M-1,0));
while(!Q.empty())
{
pair<int,int> p = Q.front();
Q.pop();
for(int x =-2;x<=2;x++)
{
for(int y =-2;y<=2;y++)
{
int i = p.first+x;
int j = p.second+y;
if(i<0 || i>=M || j<0 || j>=N || g[i][j]==-1||abs(x)+abs(y)!=3) continue;
g[i][j]=min(g[i][j],g[p.first][p.second]+1);
if(visited[i][j]==0)
{
visited[i][j]=1;
Q.push(make_pair(i,j));
}
}
}
}
}
int main(){
// freopen("E.in","r",stdin);
// freopen("E.out","w",stdout);
int n,m;
while(cin>>m>>n){
vector<vector<int> >g;
g.resize(m);
for(int i=0;i<m;i++){
g[i].resize(n);
for(int j=0;j<n;j++)g[i][j]=INF;
}
g[m-1][0]=0;
solve(g);
if(g[0][m-1]==INF)
cout<<"-1"<<endl;
else
cout<<g[0][n-1]<<endl;
}
return 0;
}