蛇形矩阵算法实现(C++ and java)

蛇形矩阵

非常直接就是像蛇一样的矩阵,不过在算法题里面,这条蛇的扭曲方法不一样,就此诞生了很多各种各样的算法题。

  •  

然后有一个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);
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值