-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathccc08s3.cpp
60 lines (56 loc) · 1.67 KB
/
ccc08s3.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
#include <bits/stdc++.h>
using namespace std;
char graph[22][22];
int dist[22][22];
void BFS ()
{
list<pair<int,int>> pq;
pq.push_back(make_pair(1, 1));
dist[1][1] = 0;
while (!pq.empty())
{
int u = pq.front().first, v = pq.front().second;
pq.pop_front();
if (dist[u][v]+1 < dist[u+1][v] && graph[u+1][v] != '*' && (graph[u][v] == '+' || graph[u][v] == '|'))
{
dist[u+1][v] = dist[u][v] + 1;
pq.push_back(make_pair(u+1, v));
}
if (dist[u][v]+1 < dist[u-1][v] && graph[u-1][v] != '*' && (graph[u][v] == '+' || graph[u][v] == '|'))
{
dist[u-1][v] = dist[u][v] + 1;
pq.push_back(make_pair(u-1, v));
}
if (dist[u][v]+1 < dist[u][v+1] && graph[u][v+1] != '*' && (graph[u][v] == '+' || graph[u][v] == '-'))
{
dist[u][v+1] = dist[u][v] + 1;
pq.push_back(make_pair(u, v+1));
}
if (dist[u][v]+1 < dist[u][v-1] && graph[u][v-1] != '*' && (graph[u][v] == '+' || graph[u][v] == '-'))
{
dist[u][v-1] = dist[u][v] + 1;
pq.push_back(make_pair(u, v-1));
}
}
}
int main()
{
int t, r, c;
scanf("%i", &t);
for (int a = 0; a < t; a++)
{
scanf("%i%i", &r, &c);
for (int x = 0; x < 22; x++)
for (int y = 0; y < 22; y++)
graph[x][y] = '*', dist[x][y] = INT_MAX;
for (int x = 1; x <= r; x++)
for (int y = 1; y <= c; y++)
scanf(" %c", &graph[x][y]);
BFS();
if (dist[r][c] == INT_MAX)
printf("-1\n");
else
printf("%i\n", dist[r][c]+1);
}
return 0;
}