1. 程式人生 > >ACM 陣列蛇型走位

ACM 陣列蛇型走位

 

 

 

 

 

import java.util.Scanner;

public class One {

    public static void main(String[] args) {


        Scanner scanner = new Scanner(System.in);
        int i = scanner.nextInt();

        int[][] arr = new int[i + 2][i + 2];

        int sum = 0;
        for (int out = 1; out < i + 1; out++) {
            for (int inter = 1; inter < i + 1; inter++) {
                arr[out][inter] = ++sum;
            }
        }

        //計數
        int add = 0;
        //座標
        int a = 1, b = 1;

        while (add != i * i) {

            System.out.println(a + "," + b);

            arr[a][b] = 0;
            add++;

            if (arr[a][b + 1] != 0) {//做右
                b++;
            } else if (arr[a + 1][b] != 0) {//下
                a++;
            } else if (arr[a][b - 1] != 0) {//做
                b--;
            } else {
                a--;//上
            }



        }
    }
}