Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class C{
- public static void main(String args[]){
- Scanner in = new Scanner(System.in);
- int cases = in.nextInt();
- int di []= {-1,-1,0,1,1,1,0,-1};
- int dj []= {0,1,1,1,0,-1,-1,-1};
- in.nextLine();
- int d = cases;
- while(cases > 0){
- int city [][] = new int [22][22];
- int count = in.nextInt();
- int l = 1;
- for (int i = 0; i < 22 ; i++ ) {
- for (int j = 0; j < 22 ; j++) {
- if(!((i > 0 && i < 21) && (j > 0 && j < 21))){
- city[i][j] = -1;
- }
- }
- }
- in.nextLine();
- String locate ;
- do{
- locate = in.nextLine();
- if(!locate.equals("")){
- String position [] = locate.split(" ");
- int x = Integer.parseInt(position[0]);
- int y = Integer.parseInt(position[1]);
- city[x][y] = 1;
- }
- }while(!locate.equals(""));
- if(d > cases ){
- System.out.print("\n");
- }
- if (l == 1) {
- System.out.println("********************");
- print(city);
- System.out.println("********************");
- l++;
- }
- int auxCity [][] = city ;
- while(count > 1) {
- auxCity = calculator(auxCity,di,dj);
- print(auxCity);
- System.out.println("********************");
- count--;
- }
- cases--;
- };
- }
- static int[][] calculator(int [][] city, int di[], int dj[]){
- int [][] auxCity = new int[22][22];
- for(int i = 1; i <= 20; i++){
- for (int j = 1; j <= 20 ; j++ ) {
- int neighbors = 0;
- for (int k = 0; k < 8 ; k++ ) {
- int x = i + di[k];
- int y = j + dj[k];
- if(city[x][y] == 1){
- neighbors ++;
- }
- }
- if(neighbors == 3){
- auxCity[i][j] = 1;
- }else if(neighbors == 2){
- if(city[i][j] == 0){
- auxCity[i][j] = 0;
- }else{
- auxCity[i][j] = 1;
- }
- }else{
- auxCity[i][j] = 0;
- }
- }
- }
- return auxCity;
- }
- static void print(int [][] city){
- for (int i = 1; i <= 20; i++) {
- for (int j = 1; j <= 20 ; j++) {
- if(city[i][j] == 0){
- System.out.print(" ");
- }else{
- System.out.print("O");
- }
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement