본문 바로가기

알고리즘/백준 BOJ17

1003. 피보나치 함수 1. 시간초과된 풀이 더보기 #include #include #define endl "\n" using namespace std; int T, N; vector testCase; int cnt_zero = 0, cnt_one = 0; void Input() { cin >> T; for(int i = 0; i > N; testCase.push_back(N); } } int fibonacci(int n) { if(n == 0) { cnt_zero += 1; return 0; } else if(n == 1) { cnt_one += 1; return 0; } else{ return fibonacci(n-1) + fibonacci(n-2); } } void Solution() { f.. 2022. 8. 2.
2667. 단지번호 붙이기 1. 해결방법 BFS 이용해서 "한뭉텅이탐색" map[][]을 입력받을 때, 집이 존재하는 위치에서 부터 BFS탐색하기 2. Solution #include #include #include #include #include #define MAX 25 #define endl '\n' using namespace std; int dx[] = {0, 0, -1, 1}; int dy[] = {1, -1, 0, 0}; int N, total = 0; int map[MAX][MAX]; bool visited[MAX][MAX]; vector cnt; vector v; void Input() { cin >> N; int house; for(int i = 0; i > I.. 2022. 7. 9.
1697. 숨바꼭질 1. 해결방법 수빈이 처음 위치(N)에서 BFS 돌리기 (BFS 돌리면 최단거리 나오기 때문) 2. Solution #include #include #define endl "\n" #define MAX 100000+1 using namespace std; int dx[] = {-1, 1}; int N, K; bool visited[MAX]; void Input() { cin >> N >> K; } void BFS(int X) { queue q; q.push(make_pair(X, 0)); visited[X] = true; while(!q.empty()) { int x = q.front().first; int t = q.front().second; q.pop(); if(x == K) { cout 2022. 7. 6.
1012. 유기농 배추 1. 해결방법 배추가 있는 위치에서 BFS 탐색한다. 1) 배추 위치 저장 2) 배추 위치 개수 v.size() 만큼 BFS BFS는 Queue에 한뭉텅이 원소들을 push()하며,원소의 주변을 탐색한다. 2. Solution #include #include #include #include #define endl "\n" #define MAX 50 using namespace std; int T, M, N, K, Answer; int map[MAX][MAX]; bool visit[MAX][MAX]; int dx[] = {0, 0, -1, 1}; int dy[] = {1, -1, 0, 0}; vector v; void Initialize(){ Answer = 0; memset(map, 0, sizeof(.. 2022. 7. 3.