-
[C++] 백준 16935 배열 돌리기 3알고리즘/백준 2022. 12. 24. 15:21반응형
#include <iostream> using namespace std; int n,m,r; int rem[101][101]; int arr[101][101]; //row == 행 column = 열 int row,column; void input() { cin >> n >> m >> r; row = n; column = m; for(int i=1; i<=n; i++) { for(int j=1; j<=m; j++) { cin >> arr[i][j]; } } } void printArr() { for(int i=1; i<=row; i++) { for(int j=1; j<=column; j++) { cout << arr[i][j] << " "; } cout << "\n"; } } void turnArr(int tmp) { if(tmp == 1) { for(int i=1; i<=row; i++) { for(int j=1; j<=column; j++) { rem[i][j] = arr[row-i+1][j]; } } } else if(tmp == 2) { for(int i=1; i<=row; i++) { for(int j=1; j<=column; j++) { rem[i][j] = arr[i][column-j+1]; } } } else if(tmp == 3) { int tmp = row; row = column; column = tmp; for(int i=1; i<=row; i++) { for(int j=1; j<=column; j++) { rem[i][j] = arr[column-j+1][i]; } } } else if(tmp == 4) { int tmp = row; row = column; column = tmp; for(int i=1; i<=row; i++) { for(int j=1; j<=column; j++) { rem[i][j] = arr[j][row-i+1]; } } } else if(tmp == 5) { for(int i=1; i<=row/2; i++) { for(int j=1; j<=column/2; j++) { rem[i][j] = arr[row/2+1+i-1][j]; } } for(int i=1; i<=row/2; i++) { for(int j=column/2+1; j<=column; j++) { rem[i][j] = arr[i][j-column/2]; } } for(int i=row/2+1; i<=row; i++) { for(int j=1; j<=column/2; j++) { rem[i][j] = arr[i][j+column/2]; } } for(int i=row/2+1; i<=row; i++) { for(int j=column/2+1; j<=column; j++) { rem[i][j] = arr[i-row/2][j]; } } } else if(tmp == 6) { for(int i=1; i<=row/2; i++) { for(int j=1; j<=column/2; j++) { rem[i][j] = arr[i][j+column/2]; } } for(int i=1; i<=row/2; i++) { for(int j=column/2+1; j<=column; j++) { rem[i][j] = arr[i+row/2][j]; } } for(int i=row/2+1; i<=row; i++) { for(int j=1; j<=column/2; j++) { rem[i][j] = arr[i-row/2][j]; } } for(int i=row/2+1; i<=row; i++) { for(int j=column/2+1; j<=column; j++) { rem[i][j] = arr[i][j-column/2]; } } } } void copyArr() { for(int i=1; i<=row; i++) { for(int j=1; j<=column; j++) { arr[i][j] = rem[i][j]; } } } void solve() { int tmp; for(int i=0; i<r; i++) { cin >> tmp; turnArr(tmp); copyArr(); } } int main() { input(); solve(); printArr(); }
주어진 조건에 맞게 구현만 하면 되는 문제
배열을 하나만 선언해놓고 하면 상당히 복잡하기 때문에 배열 두개를 선언해 변경 전, 후를 구분하는게 포인트다
입출력, 배열복사, 풀이를 한곳에서 하려면 지역변수 이름짓기도 힘들고 꼬이는 부분이 많기 때문에 기능단위로 나눠서 풀이해주는 습관을 들여보자
728x90반응형'알고리즘 > 백준' 카테고리의 다른 글
[C++] 백준 1043 거짓말 (1) 2023.01.04 [C++] 백준 10836 여왕벌 (0) 2023.01.04 백준 18352 특정 거리의 도시 찾기 c++ (0) 2022.12.24 백준 1446 지름길 c++ (0) 2022.12.24 백준 23843 콘센트 c++ (1) 2022.12.23