diff --git a/src/week3/BOJ12851/BOJ12851_V1.cpp b/src/week3/BOJ12851/BOJ12851_V1.cpp new file mode 100644 index 0000000..482e5b8 --- /dev/null +++ b/src/week3/BOJ12851/BOJ12851_V1.cpp @@ -0,0 +1,43 @@ +#include +using namespace std; +int brother, subin; +int ret = INT_MAX; +int ret_cnt; +int visited[100001]; +void solve(int idx, int level) { + if (idx > 100000 || idx < 0) { + return; + } + if (visited[idx]) return; + if (level > ret) return; + if (level == ret && idx == brother) { + ret_cnt++; + return; + } + if (idx == brother && level < ret) { + ret = level; + ret_cnt = 1; + return; + } + + visited[idx] = 1; + solve(idx-1, level+1); + solve(idx+1, level+1); + solve(idx*2, level+1); + visited[idx] = 0; +} +int main() { + + ios::sync_with_stdio(false); + cin.tie(NULL); + cout.tie(NULL); + + cin >> subin >> brother; + + solve(subin, 0); + + cout << ret << '\n' << ret_cnt << '\n'; + + + return 0; +} \ No newline at end of file diff --git a/src/week3/BOJ12851/BOJ12851_V2.cpp b/src/week3/BOJ12851/BOJ12851_V2.cpp new file mode 100644 index 0000000..9cb21e3 --- /dev/null +++ b/src/week3/BOJ12851/BOJ12851_V2.cpp @@ -0,0 +1,38 @@ +#include +using namespace std; +int brother, subin; +int visited[100001]; +int cnt[100001]; +int main() { + + ios_base::sync_with_stdio(false); + cin.tie(NULL); + cout.tie(NULL); + + cin >> subin >> brother; + queue q; + q.push(subin); + visited[subin] = 1; + cnt[subin] = 1; + int now; + while(!q.empty()) { + now = q.front(); + q.pop(); + for (int next : {now-1, now+1, now *2}) { + if (0 <= next && next <= 100000) { + if (visited[next] == 0) { + q.push(next); + visited[next] = visited[now] + 1; + cnt[next] = cnt[now]; + } else if (visited[next] == visited[now] + 1) { + cnt[next] += cnt[now]; + } + + } + } + + } + + cout << visited[brother]-1 << '\n' << cnt[brother] << '\n'; + return 0; +} \ No newline at end of file