Problem/시뮬레이션
[C/C++] BOJ 3048 :: 개미
지무룩
2019. 3. 20. 22:40
BOJ 3048 :: 개미
문제 링크 : https://www.acmicpc.net/problem/3048
나의 풀이
개미의 정보를 담을 구조체를 만든다.
dir : 개미의 방향, 0 : 오른쪽으로 이동하는 그룹, 1 : 왼쪽으로 이동하는 그룹
alpha : 개미의 알파벳
반복문을 돌며 오른쪽으로 이동중인 개미가 왼쪽으로 이동중인 개미와 마주쳤을 때 둘의 위치를 바꾼다.
나의 코드
Github : https://github.com/j2wooooo/Daliy_Algorithms/blob/master/Daliy_Algorithms/BOJ_3048/BOJ_3048.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | // 백준알고리즘 3048번 :: 개미 #include<iostream> using namespace std; struct info{ int dir; char alpha; }; int N1, N2, T; struct info ans[27]; int main() { char ant = 0; cin >> N1 >> N2; for (int i = 0; i < N1; i++) { cin >> ant; ans[N1-1 -i] = { 0, ant }; } for (int i = N1; i < N1+N2; i++) { cin >> ant; ans[i] = { 1, ant }; } cin >> T; for (int i = 0; i < T; i++) { for (int j = 0; j < N1 + N2 - 1; j++) { if (ans[j].dir != ans[j + 1].dir && ans[j].dir == 0) { struct info temp = ans[j]; ans[j] = ans[j + 1]; ans[j + 1] = temp; j++; } } } for (int i = 0; i < N1 + N2; i++) cout << ans[i].alpha; return 0; } | cs |