Array Rotating technic

이영구·2022년 12월 29일
0

Algorithm

목록 보기
19/19

Array 배열을 rotating 하는 문제는 심심치 않게 기본 동작으로 처리하는 문제가 나오는 경우가 많다. Python을 이용하면 zip operation을 통해서 array rotating 원리를 파악하지 않고도 비교적 쉽게 구현이 가능하다.
나중에 구현할 때 참고하도록 하자~!!

  • python rotating operation with zip
a = [list(map(int, rdln().split())) for _ in range(n)]

b = [list(reversed(x)) for x in list(zip(*a))] # 90도 회전
c = [x[::-1] for x in a[::-1]] # 180도 회전
d = [x[::-1] for x in b[::-1]] # 270도 회전 
  • C++ rotating operation with rotating logic
void rotate(int y, int x, int L){ // g_coordinate (y, x)
    for(int i=0;i<L;i++)
        for(int j=0;j<L;j++)
            temp[i][j]=board[y+L-1-j][x+i]; // +90 rotation
            temp[i][j]=board[y+j][x+n-1-j]; // -90 rotation
    for(int i=0;i<L;i++)
        for(int j=0;j<L;j++)
            board[y+i][x+j]=temp[i][j];
}
profile
In to the code!

0개의 댓글