백준 14226 - 이모티콘
문제
백준 14226 - 이모티콘 풀러가기
문제 분석
모든 연산은 1초 가 걸리고, 걸리는 시간의 최솟값 을 구해야 하는 문제다. 따라서 bfs를 이용하여 풀 수 있다.
문제 풀이
-
전체 코드
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364#include <cstdio>#include <queue>#include <cstring>#include <algorithm>using namespace std;int cnt[1001][1001];int main() {int s;scanf("%d", &s);memset(cnt, -1, sizeof(cnt));queue<pair<int,int>> q;cnt[1][0] = 0;q.push(make_pair(1, 0));int time = 1001;while (!q.empty()) {int screen = q.front().first;int clip = q.front().second;if (screen + clip < 1001 && cnt[screen+clip][clip] ==-1) {cnt[screen + clip][clip] = cnt[screen][clip] + 1;if (screen + clip == s) {if (time > cnt[screen+clip][clip]) {time = cnt[screen+clip][clip];}}q.push(make_pair(screen + clip, clip));}if (cnt[screen][screen] == -1) {cnt[screen][screen] = cnt[screen][clip] + 1;q.push(make_pair(screen, screen));}if (screen - 1 >= 0 && cnt[screen - 1][clip] == -1) {cnt[screen - 1][clip] = cnt[screen][clip] + 1;if (screen - 1 == s) {if (time > cnt[screen-1][clip]) {time = cnt[screen-1][clip];}}q.push(make_pair(screen-1, clip));}q.pop();}printf("%d", time);return 0;}cs
아직 배움의 과정에 있는 학생이니 내용에 부족한 점이 보이면 지적은 하되, 비난은 하지 말아주세요!!
댓글남기기