코딩테스트/문제풀이 36
-
문제 풀이#include #include using namespace std;int box[1002][1002];int dist[1002][1002];int n, m, result;int dy[4] = { -1, 1, 0, 0 };int dx[4] = { 0, 0, -1, 1 };int main(){ cin >> m >> n; for (int y = 0; y > box[y][x]; dist[y][x] = -1; } } queue> q; for (int y = 0; y = n || nx = m) { continue; } if (box[ny][nx] == -1 || dist[ny][nx] != -1) { continue; } } } int result = 0; for (..
-
문제 풀이#include #include #include using namespace std;int maze[102][102];int dist[102][102];int n, m;int dx[4] = { -1, 1, 0, 0 };int dy[4] = { 0, 0, -1, 1 };int bfs() { queue> q; q.push({ 0, 0 }); dist[0][0] = 1; while (!q.empty()) { int x = q.front().first; int y = q.front().second; q.pop(); for (int dir = 0; dir = n || ny = m) continue; if (dist..
-
-
문제 풀이#include #include using namespace std;int paper[502][502];bool visited[502][502];int n, m;// 상, 하, 좌, 우 이동 방향int dx[4] = { -1, 1, 0, 0 };int dy[4] = { 0, 0, -1, 1 };int bfs(int x, int y){ queue> q; q.push({ x, y }); visited[x][y] = true; int area = 1; while (!q.empty()) { int cur_x = q.front().first; int cur_y = q.front().second; q.pop(); for (int i = 0; i = n || ny >= m) continue; ..
-
-
-
문제 풀이#include #include using namespace std;int main(){ ios::sync_with_stdio(false); cin.tie(0); int n = 0; queue q; cin >> n; // 큐 채우기 for (int i = 1; i 1) { // 우선 제일 위에 있는 카드를 버린다. q.pop(); // 제일 위에 있는 카드를 제일 아래로 옮긴다. (push 한뒤 pop) q.push(q.front()); q.pop(); } cout 풀이문제 속 요구사항을 순서대로 구현하면 되는 간단한 문제다. 제일 먼저 넣은 카드가 먼저 나가기 때문에 큐를 사용해서 풀 수 있다. 1. 우선 큐를 순서대로 채운 뒤,2. N만큼 반복하면서3. 제일 위에 있..
-
문제 풀이#include using namespace std;int main() { ios::sync_with_stdio(false); cin.tie(0); int n = 0; cin >> n; for (int y = 1; y 메모예제 입출력 부분을 살펴보면 세로에 해당하는 y 값은 1씩 증가하며 가로에 해당하는 x의 경우 i번째 줄에서 공백의 개수는 N - i개가 되고, 별의 개수는 2 * i - 1개가 된다.따라서 입력받은 n까지 y값을 하나씩 증가시키면서 내부에서 2개의 for문을 따로 돌려서 각각 공백과 별을 출력하도록 한다. 입력받은 N을 기준으로 공백과 별을 출력하며 이때 개수는 N에 비례하기 때문에 이 코드의 전체 시간 복잡도는 O(n^2)가 된다.다만 입력 부분..