top of page

CCC 2018 J4/S2: Sunflowers

Updated: Oct 30, 2021

Matrix rotated anti clockwise 90 degree, next 90 degree (180 degree), next 90 degress(270 degree), next 90 degree(360 degree).


Look the example:

Assume one matrix is:

1 2 3

4 5 6

7 8 9

This is anti clockwise 90 degree

3 6 9

2 5 8

1 4 7

This is anti clockwise 90 degree

9 8 7

6 5 4

3 2 1

This is anti clockwise 90 degree

7 4 1

8 5 2

9 6 3

this is anti clockwise 90 degree

1 2 3

4 5 6

7 8 9



#include <iostream>

using namespace std;


int main() {

int N;

cin >> N;

int A[N][N];

for(int i = 0; i < N; i++){

for(int j = 0; j < N; j++){

cin >> A[i][j];

}

}

if(N >= 2){

while(true){

if (A[0][0] < A[0][N-1] &&

A[0][0] < A[N-1][0] &&

A[N-1][0] < A[N-1][N-1] &&

A[0][N-1] < A[N-1][N - 1]){

break;

}

for(int i = 0; i < N; i++){

for(int j = i; j < N; j++){

int t = A[i][j];

A[i][j] = A[j][i];

A[j][i] = t;

}

}

int i = 0;

int j = N-1;

while(i < j){

for(int k = 0; k < N; k++){

int t = A[i][k];

A[i][k] = A[j][k];

A[j][k] = t;

}

i++;

j--;

}




}


}

for(int i = 0; i < N; i++){

for(int j = 0; j < N; j++){

cout<<A[i][j]<<" ";

}

cout<<endl;

}


}



N = int(input())

clist = []

for i in range(N):
    cur = []
    s = input().split()
    for j in range(N):
        cur.append(int(s[j]))
    clist.append(cur)


def rotateAclockwise90():
    if N < 2:
        return
    for i in range(N):
        for j in range(i, N):
            t = clist[i][j]
            clist[i][j] = clist[j][i]
            clist[j][i] = t
    i = 0
    j = N -1
    while i < j:
        for k in range(N):
            t = clist[i][k]
            clist[i][k] = clist[j][k]
            clist[j][k] = t
        i += 1
        j -= 1


for i in range(4):
    if clist[0][0] < clist[0][N-1] and \
       clist[0][0] < clist[N-1][0] and \
       clist[N-1][0] < clist[N-1][N-1] and \
       clist[0][N-1] < clist[N-1][N - 1]:
        break
    rotateAclockwise90()

for x in clist:
    print(*x, sep=' ')

Recent Posts

See All

CCC '24 J5 - Harvest Waterloo

#include<iostream> #include <vector> #include <algorithm> #include <cmath> #include <stack> using namespace std; int main() { int r, c, sr, sc; cin >> r; cin >> c; int p[r][c]; bool v

CCC '24 J4 - Troublesome Keys

#include <iostream> #include <string> #include <map> #include <vector> #include <algorithm> #include <cmath> using namespace std; int main() { string ps; string ds; cin >> ps; cin >> ds;

CCC '22 J5 - Square Pool

#include<iostream> #include <vector> #include <algorithm> #include <cmath> using namespace std; bool rowcom(pair<int, int> a, pair<int, int> b){ return a.first < b.first; } bool colcom(pair<int,

Comments


bottom of page