// 미니 점프 게임

// N×M 크기의 맵이 있다.

// 현재 (0,0)에 서 있다.
// 상하좌우로 이동할 수 있다.

// 1 : 이동 가능
// 0 : 이동 불가능

// (N-1,M-1)까지 가는 최소 이동 횟수를 출력하시오.

// 입력 형식

// 첫째 줄에 N, M이 주어진다.

// 다음 N줄에 맵 정보가 주어진다.

// 출력 형식

// 최소 이동 횟수를 출력한다.

// 입력 예시
// 3 3
// 111
// 101
// 111
// 출력 예시
// 5

#include <iostream>
#include <queue>
#include <string>

using namespace std;

int n, m;

int board[15][15];
int dist[15][15];

int dy[] = {-1,1,0,0};
int dx[] = {0,0,-1,1};

int main() {

    cin >> n >> m;

    for(int i = 0; i < n; i++) {

        string s;
        cin >> s;

        for(int j = 0; j < m; j++) {
            board[i][j] = s[j] - '0';
        }
    }

    queue<pair<int,int>> q;

    q.push({0,0});

    dist[0][0] = 1;

    while(!q.empty()) {

        int y = q.front().first;
        int x = q.front().second;

        q.pop();

        for(int i = 0; i < 4; i++) {

            int ny = y + dy[i];
            int nx = x + dx[i];

            if(ny >= 0 && ny < n &&
               nx >= 0 && nx < m) {

                if(board[ny][nx] == 1 &&
                   dist[ny][nx] == 0) {

                    dist[ny][nx] =
                        dist[y][x] + 1;

                    q.push({ny,nx});
                }
            }
        }
    }

    cout << dist[n-1][m-1];
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: