China MOOC_ object-oriented Programming--java language _ Final Exam programming questions _1 cellular automata

Source: Internet
Author: User

Final Exam Programming Questionsreturn

This is a programming question for the final exam.

Warm tips:

1. This exam belongs to the online Judge topic and is enigmatic grading by the system immediately after submission.

2. Students can submit their answers before the test deadline and the system will take the highest score as the final result.

1 cell automata (30 min)

Topic content:

This is a non-graphical version of the cellular automata. Cellular automata refers to a two-dimensional grid in which each mesh is a cell. Each cell has two states of living and dying.

At the initial moment, some cells are alive, and some cells are dead. Each step of the automata determines whether the cell's next survival depends on the survival of the other cells in the 8 compartments around each cell. The specific rules are as follows:

    • If the cell is now alive and there are 2 or 3 living cells in the surrounding 8 compartments, it will continue to survive and die if the number of living cells in the surrounding 8 lattices is less than 2 or more than 3;

    • If the cell is now dead, and there are just 3 living cells in the surrounding 8 squares, the cells are resurrected.

    • Cells located at the edge and apex of the entire grid may have fewer than 8 cells around it. That is, there are no more cells beyond the boundaries of the grid.

    • The changes in the life and death of each cell will not affect the cells around the current step, but will only show up in the next stage.

Tip: The code in the course differs from the previous sentence.

Input format:

First enter two positive integers, range [3,102], which in turn represent the width and height of the grid.

Then enter multiple sets of positive integers, which in turn represent the grid position of a living cell, each set of numbers, the first representing the line number, and the second representing the column number, starting with 0.

Finally, "1-1" means there is no longer a living cell. -1-1 is not a valid location.

Then, with a positive integer range of [1,10000], the number of steps required to perform the cell automaton is indicated.

Output format:

Outputs a positive integer that indicates the number of cells remaining alive after the execution has completed.

Input Sample:

3 3

1 1 1 2 0 1 2 1

-1-1

1

Sample output:

7

time limit: 500ms memory limit: 32000kb
Import Java.util.scanner;public class Cellmachine {static Scanner in = new Scanner (system.in); static int width = 0;//width st atic int height = 0;//height static int[][] field_old;//old grid static int[][] field_new;//new mesh static int times;//steps Public stat IC void Main (string[] args) {//First enter two positive integers with a range of [3,102], which in turn represents the width and height of the grid. width = in.nextint (); height = In.nextint ();//Initialize array initial ();//Then, with a positive integer, the range is [1,10000], indicating the number of steps required to perform the cell automaton. Times = In.nextint ();//cell Automata run (times); System.out.println (Count (field_old));//outputs a positive integer that indicates the number of cells remaining alive after the execution is complete. }//Initialize array public static void initial () {field_old = new int[height][width];field_new = new Int[height][width];for (int i = 0; i < field_old.length; i++) {for (int j = 0; J < Field_old[i].length; J + +) {Field_old[i][j] = 0;field_new[i][j] = 0;}} Then enter multiple sets of positive integers, which in turn represent the grid position of a living cell, each set of numbers, the first representing the line number, and the second representing the column number, starting with 0. while (true) {int i = in.nextint (); int j = In.nextint (); if (i = =-1 && J = =-1) {break;//Finally, "1-1" means no more living cells. -1-1 is not a valid location. }FIELD_OLD[I][J] = 1;field_new[i][j]= 1;}} The cellular automaton executes public static void run (int times) {int number = 0;for (int i = 0; I < times; i++) {for (int j = 0; J < fie Ld_old.length; J + +) {for (int k = 0; k < field_old[j].length; k++) {number = Getneighbour (j, k);//Calculate surrounding living cells if (field_old[j][k] = = 1 & amp;& (number = = 2 | | number = = 3)) {field_new[j][k] = 1;} else {field_new[j][k] = 0;} if (field_old[j][k] = = 0 && Number = = 3) {Field_new[j][k] = 1;}}} for (int j = 0, J < Field_new.length; J + +) {for (int k = 0; k < field_new[j].length; k++) {Field_old[j][k] = Field_n ew[j][k];//the new copy to the old}}}}//calculates the surrounding alive cells public static int Getneighbour (int i, int j) {int number = 0;if (i = = 0 && j =  = 0) {//upper left corner number = Field_old[i][j + 1] + field_old[i + 1][j + 1] + field_old[i + 1][j];} else if (i = = 0 && J = =  WIDTH-1) {//upper right corner number = Field_old[i][j-1] + field_old[i + 1][j-1] + field_old[i + 1][j];} else if (i = = Height-1 && J = = 0) {//bottom left corner number = Field_old[i-1][j] + field_old[i-1][j + 1] +Field_old[i][j + 1];} else if (i = = Height-1 && j = = width-1) {//lower right corner number = Field_old[i-1][j] + field_old[i-1][j-1] + field_ OLD[I][J-1];}  else if (i = = 0) {//First row other number = Field_old[i][j-1] + field_old[i + 1][j-1] + field_old[i + 1][j] + field_old[i + 1][j + 1]+ Field_old[i][j + 1];} else if (i = = height-1) {//last line other number = Field_old[i][j-1] + field_old[i-1][j-1] + field_old[i-1][j] + field_old [I-1] [j + 1]+ Field_old[i][j + 1];}  else if (j = = 0) {//leftmost other number = Field_old[i-1][j] + field_old[i-1][j + 1] + field_old[i][j + 1] + field_old[i + 1][j + 1]+ Field_old[i + 1][j];} else if (j = = width-1) {//Right other number = Field_old[i-1][j] + field_old[i-1][j-1] + field_old[i][j-1] + field_old[ i + 1][j-1]+ field_old[i + 1][j];} else {//other number = Field_old[i-1][j-1] + field_old[i-1][j] + field_old[i-1][j + 1] + field_old[i][j + 1]+ Field_o Ld[i + 1][j + 1] + field_old[i + 1][j] + field_old[i + 1][j-1] + field_old[i][j-1];} Return NuMber;}  The number of remaining living cells public static int count (int[][] field) {int # = 0;for (int i = 0; i < field.length; i++) {for (int J = 0; J < Field[i].length; J + +) {if (field[i][j] = = 1) {number++;}}} return number;}}

China MOOC_ object-oriented Programming--java language _ Final Exam programming questions _1 cellular automata

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.