Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2014-05-08代码游戏 #33

Open
leonar opened this issue May 8, 2014 · 3 comments
Open

2014-05-08代码游戏 #33

leonar opened this issue May 8, 2014 · 3 comments

Comments

@leonar
Copy link

leonar commented May 8, 2014

游戏的主题叫"落沙",顾名思义就是沙子落下。分别存在两种物体,沙子(@)和障碍物(#),由于重力影响沙子会从高处下落,但是如果遇到障碍物,则会在障碍物上堆积。
要求:
在给定空间内,设定空间的初始状态(设定N*N大小):
snap123

输出空间最后结果状态:

snap124

语言不限

@leonar
Copy link
Author

leonar commented May 15, 2014

            // input parsing
    Scanner sc = new Scanner(System.in);
    System.out.println("input size: ");
    int size = Integer.parseInt(sc.nextLine());
    System.out.println("input initial map: ");
    char[][] input = new char[size][size];
    for (int i = 0; i < size; ++i) {
        String line = sc.nextLine();
        for (int j = 0; j < size; ++j) {
            input[i][j] = line.charAt(j);
        }
    }

    for (int row = input.length - 1; row >= 0; --row) {
        for (int col = input[0].length - 1; col >= 0; --col) {
            if (input[row][col] == '@') {
                int newPos = fall(row, col, input);
                input[row][col] = ' ';
                input[newPos][col] = '@';
            }
        }
    }
    System.out.println("the finally map: ");
    printBoard(input);
}

private static int fall(int row, int col, char[][] input) {
    while (true) {
        if (row + 1 >= input.length || input[row + 1][col] == '@'
                || input[row + 1][col] == '#') {
            return row;
        }
        row++;
    }
}

// Prints board.
private static void printBoard(char[][] board) {
    for (int row = 0; row < board.length; ++row) {
        for (int col = 0; col < board.length; ++col) {
            System.out.print(board[row][col]);
        }
        if (row < board.length - 1) {
            System.out.print("\n");
        }
    }

@jcm872000
Copy link

public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[][] s = { { 1, 1, 1, 1, 1, 1, 1 }, { 2, 0, 0, 0, 0, 0, 0 }, { 0, 2, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 2, 0 }, { 0, 0, 0, 2, 0, 0, 0 }, { 2, 0, 0, 0, 0, 0, 2 } };
        System.out.println("before:");
        for (int i = 0; i < 7; i++) {
            for (int j = 0; j < 7; j++) {
                System.out.print(s[i][j] + " ");
            }
            System.out.println();
        }
        for (int i = 0; i < 7; i++) {
            int j = 1;
            for (; j < 7; j++) {
                if (s[j][i] == 2) {
                    break;
                }
            }
            s[0][i] = 0;
            s[j - 1][i] = 1;
        }
        System.out.println("after:");
        for (int i = 0; i < 7; i++) {
            for (int j = 0; j < 7; j++) {
                System.out.print(s[i][j] + " ");
            }
            System.out.println();
        }
    }

@jcm872000
Copy link

before:
1 1 1 1 1 1 1
2 0 0 0 0 0 0
0 2 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 2 0
0 0 0 2 0 0 0
2 0 0 0 0 0 2
after:
1 0 0 0 0 0 0
2 1 0 0 0 0 0
0 2 0 0 0 0 0
0 0 0 0 0 1 0
0 0 0 1 0 2 0
0 0 0 2 0 0 1
2 0 1 0 1 0 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants