日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HackerRank [Algo] Matrix Rotation

發(fā)布時間:2023/12/20 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HackerRank [Algo] Matrix Rotation 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

原題網(wǎng)址:https://www.hackerrank.com/challenges/matrix-rotation-algo

You are given a 2D matrix,?a, of dimension?MxN?and a positive integer?R. You have to rotate the matrix?R?times and print the resultant matrix. Rotation should be in anti-clockwise direction.

Rotation of a?4x5?matrix is represented by the following figure. Note that in one rotation, you have to shift elements by one step only (refer sample tests for more clarity).

It is guaranteed that the minimum of?M?and?N?will be even.

Input Format?
First line contains three space separated integers,?M,?N?and?R, where?M?is the number of rows,?N?is number of columns in matrix, and?R?is the number of times the matrix has to be rotated.?
Then?M?lines follow, where each line contains?N?space separated positive integers. These?M?lines represent the matrix.

Output Format?
Print the rotated matrix.

Constraints?
2 <=?M,?N?<= 300?
1 <=?R?<= 109?
min(M, N) % 2 == 0?
1 <=?aij?<= 108, where?i?∈?[1..M]?&?j?∈?[1..N]

Sample Input #00

4 4 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Sample Output #00

2 3 4 8 1 7 11 12 5 6 10 16 9 13 14 15

Sample Input #01

4 4 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Sample Output #01

3 4 8 12 2 11 10 16 1 7 6 15 5 9 13 14

Sample Input #02

5 4 7 1 2 3 4 7 8 9 10 13 14 15 16 19 20 21 22 25 26 27 28

Sample Output #02

28 27 26 25 22 9 15 19 16 8 21 13 10 14 20 7 4 3 2 1

Sample Input #03

2 2 3 1 1 1 1

Sample Output #03

1 1 1 1

Explanation?
Sample Case #00:?Here is an illustration of what happens when the matrix is rotated once.

1 2 3 4 2 3 4 85 6 7 8 1 7 11 129 10 11 12 -> 5 6 10 16 13 14 15 16 9 13 14 15

Sample Case #01:?Here is what happens when to the matrix after two rotations.

1 2 3 4 2 3 4 8 3 4 8 125 6 7 8 1 7 11 12 2 11 10 169 10 11 12 -> 5 6 10 16 -> 1 7 6 15 13 14 15 16 9 13 14 15 5 9 13 14

Sample Case #02:?Following are the intermediate states.

1 2 3 4 2 3 4 10 3 4 10 16 4 10 16 22 7 8 9 10 1 9 15 16 2 15 21 22 3 21 20 28 13 14 15 16 -> 7 8 21 22 -> 1 9 20 28 -> 2 15 14 27 -> 19 20 21 22 13 14 20 28 7 8 14 27 1 9 8 26 25 26 27 28 19 25 26 27 13 19 25 26 7 13 19 2510 16 22 28 16 22 28 27 22 28 27 26 28 27 26 254 20 14 27 10 14 8 26 16 8 9 25 22 9 15 193 21 8 26 -> 4 20 9 25 -> 10 14 15 19 -> 16 8 21 132 15 9 25 3 21 15 19 4 20 21 13 10 14 20 71 7 13 19 2 1 7 13 3 2 1 7 4 3 2 1

Sample Case #03:?As all elements are same, any rotation will reflect the same matrix.


方法:與一維數(shù)組平移相同,只需要將一維坐標(biāo)映射到二維。

import java.io.*; import java.util.*;public class Solution {private static int getY(int left, int top, int right, int bottom, int pos) {if (pos < bottom - top) return top + pos;if (pos < bottom - top + right - left) return bottom;if (pos < (bottom - top) * 2 + right - left) return top + ((bottom - top) * 2 + (right - left)) - pos;return top; }private static int getX(int left, int top, int right, int bottom, int pos) {if (pos < bottom - top) return left;if (pos < bottom - top + right - left) return left + pos - (bottom - top);if (pos < (bottom - top) * 2 + right - left) return right;return right - (pos - ((bottom - top) * 2 + (right - left)));}private static void reverse(int[][] matrix, int left, int top, int right, int bottom, int from, int to) {for(int i = from, j = to; i < j; i++, j--) {int iy = getY(left, top, right, bottom, i);int ix = getX(left, top, right, bottom, i);int jy = getY(left, top, right, bottom, j);int jx = getX(left, top, right, bottom, j);int t = matrix[iy][ix];matrix[iy][ix] = matrix[jy][jx];matrix[jy][jx] = t;}}private static void rotate(int[][] matrix, int left, int top, int right, int bottom, int rotate) {int length = (right - left) * 2 + (bottom - top) * 2;rotate %= length;if (rotate == 0) return;reverse(matrix, left, top, right, bottom, 0, length - 1 - rotate);reverse(matrix, left, top, right, bottom, length - rotate, length - 1);reverse(matrix, left, top, right, bottom, 0, length - 1); }public static void main(String[] args) {/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */Scanner scanner = new Scanner(System.in);int m = scanner.nextInt();int n = scanner.nextInt();int r = scanner.nextInt();int[][] matrix = new int[m][n];for(int i = 0; i < m; i++) {for(int j = 0; j < n; j++) {matrix[i][j] = scanner.nextInt();}}int left = 0, top = 0, right = n - 1, bottom = m - 1;while (left < right && top < bottom) {rotate(matrix, left, top, right, bottom, r);left++;top++;right--;bottom--;}for(int i = 0; i < m; i++) {for(int j = 0; j < n; j++) {if (j > 0) System.out.print(" ");System.out.print(matrix[i][j]);}System.out.println();}} }

總結(jié)

以上是生活随笔為你收集整理的HackerRank [Algo] Matrix Rotation的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。