首页 > 试题广场 >

棋子翻转

[编程题]棋子翻转
  • 热度指数:3418 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解

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


输入描述:

给定两个数组,分两行

第一行为分别为初始棋盘,为4*4矩阵,其中0表示白色棋子,1表示黑色棋子

第二行为翻转位置,其中翻转位置共有3个



输出描述:
请返回翻转后的棋盘,为4*4矩阵
示例1

输入

[[0,0,1,1],[1,0,1,0],[0,1,1,0],[0,0,1,0]]
[[2,2],[3,3],[4,4]]

输出

[[0,1,1,1],[0,0,1,0],[0,1,1,0],[0,0,1,0]]
最短答案,没有之一。
a = eval(input())
b = eval(input())
 
for x, y in b:
    x -= 1
    y -= 1
    if x > 0:
        a[x-1][y] = 1 - a[x-1][y]
    if x < 3:
        a[x+1][y] = 1 - a[x+1][y]
    if y > 0:
        a[x][y-1] = 1 - a[x][y-1]
    if y < 3:
        a[x][y+1] = 1 - a[x][y+1]
         
print(str(a).replace(" ", ""))

编辑于 2020-03-24 19:50:22 回复(10)

解析:https://blog.csdn.net/m0_43545515/article/details/105011268
1、解题:翻转的含义是将本来是白棋(0)变为黑棋(1),黑棋(1)变为白棋(0);
2、怎么办:
(1)考虑边界条件:在棋盘坐标(2,2)、(2,3)、(3,2)、(3,3)中间区域是有完整的上下左右的,然而四边的位置需要特殊考虑的。
(2)数组的索引也需要小心处理,数组的索引 = 坐标值 - 1

``

#filpColor函数是将黑白棋子的颜色翻转,1变为0,0变为1
def flipChess(color):
    #color代表棋子的颜色
    if color == 1:
        return 0
    else:
        return 1
    #可简化为
    #return 0 if color == 1 else 1
#array是存储需要翻转棋盘数组,position是定支点位置
def doFlip(array, position):
    for i in position:
        #获取数组索引 = 棋盘坐标 - 1 
        row, col = i[0] - 1, i[1] -1
        #如果定支点还有左方棋子(即行索引大于0),则进行翻转
        if row > 0:
            array[row - 1][col] = flipChess(array[row - 1][col])
        #如果定支点还有右方棋子(即行索引小于3),则进行翻转
        if row < 3:
            array[row + 1][col] = flipChess(array[row + 1][col])
        #如果定支点还有上方棋子(即列索引大于0),则进行翻转
        if col > 0:
            array[row][col - 1] = flipChess(array[row][col - 1])
        #如果定支点还有下方棋子(即列索引小于3),则进行翻转
        if col < 3:
            array[row][col + 1] = flipChess(array[row][col + 1])
    return array
    
if __name__ == '__main__':
    array = eval(input())
    position = eval(input())     
    doFlip(array, position)
    print(str(array).replace(' ',''))


编辑于 2020-03-22 14:26:57 回复(2)
这个题虽然不难,但是这个输入给我整得很烦,早知道我就用eval函数了😂
def solve(grid, x, y):
    x -= 1
    y -= 1
    if x > 0:
        if grid[x - 1][y]:
            grid[x - 1][y] -= 1
        else:
            grid[x - 1][y] += 1
    if y > 0:
        if grid[x][y - 1]:
            grid[x][y - 1] -= 1
        else:
            grid[x][y - 1] += 1
    if x < 3:
        if grid[x + 1][y]:
            grid[x + 1][y] -= 1
        else:
            grid[x + 1][y] += 1
    if y < 3:
        if grid[x][y + 1]:
            grid[x][y + 1] -= 1
        else:
            grid[x][y + 1] += 1
    return grid

raw = input()[1:-1]
temp = [raw[:9][1:-1], raw[10:19][1:-1], raw[20:29][1:-1], raw[30:39][1:-1]]
grid = [list(map(int, line.split(','))) for line in temp]
raw = input()[1:-1]
temp = [raw[:5][1:-1], raw[6:11][1:-1], raw[12:17][1:-1]]
cordinates = [list(map(int, line.split(','))) for line in temp]
for op in cordinates:
    grid = solve(grid, op[0], op[1])
