백준

백준 2178 : 미로탐색

E재HO 2022. 3. 9. 22:44

미로 탐색 성공임

 
 
시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 192 MB 115068 47767 30710 40.414%

문제

N×M크기의 배열로 표현되는 미로가 있다.

1 0 1 1 1 1
1 0 1 0 1 0
1 0 1 0 1 1
1 1 1 0 1 1

미로에서 1은 이동할 수 있는 칸을 나타내고, 0은 이동할 수 없는 칸을 나타낸다. 이러한 미로가 주어졌을 때, (1, 1)에서 출발하여 (N, M)의 위치로 이동할 때 지나야 하는 최소의 칸 수를 구하는 프로그램을 작성하시오. 한 칸에서 다른 칸으로 이동할 때, 서로 인접한 칸으로만 이동할 수 있다.

위의 예에서는 15칸을 지나야 (N, M)의 위치로 이동할 수 있다. 칸을 셀 때에는 시작 위치와 도착 위치도 포함한다.

입력

첫째 줄에 두 정수 N, M(2 ≤ N, M ≤ 100)이 주어진다. 다음 N개의 줄에는 M개의 정수로 미로가 주어진다. 각각의 수들은 붙어서 입력으로 주어진다.

출력

첫째 줄에 지나야 하는 최소의 칸 수를 출력한다. 항상 도착위치로 이동할 수 있는 경우만 입력으로 주어진다.

예제 입력 1 복사

4 6
101111
101010
101011
111011

예제 출력 1 복사

15

예제 입력 2 복사

4 6
110110
110110
111111
111101

예제 출력 2 복사

9

예제 입력 3 복사

2 25
1011101110111011101110111
1110111011101110111011101

예제 출력 3 복사

38

예제 입력 4 복사

7 7
1011111
1110001
1000001
1000001
1000001
1000001
1111111

예제 출력 4 복사

13

출처

알고리즘 분류

실패한 코드

dfs

 

 

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int arr[101][101];
bool visited[101][101];
int dy[4] = { 1,-1,0,0 };
int dx[4] = { 0, 0, 1,-1 };
int answer =987654321;//최소값

int n, m;

void dfs(int y, int x ,int move) {

    if (y<1 || x <1 || y > n || x >m || arr[y][x] == 0)return;

    if (y == n && x == m) {
        if (move < answer)
            answer = move;
        return;
    }


    for (int i = 0; i < 4; i++) {
        int ny = y + dy[i];
        int nx = x + dx[i];
        if (visited[ny][nx] == false) {
            visited[ny][nx] = true;
            dfs(ny, nx, move + 1);
            visited[ny][nx] = false;
        }
    }

}
int main() {

    cin >> n >> m;

    for (int i = 1; i <= n; i++) {
        string s; 
        cin >> s;
        for (int j = 0; j < m; j++) {
            arr[i][j+1] = s[j]-'0';
        }
    }



    
    dfs(1,1,0);
    cout << answer +1 << endl;
    

    return 0;
}

 

dfs로 푸니까 실패했다.

 

#include <iostream>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;

int arr[101][101];
bool visited[101][101];
int dist[101][101];
int dy[4] = { 1,-1,0,0 };
int dx[4] = { 0, 0, 1,-1 };
int answer =987654321;//최소값
queue<pair<int,int>> q;
int n, m;

void bfs(int y, int x ) {
    visited[y][x] = true;
    q.push({ y, x });

    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 > n || nx > m || ny < 1 || nx < 1)continue;

            if (arr[ny][nx] == 1 && visited[ny][nx] == false) {
                visited[ny][nx] = true;
                q.push({ ny,nx });
                dist[ny][nx] = dist[y][x] + 1;
            }


        }
    }
    
}
int main() {

    cin >> n >> m;

    for (int i = 1; i <= n; i++) {
        string s; 
        cin >> s;
        for (int j = 0; j < m; j++) {
            arr[i][j+1] = s[j]-'0';
        }
    }
    
    bfs(1,1);
    cout << dist[n][m] + 1 << endl;
    

    return 0;
}

 

그래서 bfs로 풀어봤따 dfs보단 bfs가 조금 더 빠르다!