在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 ;
     }
   }
}
相關文章
相關標籤/搜索