旋转二维数组

 

   1:      /// <summary>
   2:      /// Q:Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, 
   3:      /// write a method to rotate the image by 90 degrees. Can you do this in place?
   4:      /// </summary>
   5:      class Program
   6:      {
   7:          static void Main(string[] args)
   8:          {
   9:              Program p = new Program();
  10:              int[,] matrix = new int[3, 4] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
  11:              int[,] matrix2 = new int[4, 4] { { 1, 1, 1, 1 }, { 2, 2, 2, 2 }, { 3, 3, 3, 3 },{4,4,4,4} };
  12:              int[,] matrix3 = new int[4, 4] { { 1, 2, 3, 4 }, { 1, 2, 3, 4 }, { 1, 2, 3, 4 }, { 1, 2, 3, 4 } };
  13:              int[,] matrix4 = new int[3,3] { { 1, 2, 3 }, { 4,5,6 }, { 7,8,9 } };
  14:              int[,] matrix5 = new int[4, 4] { { 1, 2, 3,4 }, { 5,6,7,8 }, { 9,10,11,12 },{13,14,15,16} };
  15:              int[,] matrix6 = new int[3, 2] { { 1, 2}, { 3,4},{5,6} };
  16:              int[,] result = p.RotateMatrixWith90Degree(matrix);
  17:              result = p.RotateMatrixWith90DegreeInHour(matrix);
  18:              PrintMatrix(matrix5);
  19:              p.RotateMatrixWith90DegreeInPlace(ref matrix5);
  20:              PrintMatrix(matrix5);
  21:              int[,] t = (int[,])ResizeMultipleDimensionArray(matrix6, new int[] { 2,3});
  22:              PrintMatrix(t);
  23:              Console.ReadKey();
  24:          }
  25:   
  26:          /// <summary>
  27:          /// 第一种方法,很自然想到定义一个新的数组, 旋转后的元素存在新数组中
  28:          /// 该方法不受矩阵形状限制,可以是非方形矩阵
  29:          /// </summary>
  30:          public int[,] RotateMatrixWith90Degree(int[,] matrix)
  31:          {
  32:              int line = matrix.GetLength(0);
  33:              int column = matrix.GetLength(1);
  34:              if (line == 0 || column == 0)
  35:              {
  36:                  throw new ArgumentNullException("matrix is empty,either its line or column length is 0");
  37:              }
  38:   
  39:              int[,] newMatrix = new int[column, line];
  40:              for (int i = 0;i< line; i++)
  41:              {
  42:                  for (int j = 0; j < column; j++)
  43:                  {
  44:                      newMatrix[j, line-i-1] = matrix[i, j];
  45:                  }
  46:              }
  47:              return newMatrix;
  48:          }
  49:   
  50:          /// <summary>
  51:          /// 逆时针旋转90度
  52:          /// </summary>
  53:          public int[,] RotateMatrixWith90DegreeInHour(int[,] matrix)
  54:          {
  55:              int line = matrix.GetLength(0);
  56:              int column = matrix.GetLength(1);
  57:              if (line == 0 || column == 0)
  58:              {
  59:                  throw new ArgumentNullException("matrix is empty,either its line or column length is 0");
  60:              }
  61:   
  62:              int[,] newMatrix = new int[column, line];
  63:              for (int i = 0; i < line; i++)
  64:              {
  65:                  for (int j = 0; j < column; j++)
  66:                  {
  67:                      newMatrix[column-j-1, i] = matrix[i, j];
  68:                  }
  69:              }
  70:              return newMatrix;
  71:          }
  72:   
  73:          /// <summary>
  74:          /// in-place旋转
  75:          /// 假设条件:方形矩阵
  76:          /// </summary>
  77:          public void RotateMatrixWith90DegreeInPlace(ref int[,] matrix)
  78:          {
  79:              int line = matrix.GetLength(0);
  80:              int column = matrix.GetLength(1);
  81:              if (line == 0 || column == 0)
  82:              {
  83:                  throw new ArgumentNullException("matrix is empty,either its line or column length is 0");
  84:              }
  85:              else if(line!=column)
  86:              {
  87:                  throw new ArgumentException("matrix is not N*N");
  88:              }
  89:              for (int i = 0; i < line/2; i++)
  90:              {
  91:                  for (int j = i; j < line-1-i; j++)
  92:                  {
  93:                      int lefttop = matrix[i, j];
  94:                      matrix[i, j] = matrix[line - j - 1,i];
  95:                      matrix[line - j - 1, i] = matrix[line - i - 1, line - j - 1];
  96:                      matrix[line - i - 1, line - j - 1] = matrix[j, line - i - 1];
  97:                      matrix[j, line - i - 1] = lefttop;
  98:                  }
  99:              }
 100:          }
 101:   
 102:          public static Array ResizeMultipleDimensionArray(Array arr, int[] newSizes)
 103:          {
 104:              Array newArr=Array.CreateInstance(arr.GetType().GetElementType(), newSizes);
 105:              int length = arr.Length < newArr.Length ? arr.Length : newArr.Length;
 106:              Array.ConstrainedCopy(arr, 0, newArr, 0, length);
 107:              return newArr;
 108:          }
 109:   
 110:          /// <summary>
 111:          /// Print the matrix to console
 112:          /// </summary>
 113:          public static void PrintMatrix(int[,] matrix)
 114:          {
 115:              int line = matrix.GetLength(0);
 116:              int column = matrix.GetLength(1);
 117:              if (line == 0 || column == 0)
 118:              {
 119:                  Console.WriteLine("empty array!");
 120:              }
 121:              else
 122:              {
 123:                  Console.WriteLine("lines:" + line.ToString());
 124:                  Console.WriteLine("columns:" + column.ToString());
 125:              }
 126:              for (int i = 0; i < line; i++)
 127:              {
 128:                  Console.WriteLine();
 129:                  for (int j = 0; j < column; j++)
 130:                  {
 131:                      Console.Write(matrix[i, j].ToString().PadLeft(5));
 132:                      Console.Write(" ");
 133:                  }
 134:              }
 135:          }
 136:      }
posted @ 2014-01-07 18:51  Dance With Automation  Views(277)  Comments(0Edit  收藏  举报