-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17391.cpp
53 lines (49 loc) · 1 KB
/
17391.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
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
int map[301][301];
bool visited[301][301];
int mx[2] = {1,0};
int my[2] = {0,1};
int N, M;
void dfs() {
queue <pair<pair<int, int>, int >> que;
que.push(make_pair(make_pair(0, 0),0));
visited[0][0] = true;
while (!que.empty()) {
int x = que.front().first.first;
int y = que.front().first.second;
int count = que.front().second;
que.pop();
if (x == N - 1 && y == M - 1) {
cout << count;
return;
}
for (int i = 0; i < 2; i++) {
for (int j = 1; j <= map[x][y]; j++) {
int nx = x + mx[i] * j;
int ny = y + my[i] * j;
if (0 <= nx && 0 <= ny && nx < N && ny < M) {
if (!visited[nx][ny]) {
visited[nx][ny] = true;
que.push(make_pair(make_pair(nx, ny), count + 1));
}
}
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> N >> M;
for (int x = 0; x < N; x++) {
for (int y = 0; y < M; y++) {
cin >> map[x][y];
}
}
dfs();
return 0;
}