전체 글 164

백준 1285번 동전 뒤집기( C++ )

문제https://www.acmicpc.net/problem/1285 해설비트마스킹을 통해 모든 경우의 수를 확인2의 40승의 경우의 수가 나와 시간초과가 발생 => 행 or 열 중에 하나만 확인으로 2의 20승을 만들고적은 경우의 수를 택하는 방식 사용코드1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283#include iostream>#include algorithm>using namespace std; const int INF = 987654321;int ret = INF..

백준 19942번 다이어트 ( C++ )

문제https://www.acmicpc.net/problem/19942 해설비트마스킹 관련 기본적인 문제map을 이용해 vector>를 구현코드1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859#include iostream>#include algorithm>#include vector>#include map>using namespace std; const int INF = 987654321;int n, ret = INF, mp, mf, ms, mv, p[19], f[19], s[19], v[19], c[19]; mapint, vectorvectorint>>> ..

백준 1189번 컴백홈 ( C++ )

문제https://www.acmicpc.net/problem/1189 해설백트래킹을 이용한 간단한 탐색 문제코드123456789101112131415161718192021222324252627282930313233343536373839404142434445#include iostream>#include algorithm>using namespace std; int r, c, k, visited[7][7], ret;char mp[7][7];int dx[4] = {0, 1, 0, -1};int dy[4] = {-1, 0, 1, 0}; void dfs(int y, int x, int cnt){  if (cnt == k)  {    if (y == 0 && x == c - 1)    {      ret++;  ..

백준 14620번 꽃길 ( C++ )

문제https://www.acmicpc.net/problem/14620 해설꽃을 심으려면 상하좌우로 1칸씩 필요하기 때문에 n이 10이라면 1~9칸만 확인하면 된다.케이스가 10*10 중에 3개를 뽑기 때문에 대략 100만이여서 완탐으로 풀면 간단하게 풀린다. 코드12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576#include iostream>#include algorithm>using namespace std; int n, mp[14][14], visited[14][14];int ret = 9876..

백준 15684번 사다리 조작 ( C++ )

문제https://www.acmicpc.net/problem/15684 해설반복문을 돌며 사다리타기를 하는 방법을 구상해야함2차원 배열을 통해 가로선의 위치를 기록하는 방식 코드123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869#include bits/stdc++.h>using namespace std; int n, m, h, ret = 7, a, b;int visited[34][14]; bool check(){  for (int i = 1; i = n; i++)  {    int start = i;    for (int j ..

백준 9934번 완전 이진 트리 ( C ++ )

문제https://www.acmicpc.net/problem/9934 해설완전이진트리의 탐색에 대한 문제였음 완전 이진 트리에서 inorder의 경우 절반을 자르면 루트 노드가 나온다는 특징을 이용해야 하는 문제코드12345678910111213141516171819202122232425262728293031323334353637383940414243#include bits/stdc++.h>using namespace std; int k, num[1028];vectorint> ret[14]; void go(int first, int last, int level){  if (last  first)  {    return;  }  if (first == last)  {    ret[level].push_ba..

백준 2529번 알파벳 ( C++ )

문제https://www.acmicpc.net/problem/2529 해설반복문을 돌며 숫자로된 string만 만들면 쉬운 문제 코드123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051#include bits/stdc++.h>using namespace std; int k;char op[10];vectorstring> ret;bool check[10]; bool cal(char a, char b, char c){  if (c == '>')    return a > b;  else    return a  b;} void go(int idx, string tmp){  if (idx == k + ..

백준 1987번 알파벳 ( C++ )

문제https://www.acmicpc.net/problem/1987 해설여러개의 루트 중에 가장 긴 루트를 고르는 문제백트레킹으로 해결 코드1234567891011121314151617181920212223242526272829303132333435363738394041424344454647#include bits/stdc++.h>using namespace std; int r, c;int ret;string line;char mp[24][24];vectorchar> ret_temp;int dy[4] = {-1, 0, 1, 0};int dx[4] = {0, 1, 0, -1}; void dfs(int y, int x){  ret_temp.push_back(mp[y][x]);   for (int i = 0..

백준 3197번 백조의 호수 ( C++ )

문제https://www.acmicpc.net/problem/3197 해설백조가 있는 곳에서 부터 얼음이 녹는것이 아닌 물이 있는곳 모든 곳에서 부터 얼음이 녹는것이 포인트그렇기 때문에 얼음이 녹는것과 백조가 이동하는것을 따로 관리해야한다.또한 백조가 있는곳은 물 위이기 때문에 백조가 있는곳도 물으로 처리해야 한다. 코드123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102#include bits/stdc++..

백준 14497번 주난의 난 ( C++ )

문제https://www.acmicpc.net/problem/14497해설외각을 돌며 껍질 벗기는 느낌으로 푸는 문제visited를 초기화 해주는 방법으로 해결했음코드123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172#include bits/stdc++.h>using namespace std; char mp[304][304];int ret, x1, x2, y11, y2, n, m, flag;int dx[4] = {0, 1, 0, -1};int dy[4] = {-1, 0, 1, 0};int visited[304][30..