-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtoi08_maze.cpp
66 lines (56 loc) · 1.75 KB
/
toi08_maze.cpp
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
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define MOD 1e9 + 7
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
int m,n,sx,sy,ex,ey;
cin >> m >> n >> sx >> sy >> ex >> ey;
sx--; sy--; ex--; ey--;
bool a[m][n];
int d[m][n],b[m][n];
queue<pair<int,int> > q;
for(int i = 0;i < m;i++) for(int j = 0;j < n;j++){ cin >> a[i][j]; d[i][j] = 0; b[i][j] = 0; }
q.push({sx,sy});
d[sx][sy] = 1;
while(!q.empty())
{
int x = q.front().first,y = q.front().second;
q.pop();
for(int i = -1;i <= 1;i++) for(int j = -1;j <= 1;j++)
{
if(i==0 and j==0) continue;
if(i!=0 and j!=0) continue;
int ai = x+i,aj = y+j;
if(ai<0 or ai>=m or aj<0 or aj>=n) continue;
if(d[ai][aj]>0) continue;
d[ai][aj] = d[x][y]+1;
if(a[ai][aj]) q.push({ai,aj});
}
}
int mn = INT_MAX,cnt = 0;
q.push({ex,ey});
b[ex][ey] = 1;
while(!q.empty())
{
int x = q.front().first,y = q.front().second;
q.pop();
for(int i = -1;i <= 1;i++) for(int j = -1;j <= 1;j++)
{
if(i==0 and j==0) continue;
if(i!=0 and j!=0) continue;
int ai = x+i,aj = y+j;
if(ai<0 or ai>=m or aj<0 or aj>=n) continue;
if(b[ai][aj]>0) continue;
if(d[ai][aj]>0){ mn = min(mn,b[x][y]+d[ai][aj]); cnt++; /*cout << x << ' ' << y << ' ' << b[x][y] << ' ' << ai << ' ' << aj << ' ' << d[ai][aj] << endl;*/ }
b[ai][aj] = b[x][y]+1;
if(a[ai][aj]) q.push({ai,aj});
}
}
/*
cout << endl;
for(int i = 0;i < m;i++){ for(int j = 0;j < n;j++) cout << d[i][j] << ' '; cout << endl; }
*/
cout << cnt << endl << mn;
}