与えられた 2D NxN 行列を同心円として視覚化します。円内の各要素が時計回りと反時計回りの交互の方向にレイヤーごとに 1 位置ずつ回転する回転行列を見つける必要があります。すべての回転は所定の位置にある必要があります。
2 3 4 5
1 6 7 8
4 2 1 9
5 3 2 4
に変換する必要があります
1 2 3 4
4 7 1 5
5 6 2 8
3 2 4 9
解決策を考えた
1>時計回りの円の回転の場合、要素を順番に読み取ります
i -> 0 to n-1 and j = 0
j -> 0 to n-1 and i = n-1
i -> n-1 to 0 and j = n-1
j -> n-1 to 0 and i = 0
2>反時計回りの円の回転の場合、要素を順番に読み取ります
j -> 0 to n-1 and i = 0
i -> 0 to n-1 and j = n-1
j -> n-1 to 0 and i = n-1
i -> n-1 to 0 and j = 0
コード
for(int cnt = 0; cnt < n/2; cnt++)
{
if(cnt%2 == 0) // Clockwise
{
i = cnt; j = cnt;
// while loops for each case
}
else // anti-clockwise
{
i = cnt; j = cnt;
// while loops for each case
}
}
この問題を O(n2) 以上で解決するためのより良い方法はありますか?