Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class C{
  4. public static void main(String args[]){
  5.  
  6. Scanner in = new Scanner(System.in);
  7.  
  8. int cases = in.nextInt();
  9. int di []= {-1,-1,0,1,1,1,0,-1};
  10. int dj []= {0,1,1,1,0,-1,-1,-1};
  11. in.nextLine();
  12. int d = cases;
  13.  
  14. while(cases > 0){
  15.  
  16. int city [][] = new int [22][22];
  17. int count = in.nextInt();
  18. int l = 1;
  19.  
  20. for (int i = 0; i < 22 ; i++ ) {
  21. for (int j = 0; j < 22 ; j++) {
  22. if(!((i > 0 && i < 21) && (j > 0 && j < 21))){
  23. city[i][j] = -1;
  24. }
  25. }
  26. }
  27.  
  28. in.nextLine();
  29. String locate ;
  30.  
  31. do{
  32. locate = in.nextLine();
  33. if(!locate.equals("")){
  34.  
  35. String position [] = locate.split(" ");
  36. int x = Integer.parseInt(position[0]);
  37. int y = Integer.parseInt(position[1]);
  38. city[x][y] = 1;
  39. }
  40. }while(!locate.equals(""));
  41.  
  42. if(d > cases ){
  43. System.out.print("\n");
  44. }
  45.  
  46. if (l == 1) {
  47. System.out.println("********************");
  48. print(city);
  49. System.out.println("********************");
  50. l++;
  51. }
  52.  
  53. int auxCity [][] = city ;
  54. while(count > 1) {
  55. auxCity = calculator(auxCity,di,dj);
  56. print(auxCity);
  57. System.out.println("********************");
  58. count--;
  59. }
  60. cases--;
  61. };
  62. }
  63.  
  64. static int[][] calculator(int [][] city, int di[], int dj[]){
  65.  
  66. int [][] auxCity = new int[22][22];
  67.  
  68. for(int i = 1; i <= 20; i++){
  69. for (int j = 1; j <= 20 ; j++ ) {
  70. int neighbors = 0;
  71. for (int k = 0; k < 8 ; k++ ) {
  72. int x = i + di[k];
  73. int y = j + dj[k];
  74.  
  75. if(city[x][y] == 1){
  76. neighbors ++;
  77. }
  78. }
  79.  
  80. if(neighbors == 3){
  81. auxCity[i][j] = 1;
  82. }else if(neighbors == 2){
  83. if(city[i][j] == 0){
  84. auxCity[i][j] = 0;
  85. }else{
  86. auxCity[i][j] = 1;
  87. }
  88. }else{
  89. auxCity[i][j] = 0;
  90. }
  91. }
  92. }
  93.  
  94. return auxCity;
  95. }
  96.  
  97. static void print(int [][] city){
  98.  
  99. for (int i = 1; i <= 20; i++) {
  100. for (int j = 1; j <= 20 ; j++) {
  101. if(city[i][j] == 0){
  102. System.out.print(" ");
  103. }else{
  104. System.out.print("O");
  105. }
  106.  
  107. }
  108. System.out.println();
  109. }
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement