Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <queue>
- #include <vector>
- using namespace std;
- int n, m, ans;
- int map[101][101];
- int dr[] = { -1,0,1,0 };
- int dc[] = { 0,-1,0,1 };
- queue<pair<int,int>> q;
- void bfs() {
- int r, c, nr, nc;
- q.push({ 0, 0 });
- map[0][0] = 2;
- while (!q.empty()) {
- r = q.front().first;
- c = q.front().second;
- q.pop();
- for (int i = 0; i < 4; i++) {
- nr = r + dr[i];
- nc = c + dc[i];
- if (0 <= nr && nr < n && 0 <= nc && nc < m) {
- if (map[nr][nc] == 1) {
- map[nr][nc] = map[r][c]+1;
- q.push({ nr,nc });
- if (nr == n - 1 && nc == m - 1) {
- ans = map[nr][nc];
- break;
- }
- }
- }
- }
- }
- }
- int main() {
- scanf("%d %d", &n, &m);
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- scanf(" %1d", &map[i][j]);
- }
- }
- bfs();
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- printf("%d ", map[i][j]);
- }
- printf("\n");
- }
- printf("%d\n", ans-1);
- return 0;
- }
Add Comment
Please, Sign In to add comment