From 6678e97a0687383dc93ed8653d1a356227633d73 Mon Sep 17 00:00:00 2001 From: sh0723 Date: Mon, 22 Jun 2026 15:59:34 +0900 Subject: [PATCH] =?UTF-8?q?14497=20:=20=EC=A3=BC=EB=82=9C=EC=9D=98=20?= =?UTF-8?q?=EB=82=9C(=E9=9B=A3)=20solve?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/week3/BOJ14497/BOJ14497_V1.cpp | 64 ++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/week3/BOJ14497/BOJ14497_V1.cpp diff --git a/src/week3/BOJ14497/BOJ14497_V1.cpp b/src/week3/BOJ14497/BOJ14497_V1.cpp new file mode 100644 index 0000000..bc1cb82 --- /dev/null +++ b/src/week3/BOJ14497/BOJ14497_V1.cpp @@ -0,0 +1,64 @@ +#include +using namespace std; +int visited[301][301]; +int inp_class[301][301]; +int dy[4] = {0,0,1,-1}; +int dx[4] = {1,-1,0,0}; +pair friends, junan; +int M,N; +int main() { + + ios_base::sync_with_stdio(false); + cin.tie(NULL); + cout.tie(NULL); + + cin >> M >> N; + cin >> junan.first >> junan.second >> friends.first >> friends.second; + + for (int i=1; i<=M; i++) { + string s; + cin >> s; + for (int j=1; j<=N; j++) { + if (i == junan.first && j == junan.second) inp_class[i][j] = -1; + else if (i == friends.first && j == friends.second) inp_class[i][j] = 2; + else inp_class[i][j] = s[j-1] - '0'; + } + } + int cost = 0; + queue > q; + q.push(junan); + visited[junan.first][junan.second] = 1; + while(true) { + queue > next_q; + + while(!q.empty()) { + pair curr = q.front(); + q.pop(); + + if (inp_class[curr.first][curr.second] == 2) { + cout << cost << '\n'; + return 0; + } + + for (int i=0; i<4; i++) { + int ny = curr.first + dy[i]; + int nx = curr.second + dx[i]; + if (ny < 1 || ny > M || nx < 1 || nx > N) continue; + if (visited[ny][nx]) continue; + if (inp_class[ny][nx] == 0) { + q.push({ny,nx}); + visited[ny][nx] = 1; + } else { + next_q.push({ny,nx}); + visited[ny][nx] = 1; + } + } + } + cost++; + q = next_q; + } + + + + return 0; +} \ No newline at end of file