原题网址: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, MN 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 <= MN <= 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.

方法:与一维数组平移相同,只需要将一维坐标映射到二维。

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();}}
}

HackerRank [Algo] Matrix Rotation相关推荐

  1. python俄罗斯方块算法详解_用 Python 写一个俄罗斯方块游戏 (

    @@ -2,34 +2,34 @@ > * 原文作者:[Dr Pommes](https://medium.com/@pommes) > * 译文出自:[掘金翻译计划](https://g ...

  2. 微软等数据结构+算法面试100题全部答案集锦

    微软等数据结构+算法面试100题全部答案集锦 作者:July.阿财. 时间:二零一一年十月十三日. 引言 无私分享造就开源的辉煌. 今是二零一一年十月十三日,明日14日即是本人刚好开博一周年.在一周年 ...

  3. 冯乐乐 unity_Unity常用矩阵运算的推导补遗——切线空间

    在上一篇文章中,我写了一些关于Unity中各个坐标空间及其转换矩阵是如何得到的,说实在的,我是那种"记忆需要依靠外部装置存储"类.如同<攻壳机动队>的电子脑一样的人,每 ...

  4. 【VINS-MONO测试】安卓手机采集mono+imu数据

    上一个vins-mono环境配置测试完成后,初步建立好实验环境,接下来开始进行数据采集(cam+imu).打包.标定.运行. 记录一下后续要更新的内容吧: ros打包.另外两种标定方式.手机在线测试. ...

  5. Unity加载火炬之光的场景

    原文  http://blog.csdn.net/langresser_king/article/details/38423793 因为一些基础的数学问题,前前后后一共研究了四五天,今天终于有些眉目了 ...

  6. ROS实验笔记之——VINS-Mono在l515上的实现

    之前博客<ROS实验笔记之--Intel Realsense l515激光相机的使用>实现了用l515运行RTABmap,本博文试试在l515上实现vins-mono 首先需要将vins- ...

  7. 微软面试100题(含全部答案)

    引言 无私分享造就开源的辉煌. 今是二零一一年十月十三日,明日14日即是本人刚好开博一周年.在一周年之际,特此分享出微软面试全部100题答案的完整版,以作为对本博客所有读者的回馈. 一年之前的10月1 ...

  8. 微软面试100题2010年版全部答案集锦

      微软等数据结构+算法面试100题全部答案集锦 作者:July.阿财. 时间:二零一一年十月十三日. 引言 无私分享造就开源的辉煌. 今是二零一一年十月十三日,明日14日即是本人刚好开博一周年.在一 ...

  9. 重磅分享:微软面试100题2010年版全部答案完整亮相

    欢迎zhangxusoftgcy我的:资源博客空间简历设置|帮助|退出 CSDN首页 资讯 论坛 博客 下载 搜索 更多 CTO俱乐部 学生大本营 培训充电 移动开发 软件研发 云计算 程序员 TUP ...

  10. 微软等数据结构+算法面试100题全部答案完整亮相

    重磅分享:微软等数据结构+算法面试100题全部答案完整亮相 来源: 王永刚的日志 本文转载自CSDN大牛的一篇博客:http://blog.csdn.net/v_july_v/article/deta ...

最新文章

  1. SAP Batch Management 批次主数据中classification视图中GR Date没有被更新?
  2. window下git的使用
  3. 第三讲:WCF介绍(3)
  4. Android 通信--蓝牙
  5. 服务机器人---建图工具
  6. 把内表 itab1 的 n1 到 n2 行内容附加到 itab2 内表中去.
  7. 结合webpack配置_前端 Webpack 工程化的最佳实践
  8. OCR系列——总体概述
  9. Java 汉字转拼音 Scala 汉字转拼音
  10. 最小生成树基础 (Kruskal)
  11. c# key event
  12. php分布式gearman,使用Gearman搭建分布式任务分发平台
  13. 腾讯、阿里、搜狐、人人、去哪儿、迅雷等互联网企业产品笔试题目(附个人答案)
  14. php传奇发布站,传奇发布网站php源码
  15. 即使被拖库,也可以保证密码不泄露(一种安全的加密方案)
  16. fg-bg Assignment Imbalance problem
  17. 十一、如何挑选股票?
  18. PyPDF2读取PDF文件内容保存到本地TXT
  19. 奔驰A200L升级内饰氛围灯,发光涡轮等,绚丽多彩,温馨又惬意
  20. 【java面对对象】分数类型加减乘除运算的实现

热门文章

  1. 基于单片机智能自动浇花控制系统设计(毕业设计)
  2. python题目59:员工考勤记录
  3. 未安装任何音频输出设备
  4. html设计学校网站,基于HTML5的学校网站设计.doc
  5. 【有图有真相】全国软考高级三连冠感悟
  6. 金蝶KIS/K3各版本下载地址
  7. python绘图3d_超好看的3D绘图方式,Python厉害了!
  8. AutoCAD2004下载AutoCAD2004中文版安装教程
  9. 编译原理-第四版-刘铭---期末复习
  10. WAP网站制作(WAP网站建设)全攻略教程二