diff --git a/src/week3/BOJ14497/BOJ14497_V2.cpp b/src/week3/BOJ14497/BOJ14497_V2.cpp new file mode 100644 index 0000000..4aa522b --- /dev/null +++ b/src/week3/BOJ14497/BOJ14497_V2.cpp @@ -0,0 +1,60 @@ +#include +using namespace std; +int visited[300][300]; +char inp[300][300]; +int dy[4] = {-1, 0, 1, 0}; +int dx[4] = {0, 1, 0, -1}; +int N,M,jx, jy, tx,ty; +int main() { + + ios_base::sync_with_stdio(false); + cin.tie(NULL); + cout.tie(NULL); + + cin >> N >> M >> jy >> jx >> ty >> tx; + jy--;jx--;ty--;tx--; + for (int i=0; i> s; + for (int j=0; j> q; + q.push({jy, jx}); + visited[jy][jx] = 1; + + while(true) { + queue> next_q; + while(!q.empty()) { + pair p = q.front(); + q.pop(); + if (p.first == ty && p.second == tx) { + cout << cnt << '\n'; + return 0; + } + for (int i=0; i<4; i++) { + int ny = p.first + dy[i]; + int nx = p.second + dx[i]; + if (ny < 0 || nx < 0 || ny >= N || nx >= M) continue; + if (visited[ny][nx]) continue; + if (inp[ny][nx] == '1') { + next_q.push({ny, nx}); + visited[ny][nx] = 1; + } else { + q.push({ny, nx}); + visited[ny][nx] = 1; + } + } + } + cnt++; + q = next_q; + } + + cout << cnt << '\n'; + + return 0; +} \ No newline at end of file