在4*4的棋盘上摆满了黑白棋子,黑白两色的位置和数目随机其中左上角坐标为(1,1),右下角坐标为(4,4),现在依次有一些翻转操作,要对一些给定支点坐标为中心的上下左右四个棋子的颜色进行翻转,请计算出翻转后的棋盘颜色。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import java.util.*;
 
public class Main {
 
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
 
    while (in.hasNext()) {
 
      // 棋盘
      String str = in.nextLine().replaceAll("[^0-9]""");
      int[][] board = new int[4][4];
 
      int index = 0;
      for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
          board[i][j] = Integer.parseInt(str.substring(index, index + 1));
          index++;
        }
      }
 
      // 给定点
      str = in.nextLine().replaceAll("[^0-9]""");
      int[][] local = new int[3][2];
 
      index = 0;
      for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 2; j++) {
          local[i][j] = Integer.parseInt(str.substring(index, index + 1)) - 1;
          index++;
        }
      }
 
      // 翻转
      board = doReverse(board, local);
 
      // 输出
      StringBuilder sb = new StringBuilder();
      sb.append("[");
      for (int i = 0; i < 4; i++) {
        sb.append("[");
        for (int j = 0; j < 4; j++) {
          if (j == 3) {
            sb.append(board[i][j]);
          else {
            sb.append(board[i][j] + ",");
          }
        }
        if (i == 3) {
          sb.append("]");
        else {
          sb.append("],");
        }
      }
      sb.append("]");
 
      System.out.println(sb.toString());
    }
 
  }
 
  private static int[][] doReverse(int[][] board, int[][] local) {
 
    for (int[] loc : local) {
 
      int row = loc[0];
      int col = loc[1];
 
      if (row > 0) {
        board[row - 1][col] = reverse(board[row - 1][col]);
      }
 
      if (row < 3) {
        board[row + 1][col] = reverse(board[row + 1][col]);
      }
 
      if (col > 0) {
        board[row][col - 1] = reverse(board[row][col - 1]);
      }
 
      if (col < 3) {
        board[row][col + 1] = reverse(board[row][col + 1]);
      }
 
    }
 
    return board;
  }
 
  private static int reverse(int color) {
    if (color == 1) {
      return 0;
    else {
      return 1;
    }
  }
}

 

posted @ 2020-04-16 16:18  winner66  阅读(1013)  评论(0)    收藏  举报