-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathj.cpp
86 lines (72 loc) · 1.33 KB
/
j.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <algorithm>
#include <iostream>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <map>
using namespace std;
#define mp make_pair
#define st first
#define nd second
#define ll long long
#define pll pair<long long, long long>
#define pb push_back
int n, m, x, y, step, nextx, nexty;
queue<pair<pair<int, int>, int>> Q;
bool visited[505][505];
char c;
int num[505][505];
int d[4][2] = {
{0, 1},
{1, 0},
{0, -1},
{-1, 0}};
bool isWithinBounds(int x, int y)
{
return x >= 1 and x <= n and y >= 1 and y <= m;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
{
cin >> c;
num[i][j] = c - '0';
}
Q.push(mp(mp(1, 1), 0));
while (!Q.empty())
{
x = Q.front().st.st;
y = Q.front().st.nd;
step = Q.front().nd;
Q.pop();
if (visited[x][y])
continue;
visited[x][y] = true;
if (x == n and y == m)
{
cout << step << endl;
return 0;
}
for (int i = 0; i < 4; i++)
{
nextx = x + d[i][0] * num[x][y];
nexty = y + d[i][1] * num[x][y];
if (isWithinBounds(nextx, nexty))
Q.push(mp(mp(nextx, nexty), step + 1));
}
}
cout << -1 << endl;
return 0;
}