print(''.join([c for c in str(grid) if c != ' ']))



发表于 2020-12-23 15:24:31 回复(0)
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;
    }
  }
}

发表于 2020-04-01 14:09:05 回复(0)
这个题使用python的numpy模块,竟然会报“ModuleNotFoundError: No module named 'numpy' ”的错误。
发表于 2023-07-21 09:17:39 回复(0)
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner1 = new Scanner(System.in);
        char[] char1 = scanner1.nextLine().toCharArray();
        Scanner scanner2 = new Scanner(System.in);
        char[] char2 = scanner2.nextLine().toCharArray();
        int[][] input1 = new int[4][4];
        int[][] input2 = new int[3][2];
        ArrayList<Integer> list1 = new ArrayList<>();
        for (char c : char1) {
            if (Character.isDigit(c)) {
                list1.add(Integer.parseInt(String.valueOf(c)));
            }
        }
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                input1[i][j] = list1.get(4 * i + j);
            }
        }
        ArrayList<Integer> list2 = new ArrayList<>();
        for (char c : char2) {
            if (Character.isDigit(c)) {
                list2.add(Integer.parseInt(String.valueOf(c)));
            }
        }
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 2; j++) {
                input2[i][j] = list2.get(2 * i + j);
            }
        }
        for (int i = 0; i < 3; i++) {
            input1 = ReverseAtOnePoint(input1, input2[i]);
        }
        System.out.println(Arrays.deepToString(input1));
    }

    //    在指定一个点处翻转
    public static int[][] ReverseAtOnePoint(int[][] board, int[] location) {
        int row = location[0] - 1;
        int col = location[1] - 1;
        if (row - 1 >= 0 & col >= 0) {
            board[row - 1][col] = Reverse(board[row - 1][col]);
        }
        if (row + 1 <= 3 & col >= 0) {
            board[row + 1][col] = Reverse(board[row + 1][col]);
        }
        if (row >= 0 & col - 1 >= 0) {
            board[row][col - 1] = Reverse(board[row][col - 1]);
        }
        if (row >= 0 & col + 1 <= 3) {
            board[row][col + 1] = Reverse(board[row][col + 1]);
        }
        return board;
    }

    public static int Reverse(int color) {
        if (color == 1) {
            return 0;
        } else {
            return 1;
        }
    }
}
发表于 2023-05-14 22:09:22 回复(1)
# 主要是输出格式比较烦,我代码写的还是不够简洁,小白见谅
import sys

lst = []
for line in sys.stdin:
    a = line.split()
    lst.append(eval(a[0]))
juzhen = lst[0]
for i in lst[1]:
    if (i[0] - 2)>=0:
        juzhen[i[0] - 2][i[1] - 1] = (juzhen[i[0] - 2][i[1] - 1] + 1) % 2
    if (i[0])<=3:
        juzhen[i[0]][i[1] - 1] = (juzhen[i[0]][i[1] - 1] + 1) % 2
    if (i[1] - 2)>=0:
        juzhen[i[0] - 1][i[1] - 2] = (juzhen[i[0] - 1][i[1] - 2] + 1) % 2
    if (i[1])<=3:
        juzhen[i[0] - 1][i[1]] = (juzhen[i[0] - 1][i[1]] + 1) % 2
st = ''
for i in str(juzhen):
    if ' ' not in i:
        st+=i
print(st)
发表于 2023-03-28 15:47:32 回复(0)
###先计算上下左右点的索引位置,再判断该点是否在矩阵,从而进行翻转
array = eval(input()) 
index = eval(input())
for i in index: 
    x , y = i[0]-1,i[1]-1 ##将支点坐标转换为索引下标(列表索引位置从0开始,所以要减一)
    trans_index = [[x-1,y],[x+1,y],[x,y-1],[x,y+1]]  ##生成上下左右4点的索引列表
    for j in trans_index:  ##对4点索引的列表进行循环
        if j[0] in range(4) and j[1] in range(4): ##判断该点是否在矩阵之中
            array[j[0]][j[1]] = 1-array[j[0]][j[1]] ##如果在,就进行翻转
array = str(array).replace(' ','') ##输出要求是字符串,且标点前后没有空格,先转换为字符串后消除所有空格
print(array)

发表于 2023-02-03 23:48:16 回复(0)
#小弟初学python
a = eval(input())
b = eval(input())#[[2,3]]
for i in b: #i=[2,3]
    i[0]=i[0]-1 #i[0]=1
    i[1]=i[1]-1 #i[1]=2
    if i[0]==0:
        if i[1]==0:
            a[i[0]][i[1]+1]=1-a[i[0]][i[1]+1]
            a[i[0]+1][i[1]]=1-a[i[0]+1][i[1]]
        elif i[1]==3:
            a[i[0]][i[1]-1]=1-a[i[0]][i[1]-1]
            a[i[0]+1][i[1]]=1-a[i[0]+1][i[1]]
        else:
            a[i[0]][i[1]-1]=1-a[i[0]][i[1]-1]
            a[i[0]][i[1]+1]=1-a[i[0]][i[1]+1]
            a[i[0]+1][i[1]]=1-a[i[0]+1][i[1]]
    elif i[0]==3:
        if i[1]==0:
            a[i[0]][i[1]+1]=1-a[i[0]][i[1]+1]
            a[i[0]-1][i[1]]=1-a[i[0]-1][i[1]]
        elif i[1]==3:
            a[i[0]][i[1]-1]=1-a[i[0]][i[1]-1]
            a[i[0]-1][i[1]]=1-a[i[0]-1][i[1]]
        else:
            a[i[0]][i[1]-1]=1-a[i[0]][i[1]-1]
            a[i[0]][i[1]+1]=1-a[i[0]][i[1]+1]
            a[i[0]-1][i[1]]=1-a[i[0]-1][i[1]]
    elif i[0]==1&nbs***bsp;i[0]==2:
        if i[1]==0:
            a[i[0]-1][i[1]]=1-a[i[0]-1][i[1]]
            a[i[0]+1][i[1]]=1-a[i[0]+1][i[1]]
            a[i[0]][i[1]+1]=1-a[i[0]][i[1]+1]
        elif i[1]==3:
            a[i[0]-1][i[1]]=1-a[i[0]-1][i[1]]
            a[i[0]+1][i[1]]=1-a[i[0]+1][i[1]]
            a[i[0]][i[1]-1]=1-a[i[0]][i[1]-1]
        else:
            a[i[0]][i[1]-1]=1-a[i[0]][i[1]-1]
            a[i[0]][i[1]+1]=1-a[i[0]][i[1]+1]
            a[i[0]-1][i[1]]=1-a[i[0]-1][i[1]]
            a[i[0]+1][i[1]]=1-a[i[0]+1][i[1]]
print(str(a).replace(" ", ""))

发表于 2021-11-08 09:45:03 回复(0)
def turnColor(boards___,points___):
    for ele in
def turnColor(boards___,points___):
    for ele in points___:
        position___=getPosition(ele[0],ele[1])
        for positions in position___:
            if boards___[positions[0]][positions[1]]==1:
                boards___[positions[0]][positions[1]]=0
            else:
                boards___[positions[0]][positions[1]]=1
             
    return boards___
             
         
def getPosition(x,y):
    x=x-1
    y=y-1
    positions__=[]
    index___=[]
    positions__.append([x,y-1])
    positions__.append([x,y+1])
    positions__.append([x-1,y])
    positions__.append([x+1,y])
    for index in range(len(positions__)-1,-1,-1):
        if (positions__[index][0]<0)or(positions__[index][1]<0)or(positions__[index][0]>=4)or(positions__[index][1]>=4):
             positions__.pop(index)
     
 
     
    return positions__
 
if __name__ == "__main__":
    l1=[[0,0,1,1],[1,0,1,0],[0,1,1,0],[0,0,1,0]]
    l2=[[2,2],[3,3],[4,4]]
    result___=turnColor(l1,l2)
对是对了看了各位大哥的答案感觉真是路漫漫其修远兮啊
发表于 2021-08-16 16:29:28 回复(0)
a = eval(input())#矩阵
b = eval(input()) #位置
for i,j in b:
    i-=1
    j-=1
    if i<3:
        a[i+1][j]=1 if a[i+1][j]==0 else 0
    if i>0:
        a[i-1][j]=1 if a[i-1][j]==0 else 0
    if j<3:
         a[i][j+1]=1 if a[i][j+1]==0 else 0
    if j>0:
        a[i][j-1]=1 if a[i][j-1]==0 else 0
print(str(a).replace(" ",""))
发表于 2021-04-04 21:43:56 回复(0)
grid = eval(input())
node = eval(input())
directions = [[1,0],[-1,0],[0,1],[0,-1]]
for x,y in node:
    for idx,idy in directions:
        new_x, new_y = x - 1 + idx, y - 1 + idy 
        if 0 <= new_x < 4 and 0 <= new_y < 4:
            grid[new_x][new_y] = 1 - grid[new_x][new_y]
print(str(grid).replace(' ', ''))

发表于 2021-03-12 14:34:48 回复(0)

import numpy as np
a1=np.array([[0,0,1,1],[1,0,1,0],[0,1,1,0],[0,0,1,0]])
a=a1*0
b=np.array([[2,2],[3,3],[4,4]])
for i in np.arange(3):
    x=b[i]
    if x[0]==1:
        if x[1]==1:
            a[x[0]-1][x[1]]=a[x[0]-1][x[1]]+1
            a[x[0]][x[1]-1]=a[x[0]-1][x[1]]+1
        elif x[1]==4:
            a[x[0]-1][x[1]-2]=a[x[0]-1][x[1]-2]+1
            a[x[0]][x[1]-1]=a[x[0]][x[1]-1]+1
        else:
            a[x[0]-1][x[1]-2]=a[x[0]-1][x[1]-2]+1
            a[x[0]-1][x[1]]=a[x[0]-1][x[1]]+1
            a[x[0]][x[1]-1]=a[x[0]][x[1]-1]+1
    if x[0]==4:
        if x[1]==1:
            a[x[0]-1][x[1]]=a[x[0]-1][x[1]]+1
            a[x[0]-2][x[1]-1]=a[x[0]-2][x[1]-1]+1
        elif x[1]==4:
            a[x[0]-1][x[1]-2]=a[x[0]-1][x[1]-2]+1
            a[x[0]-2][x[1]-1]=a[x[0]-2][x[1]-1]+1
        else:
            a[x[0]-1][x[1]-2]=a[x[0]-1][x[1]-2]+1
            a[x[0]-1][x[1]]=a[x[0]-1][x[1]]+1
            a[x[0]-2][x[1]-1]=a[x[0]-2][x[1]-1]+1
    else:
        if x[1]==1:
            a[x[0]-1][x[1]]=a[x[0]-1][x[1]]+1
            a[x[0]-2][x[1]-1]=a[x[0]-2][x[1]-1]+1
            a[x[0]][x[1]-1]=a[x[0]-1][x[1]]+1
        elif x[1]==4:
            a[x[0]-1][x[1]-2]=a[x[0]-1][x[1]-2]+1
            a[x[0]-2][x[1]-1]=a[x[0]-2][x[1]-1]+1
            a[x[0]][x[1]-1]=a[x[0]-1][x[1]]+1
        else:
            a[x[0]-1][x[1]]=a[x[0]-1][x[1]]+1
            a[x[0]][x[1]-1]=a[x[0]][x[1]-1]+1
            a[x[0]-1][x[1]-2]=a[x[0]-1][x[1]-2]+1
            a[x[0]-2][x[1]-1]=a[x[0]-2][x[1]-1]+1
a=a%2
new=a1+a
new=new%2
print(new)

发表于 2020-11-11 20:41:34 回复(1)
C语言,害,输入输出就,,,,搞了很久
#include<stdio.h>
#include<string.h>
#define N 4
#define MAX 16
 
int main(){
    int chess[N][N];
    int axis[MAX][2];
    char str1[80];
    char str2[100];
    gets(str1);
    gets(str2);
    int x=0,y=0;
    for(int i=0;i<strlen(str1);i++)
    {
        if((str1[i]-0 == 48) || (str1[i]-0 == 49))
        {
            chess[x][y] = str1[i]-'0';
            y += 1;
            if(y>N-1) {y=0;x++;}
            if(x>N-1) break;
        }
    }
    x=0;
    y=0;
    for(int i=0;i<strlen(str2);i++)
    {
        if((str2[i]-0 > 48) && (str2[i]-0 < 53))
        {
            axis[x][y++] = str2[i]-'0';
            if(y>1) {y=0;x++;}
            if(x>=MAX) break;
        }
    }
    int length = x;
    int x_label, y_label;
    for(x=0;x<length;x++)
    {
        x_label = axis[x][0]-1;
        y_label = axis[x][1]-1;
        if(x_label-1 >= 0) chess[x_label-1][y_label] =(chess[x_label-1][y_label]==0)?1:0;
        if(y_label-1 >= 0) chess[x_label][y_label-1] =(chess[x_label][y_label-1]==0)?1:0;
        if(x_label+1 < 4) chess[x_label+1][y_label] =(chess[x_label+1][y_label]==0)?1:0;
        if(y_label+1 < 4) chess[x_label][y_label+1] =(chess[x_label][y_label+1]==0)?1:0;
    }
    int change = 0;
    for(x=0;x<4;x++)
    {
        change += 2;
        for(y=0;y<4;y++)
        {
            str1[change] = chess[x][y]==0?'0':'1';
            change += 2;
        }
    }
    puts(str1);
    return 0;
}

发表于 2020-09-19 17:05:20 回复(0)
s = eval(input().strip(' '))
queue =eval(input().strip(' '))
for cur in queue:
    x = cur[0] - 1
    y = cur[1] - 1
    for i,j in [[1,0],[-1,0],[0,1],[0,-1]]:
        if 0 <=x+i < 4 and 0<= y+j < 4:
            if s[i+x][y+j] == 0:
                s[i+x][y+j] = 1
            else:
                s[i+x][y+j] = 0
print(''.join(str(s).split(' ')))

发表于 2020-08-21 21:27:06 回复(0)

C++

#include <vector>
#include <iostream>
using namespace std;


vector<vector<int> > solution(vector<vector<int> > &matrix, vector<vector<int> > &change){
    int len = 4;
    for(int i = 0; i < change.size(); i++){
        int a = change[i][0]-1;
        int b = change[i][1]-1;
        if(a - 1 >= 0){
            if(matrix[a-1][b] == 0){
                matrix[a-1][b] = 1;
            }else matrix[a-1][b] = 0;
        }
        if(a+1 < len){
            if(matrix[a+1][b] == 0){
                matrix[a+1][b] = 1;
            }else matrix[a+1][b] = 0;
        }
        if(b - 1 >= 0){
            if(matrix[a][b-1] == 0){
                matrix[a][b-1]  = 1;
            }else matrix[a][b-1]  = 0;
        }
        if(b+1 < len){
            if(matrix[a][b+1] == 0){
                matrix[a][b+1] = 1;
            }else matrix[a][b+1] = 0;
        }
    }
    return matrix;
}

int main(void){
    string input, input2;
    cin >> input;
    cin.clear();
    cin >> input2;
    vector<vector<int> > matrix;
    vector<int> temp;
    for(int i = 0; i < input.size(); i++){
        if(input[i] == '[' || input[i] == ']' || input[i] == ',') continue;
        if(temp.size() == 4){
            matrix.push_back(temp);
            temp.clear();    
        }
        temp.push_back(input[i]- '0');
    }
    matrix.push_back(temp);


    vector<vector<int> > change;
    vector<int> temp1;
    for(int i = 0; i < input2.size(); i++){
        if(input2[i] == '[' || input2[i] == ']' || input2[i] == ',') continue;
        if(temp1.size() == 2){
            change.push_back(temp1);
            temp1.clear();    
        }
        temp1.push_back(input2[i]- '0');
    }
    change.push_back(temp1);

    vector<vector<int> > res = solution(matrix, change);
    string out = "[";
    for(int i = 0; i < res.size(); i++){
        string temp3 = "[";
        for(int j = 0; j < res[i].size(); j++){
            if(j != res[i].size()-1)temp3 += to_string(res[i][j]) + ",";
            else temp3 += to_string(res[i][j]) + "]";    
        }
        if(i != res.size()-1) out += temp3 + ",";
        else out += temp3 + "]";
    }
    cout << out << endl;


    return 0;
} 
编辑于 2020-05-23 15:49:18 回复(0)
array=eval(input())
position=eval(input())
for zuobiao in position:
    if zuobiao[0]>1:
        temp1=array[zuobiao[0]-2][zuobiao[1]-1]
        array[zuobiao[0]-2][zuobiao[1]-1]=1-temp1
    if zuobiao[0]<4:
        temp2=array[zuobiao[0]][zuobiao[1]-1]
        array[zuobiao[0]][zuobiao[1]-1]=1-temp2
    if zuobiao[1]>1:
        temp3=array[zuobiao[0]-1][zuobiao[1]-2]
        array[zuobiao[0]-1][zuobiao[1]-2]=1-temp3
    if zuobiao[1]<4:
        temp4=array[zuobiao[0]-1][zuobiao[1]]
        array[zuobiao[0]-1][zuobiao[1]]=1-temp4
print(str(array).replace(" ",''))

发表于 2020-05-09 00:09:45 回复(0)
n=input()
n2=input()
for i in range(len(n2)):
    a=n2[i][0]-1
    b=n2[i][1]-1
    if a-1>=0:
        if n[a-1][b]==0:
            n[a-1][b]=1
        else:
            n[a-1][b]=0
    if a+1<=3:
        if n[a+1][b]==0:
            n[a+1][b]=1
        else:
            n[a+1][b]=0
    if b-1>=0:
        if n[a][b-1]==0:
            n[a][b-1]=1
        else:
            n[a][b-1]=0
    if b+1<=3:
        if n[a][b+1]==0:
            n[a][b+1]=1
        else:
            n[a][b+1]=0
print str(n).replace(" ","")
发表于 2020-04-23 12:12:07 回复(0)
def turn(matrix,i,j):
    if i-1>=0:
        matrix[i-1][j] = int(not matrix[i-1][j])
    if i+1<=3:
        matrix[i+1][j] = int(not matrix[i+1][j])
    if j-1>=0:
        matrix[i][j-1] = int(not matrix[i][j-1])
    if j+1<=3:
        matrix[i][j+1] = int(not matrix[i][j+1])
    return matrix
            
if __name__ == '__main__':
    matrix = eval(input())
    position = eval(input())
    for each in position:
        i = each[0]-1
        j = each[1]-1
        matrix = turn(matrix,i,j)
    print(str(matrix).replace(' ','')
最后输出的str格式真是迷了我一阵。。。
发表于 2020-03-19 17:26:56 回复(3)
import java.util.Scanner;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		char[] str = sc.nextLine().toCharArray();
		String s_str = strToString(str);
		char[] str_chess = s_str.toCharArray();
		char[][] chess = new char [4][4];
		for(int i =0;i<4;++i){
			for(int j =0;j<4;++j){
				chess[i][j] = str_chess[i*4+j];
			}
		}
		
		char[] point = sc.nextLine().toCharArray();
		String s_point = strToString(point);
		char[] str_point = s_point.toCharArray();
		for(int i=0;i<str_point.length/2;++i){
			flip_chess(chess,charToint(str_point[i*2])-1,charToint(str_point[i*2+1])-1);
		}
		System.out.print('[');
		for(int i=0;i<4;++i){
			System.out.print('[');
			for(int j=0;j<4;++j){
				if(j!=3)	System.out.print(charToint(chess[i][j])+",");
				else System.out.print(charToint(chess[i][j])+"]");
			}
			if(i!=3)	System.out.print(',');
		}
		System.out.print(']');
		
	}
	
	public static int charToint(char a){
		return a-'0';
	}
	public static String strToString(char[] str){
		String a = "";
		for(int i =0 ; i< str.length; ++i){
			if(str[i]>='0' && str[i]<= '9'){
				a+=str[i];
			}
		}
		return a;
	}
	
	public static void flip(char[][] array, int row, int col){
		if(array[row][col] == '0'){
			array[row][col] = '1';
		}
		else array[row][col] = '0';
	}
	
	public static void flip_chess(char[][] array,int a,int b){
		if(a-1>=0)	flip(array,a-1,b);//up
		if(a+1<=3)	flip(array,a+1,b);//down
		if(b-1>=0)	flip(array,a,b-1);//left
		if(b+1<=3)	flip(array,a,b+1);//right
	}
}

发表于 2020-03-18 13:43:01 回复(0)