competitive-programmingFeb 27, 20211 min read
[BOJ] 14226. Emoticon Explained
Updated: Feb 27, 2021
Breadth first search
Available in:enko
#Problem
#Approach
A typical bfs problem.
The data structure of the element that goes into the queue:
Start from , and at each step additionally explore the following search spaces.
- If , print and terminate.
- If and , explore
- If and , explore
- If , explore
#Code

/**
* author: jooncco
* written: 2021. 2. 27. Sat. 22:23:27 [UTC+9]
**/
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <string.h>
#include <vector>
#include <deque>
#include <queue>
#include <set>
#include <cmath>
using namespace std;
#define FAST_IO ios_base::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef deque<int> di;
typedef deque<ii> dii;
const int LIMIT= 1001;
int S;
bool visited[1010][1010]= {0};
int main() {
FAST_IO;
cin >> S;
queue<pair<ii,int>> Q;
Q.push(make_pair({1,0},0));
int ans= 0;
while (!Q.empty()) {
pair<ii,int> cur= Q.front(); Q.pop();
int s= cur.first.first, clip= cur.first.second;
int cnt= cur.second;
if (s == S) {
ans= cnt;
break;
}
if ( s+clip <= LIMIT && !visited[s+clip][clip] ) visited[s+clip][clip]= 1, Q.push( make_pair({s+clip,clip}, cnt+1) );
if ( !visited[s][s] ) visited[s][s]= 1, Q.push( make_pair({s,s}, cnt+1));
if ( s > 1 && !visited[s-1][clip] ) visited[s-1][clip]= 1, Q.push( {make_pair(s-1,clip}, cnt+1) );
}
cout << ans;
}
#Complexity
- Time:
- Space: