[南陽OJ-No.33]蛇形填數|在n*n方陳裏填入1,2,...,n*n,要求填成蛇形。

南陽OJ-No.33

時間限制3000ms,空間限制65535KB,難度3

描述

在n*n方陳裏填入1,2,…,n*n,要求填成蛇形。例如n=4時方陣爲:

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

輸入

直接輸入方陳的維數,即n的值。(n<=100)

輸出

輸出結果是蛇形方陳。

樣例輸入

3

樣例輸出

7 8 1
6 9 2
5 4 3


思路:4 連通問題
n=1,特殊情況
n>2,
我先畫出了n=2,3,4,5,6的蛇形數組進行觀察,數組構建過程是從右上角的數字1開始,然後按照順時針方向依次構建。方陣中每個格子相鄰的都有四個格子。觀察構建順序,數字存放順序爲順時針方向,即下、左、上、右,進行循環判斷填充,注意判斷數組下標不越界


JAVA

時間83,內存1475

import java.util.Scanner;

public class Main {
    public static Scanner cin = new Scanner(System.in);
    public static int N = cin.nextInt(), x = 0, y = N-1, count = 1;
    public static int[][] num = new int[N][N];

    static {
        for (int x=0; x<N; x++) {
            for (int y=0; y<N; y++) {
                num[x][y] = 0;
            } 
        }

        num[x][y] = 1;
    }

    public static void print(int[][] num) {
        for (int x=0; x<N; x++) {
            for (int y=0; y<N; y++) {
                System.out.print(num[x][y] + " ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) throws Exception {   
        while(count < N*N) {
            while(x<N-1 && num[x+1][y]==0)
                num[++x][y] = ++count;

            while(y>0 && num[x][y-1]==0)
                num[x][--y] = ++count;

            while(x>0 && num[x-1][y]==0)
                num[--x][y] = ++count;

            while(y<N-1 && num[x][y+1]==0)
                num[x][++y] = ++count;
        }
        print(num);
    }
}

這個是在南陽OJ中扒出來的以爲大神,不懂啊(⊙﹏⊙)
時間36,內存676

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        int i,j,h = 0;
        short num = 1;
        Scanner scanner = new Scanner(System.in);
        short n = scanner.nextShort();
        short[][] array = new short[n][n];
        int hmax = (n+1)>>1;
        for ( h = 0; h <= hmax; h++) {
            if (h==hmax&&n%2!=0) {
                array[n>>1][n>>1] = num;
            }else {
                for ( i =h; i < n-1-h; i++) {
                    array[i][n-1-h]=num++;
                }
                for ( i = n-1-h; i >h ; i--) {
                    array[n-1-h][i]=num++;
                }
                for ( i = n-1-h; i >h ; i--) {
                    array[i][h]=num++;
                }
                for ( i = h; i < n-1-h; i++) {
                    array[h][i] = num++;
                }
            }
        }
        StringBuffer sb = new StringBuffer();
        for ( i = 0; i < array.length; i++) {
            for ( j = 0; j < array[i].length; j++) {
                sb.append(array[i][j]).append(" ");
            }
            sb.append("\n");
        }
        System.out.print(sb);
    }
}        

C++

時間4,內存240

#include <iostream>
using namespace std;

int main()
{
  int N, x, y, count;

  cin >> N;
  x = 0;
  y = N-1;
  count = 1;

  int num[N][N];

  for (int x=0; x<N; x++) {
        for (int y=0; y<N; y++) {
            num[x][y] = 0;
        } 
  }

  num[x][y] = 1;

  while(count < N*N) {
        while(x<N-1 && num[x+1][y]==0)
            num[++x][y] = ++count;

        while(y>0 && num[x][y-1]==0)
            num[x][--y] = ++count;

        while(x>0 && num[x-1][y]==0)
            num[--x][y] = ++count;

        while(y<N-1 && num[x][y+1]==0)
            num[x][++y] = ++count;
  }

  for (int x=0; x<N; x++) {
        for (int y=0; y<N; y++) {
            cout << num[x][y] << " ";
        }
        cout << endl;
  }

  return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章