-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from AlgoLeadMe/1-dhlee777
1-dhlee777
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include<iostream> | ||
#include<queue> | ||
using namespace std; | ||
int dt[100001]; // μκ°(λͺμ΄)λ₯Ό μ μ₯νλ λ°°μ΄,μΈλ±μ€λ μλΉμ΄κ° μλ μμΉμ΄λ€. | ||
int k[3] = { -1,1}; //-1,1μ μμΉμ΄λμ λν΄μ£ΌκΈ°μν λ°°μ΄ | ||
queue<int>q; | ||
|
||
void bfs(int start, int end) { | ||
q.push(start); // νμ μ μΌ μ²μ μλΉμ΄μ μμμμΉλ₯Ό λ£λλ€. | ||
while (!q.empty()) { | ||
int c = q.front(); //νμμ μμΉλ₯Ό κΊΌλΈλ€. | ||
q.pop(); | ||
if (c == end) { //νμμ κΊΌλΈ μμΉκ° λμμ μμΉμΌκ²½μ° | ||
cout << dt[end]; //λμμ μ°Ύμ μμΉμμμ μ΅μμκ°μ μΆλ ₯ | ||
return; | ||
} | ||
for (int i = 0; i < 2; i++) { //cκ° κ°μμλ μμΉλ₯Ό μ΄ν΄λ³Έλ€(-1,+1) | ||
int d = c + k[i]; // -1,+1 μ μμΉμ΄λμ ν΄μ€κ°μ dμλ£λλ€. | ||
if (d >= 0 && !dt[d] && d <= 100000) { // λ°©λ¬Ένμ§ μμ μμΉμ΄λ©΄ νμμ μ§ννλ€. | ||
dt[d] = dt[c] + 1; //쑰건μ λ§μ‘±νλ©΄ λ°©κΈνμνμμΉμ μκ°μ κ·Έμ μμΉμμκ° +1μ ν΄μ€λ€. | ||
q.push(d); //bfsλ₯Όμν΄ νμ λ°©κΈ νμνμμΉλ₯Ό λ£λλ€. | ||
} | ||
} | ||
if (c * 2 <= 100000 && !dt[c * 2]) { //*2λ₯Ό ν΅ν νμμ κ²½μ° | ||
dt[c * 2] = dt[c] + 1; //-1,1μκ²½μ°μ λμΌ | ||
q.push(c * 2); | ||
} | ||
} | ||
} | ||
int main(void) { | ||
ios_base::sync_with_stdio(false); | ||
cin.tie(NULL); | ||
int start,end; | ||
cin >> start >> end; //μλΉμ΄μ νμ¬μμΉμ,λμμ νμ¬μμΉλ₯Ό μ λ ₯λ°λλ€. | ||
bfs(start, end); | ||
return 0; | ||
} |