蛇形矩阵:
非常直接就是像蛇一样的矩阵,不过在算法题里面,这条蛇的扭曲方法不一样,就此诞生了很多各种各样的算法题。
然后有一个sample
这里用的方法还是观察了这个矩阵,find对角线的规律, 这里分了基数对角线和偶数对角线,基数对角线和偶数一个是赠一个是减,其次再有一些特殊的情况就是边界问题,加了几个if判断一下就OK
C++版本
#include<iostream>
using namespace std;
void print_arrray(int n, int m, int **a)
{
/* a=new int*[n];
for(int i=0;i<m;i++)
{
a[i]=new int[i];
}*/
int count = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
count++;
cout << a[i][j] << "\t";
if (count == m )
{
cout << endl;
count = 0;
}
}
}
}
int** getGrid(int n, int m, int **a)
{
int **b = new int*[n];
for(int i=0;i<n;i++)
{
b[i] = new int[m];
}
int num = 1;
int row = 0, col = m - 1;
//int x = 1, y = 3;
//b[2][y] = 6;
//cout<<b[x][y]<<endl;
//a[n][col] = 6;//row ±ØÐëÈ·¶¨Öµ
// a[row][col]=1;
//cout << n << m << endl;
bool isRow = false; //false is even true is odd
for (int i = 0; i < n+m - 1; i++) //fenzu
{
//cout << "hello_1" << endl;
// row = i;
while (row>=0&&row <= n - 1 && col>=0&&col <= m - 1) //meizu chuli
{
//cout << "hello_2" << endl;
//cout << "row: " << row << " col: " << col << " num: " << num << endl;
b[row][col] = num;
//cout << "OK1" << endl;
num++;
if (isRow == false)
{
//cout << "OK2" << endl;
row++;
col++;
if (row==n)
{
//cout << "OK3" << endl;
row=row-1;
col = col - 2;
break;
}
else if ( col > m - 1&&row!=n)//youshangjiao
{
//cout << "OK3_1" << endl;
col = col - 1;
break;
}
}
if (isRow == true)
{
//cout << "OK4" << endl;
row--;
col--;
if (row < 0 && col >= 0)
{
//cout << "OK5" << endl;
row = 0;
break;
}
else if ( col < 0)
{
//cout << "OK5_1" << endl;
row = row+2;
col = 0;
break;
}
}
}
// cout << "hello_3" << endl;
isRow = !isRow;
}
//cout << "hello_4" << endl;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
*(a[i] + j)=b[i][j];
}
}
return (int**) b;
}
int main() {
int n = 5;
int m = 4;
int ** a = new int*[n];
for(int i=0;i<n;i++)
{
a[i]=new int[m];
}
void print_arrray(int n, int m, int **a);
getGrid(n, m, (int**)a);
print_arrray(n,m,a);
return 0;
}
由于C++中二维数组的指针操作实在是难顶,在函数中传递是真的让人头秃还是用Java方便多了
package 蛇形矩阵;
import java.util.Scanner;
public class array {
public void print(int n,int m, int[][]a) {
int count = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
count++;
System.out.printf("%3d",a[i][j]);
if (count == m )
{
System.out.println();
count = 0;
}
}
}
}
public void creat_arry(int n, int m, int[][]a) {
boolean isodd = false;
int num =1;
int row =0, col = m-1;
for (int i = 0; i < n+m-1; i++) {
while (row>=0&&row <= n - 1 && col>=0&&col <= m - 1) //meizu chuli
{
a[row][col] = num;
num++;
if (isodd == false)
{
row++;
col++;
if (row==n)
{
row=row-1;
col = col - 2;
break;
}
else if ( col > m - 1&&row!=n)//youshangjiao
{
col = col - 1;
break;
}
}
if (isodd == true)
{
row--;
col--;
if (row < 0 && col >= 0)
{
row = 0;
break;
}
else if ( col < 0)
{
row = row+2;
col = 0;
break;
}
}
}
// cout << "hello_3" << endl;
isodd = !isodd;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
array array=new array();
int n, m;
Scanner inputScanner = new Scanner(System.in);
n = inputScanner.nextInt();
m = inputScanner.nextInt();
int a[][] = new int [n][m];
array.creat_arry(n, m, a);
array.print(n, m, a);
}
}