From add879239664f11cfe7da03ae79bcae78b7ccece Mon Sep 17 00:00:00 2001 From: sh0723 Date: Mon, 25 May 2026 01:12:52 +0900 Subject: [PATCH] =?UTF-8?q?2178=20:=20=EB=AF=B8=EB=A1=9C=ED=83=90=EC=83=89?= =?UTF-8?q?=20solve?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/week2/BOJ2178/BOJ2178_V1.cpp | 56 ++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/week2/BOJ2178/BOJ2178_V1.cpp diff --git a/src/week2/BOJ2178/BOJ2178_V1.cpp b/src/week2/BOJ2178/BOJ2178_V1.cpp new file mode 100644 index 0000000..0791a5c --- /dev/null +++ b/src/week2/BOJ2178/BOJ2178_V1.cpp @@ -0,0 +1,56 @@ +#include +using namespace std; +int N, M; +int dy[4] = {-1, 0, 1, 0}; +int dx[4] = {0, 1, 0, -1}; +vector> inp_map; +vector> visited; + +void BFS(int y, int x) { + visited[y][x] = 1; + queue> q; + q.push(make_pair(y, x)); + + while (!q.empty()) { + int cy = q.front().first; + int cx = q.front().second; + q.pop(); + for (int i = 0; i < 4; i++) { + + int ny = cy + dy[i]; + int nx = cx + dx[i]; + if (0 <= ny && ny < N && 0 <= nx && nx < M) { + if (visited[ny][nx] == 0 && inp_map[ny][nx] == 1) { + visited[ny][nx] = visited[cy][cx] + 1; + q.push(make_pair(ny, nx)); + } + + } + } + } +} + +int main() { + + ios_base::sync_with_stdio(false); + cin.tie(NULL); + cout.tie(NULL); + + cin >> N >> M; + + for(int i = 0; i < N; i++) { + inp_map.push_back(vector()); + visited.push_back(vector()); + string tmp; + cin >> tmp; + for(int j = 0; j < M; j++) { + inp_map[i].push_back(tmp[j]-'0'); + visited[i].push_back(0); + } + } + + BFS(0, 0); + + cout << visited[N-1][M-1] << "\n"; + return 0; +} \ No newline at end of file