알고리즘
-
[알고리즘] 백준 2580번: 스도쿠 - C++알고리즘/백준 2020. 12. 12. 21:46
www.acmicpc.net/problem/2580 2580번: 스도쿠 스도쿠는 18세기 스위스 수학자가 만든 '라틴 사각형'이랑 퍼즐에서 유래한 것으로 현재 많은 인기를 누리고 있다. 이 게임은 아래 그림과 같이 가로, 세로 각각 9개씩 총 81개의 작은 칸으로 이루 www.acmicpc.net #include using namespace std; int s[9][9]; bool c[9][9], c1[9][9], c2[9][9]; int square(int i, int j) { return (i / 3) * 3 + (j / 3); } void sdo(int z) { if (z == 81) { for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { cout
-
[알고리즘] 백준 9663번: N-Queen - C++알고리즘/백준 2020. 12. 12. 21:43
www.acmicpc.net/problem/9663 9663번: N-Queen N-Queen 문제는 크기가 N × N인 체스판 위에 퀸 N개를 서로 공격할 수 없게 놓는 문제이다. N이 주어졌을 때, 퀸을 놓는 방법의 수를 구하는 프로그램을 작성하시오. www.acmicpc.net #include using namespace std; bool a[15][15]; int N, ans; bool check(int row, int col) { int x, y; x = row - 1; y = col; while(x >= 0) { if (a[x][y]) return false; x--; } x = row - 1; y = col - 1; while (x >= 0 && y >= 0) { if (a[x][y]) ret..
-
[알고리즘] 9019번: DSLR - C++알고리즘/백준 2020. 12. 12. 21:42
www.acmicpc.net/problem/9019 9019번: DSLR 네 개의 명령어 D, S, L, R 을 이용하는 간단한 계산기가 있다. 이 계산기에는 레지스터가 하나 있는데, 이 레지스터에는 0 이상 10,000 미만의 십진수를 저장할 수 있다. 각 명령어는 이 레지스터에 www.acmicpc.net #include #include #include using namespace std; int main() { int T; cin >> T; while (T--) { int A, B, now, next; cin >> A >> B; bool check[10000] = { false, }; char how[10000] = { '\0', }; int from[10000] = { 0, }; queue reg..
-
[알고리즘] 백준 10971번: 외판원 순회 2 - C++알고리즘/백준 2020. 12. 12. 21:40
www.acmicpc.net/problem/10971 10971번: 외판원 순회 2 첫째 줄에 도시의 수 N이 주어진다. (2 ≤ N ≤ 10) 다음 N개의 줄에는 비용 행렬이 주어진다. 각 행렬의 성분은 1,000,000 이하의 양의 정수이며, 갈 수 없는 경우는 0이 주어진다. W[i][j]는 도시 i에서 j www.acmicpc.net #include #include #include using namespace std; int main() { int N; cin >> N; vector w(N, vector(N, 0)); vector d(N); for (int i = 0; i > w[i][j]; } ..
-
[알고리즘] 백준 1476번 : 날짜 계산 - C++알고리즘/백준 2020. 12. 12. 21:36
www.acmicpc.net/problem/1476 1476번: 날짜 계산 준규가 사는 나라는 우리가 사용하는 연도와 다른 방식을 이용한다. 준규가 사는 나라에서는 수 3개를 이용해서 연도를 나타낸다. 각각의 수는 지구, 태양, 그리고 달을 나타낸다. 지구를 나타 www.acmicpc.net #include using namespace std; int main() { int E, S, M, e = 1, s = 1, m = 1, ans = 1; cin >> E >> S >> M; while (E != e || S != s || M != m) { ans++; if (e >= 15) e = 1; else e++; if (s >= 28) s = 1; else s++; if (m >= 19) m = 1; else..
-
[알고리즘] 백준 17974번: 모든 순열 - C++알고리즘/백준 2020. 12. 12. 21:35
www.acmicpc.net/problem/10974 10974번: 모든 순열 N이 주어졌을 때, 1부터 N까지의 수로 이루어진 순열을 사전순으로 출력하는 프로그램을 작성하시오. www.acmicpc.net #include #include using namespace std; int main() { int N; int *a; scanf("%d", &N); a = (int *)malloc(sizeof(int)*N); for (int i = 0; i < N; i++) { a[i] = i + 1; printf("%d ", a[i]); } printf("\n"); while (next_permutation(a, a + N)) { for (int i = 0; i < N; i++) { printf("%d ", a[..
-
[알고리즘] 백준 11723번 : 집합 - C++알고리즘/백준 2020. 12. 12. 21:30
www.acmicpc.net/problem/11723 11723번: 집합 첫째 줄에 수행해야 하는 연산의 수 M (1 ≤ M ≤ 3,000,000)이 주어진다. 둘째 줄부터 M개의 줄에 수행해야 하는 연산이 한 줄에 하나씩 주어진다. www.acmicpc.net #include #include int main() { int m, x; char in[7]; int set = 0; scanf("%d", &m); while (m--) { scanf("%s", in); if (!strcmp(in, "add")) { scanf("%d", &x); x--; set = (set | (1
-
[알고리즘] 2차원 배열 90도 회전하기 - C/C++알고리즘/알고리즘 구현 2020. 4. 18. 20:27
void rotate_cw(int M, int array[4][4], int count) { int rotatedArray[4][4]; while (count--) { for (int row = 0; row < M; ++row) { for (int col = 0; col < M; ++col) { rotatedArray[row][col] = array[M - 1 - col][row]; } } for (int row = 0; row < M; ++row) { for (int col = 0; col < M; ++col) { array[row][col] = rotatedArray[row][col]; } } } } void rotate_ccw(int M, int array[4][4], int count) { int..