1. 程式人生 > >程式設計題12

程式設計題12

在4x4的棋盤上擺滿了黑白棋子,黑白兩色的位置和數目隨機其中左上角座標為(1,1),右下角座標為(4,4),現在依次有一些翻轉操作,要對一些給定支點座標為中心的上下左右四個棋子的顏色進行翻轉,請計算出翻轉後的棋盤顏色。

給定兩個陣列A和f,分別為初始棋盤和翻轉位置。其中翻轉位置共有3個。請返回翻轉後的棋盤。

測試樣例:
[[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]]
https://www.nowcoder.com/practice/0b5ab6cc51804dd59f9988ad70d8c4a0?tpId=49&&tqId=29282&rp=1&ru=/activity/oj&qru=/ta/2016test/question-ranking

mport java.util.*;
public class Flip {
    public int[][] flipChess(int[][] A, int[][] f) {
		for(int i=0;i<f.length;i++){
	    	   for(int j=0;j<f[i].length;j++){
	    		   f[i][j]=f[i][j]-1;
	    	     }
	    	   }
	   
       for(int i=0;i<f.length;i++){
    	   for(int j=0;j<=0;j++){
    		   if
(f[i][j]==0&&f[i][j+1]==0){ A[f[i][j]+1][f[i][j+1]]=changeValue(A[f[i][j]+1][f[i][j+1]]);//下 A[f[i][j]][f[i][j+1]+1]=changeValue(A[f[i][j]][f[i][j+1]+1]);//右 }else if(f[i][j]==0&&f[i][j+1]==A[0].length-1){ A[f[i][j]+1][f[i][j+1]]=changeValue(A[f[i][j]+1
][f[i][j+1]]);//下 A[f[i][j]][f[i][j+1]-1]=changeValue(A[f[i][j]][f[i][j+1]-1]);//左 }else if(f[i][j]==0){ A[f[i][j]][f[i][j+1]-1]=changeValue(A[f[i][j]][f[i][j+1]-1]);//左 A[f[i][j]][f[i][j+1]+1]=changeValue(A[f[i][j]][f[i][j+1]+1]);//右 A[f[i][j]+1][f[i][j+1]]=changeValue(A[f[i][j]+1][f[i][j+1]]);//下 }else if(f[i][j]==A.length-1&&f[i][j+1]==0){ A[f[i][j]-1][f[i][j+1]]=changeValue(A[f[i][j]-1][f[i][j+1]]);//上 A[f[i][j]][f[i][j+1]+1]=changeValue(A[f[i][j]][f[i][j+1]+1]);//右 }else if(f[i][j]==A.length-1&&f[i][j+1]==A[A.length-1].length-1){ A[f[i][j]-1][f[i][j+1]]=changeValue(A[f[i][j]-1][f[i][j+1]]);//上 A[f[i][j]][f[i][j+1]-1]=changeValue(A[f[i][j]][f[i][j+1]-1]);//左 }else if(f[i][j]==A.length-1){ A[f[i][j]-1][f[i][j+1]]=changeValue(A[f[i][j]-1][f[i][j+1]]);//上 A[f[i][j]][f[i][j+1]-1]=changeValue(A[f[i][j]][f[i][j+1]-1]);//左 A[f[i][j]][f[i][j+1]+1]=changeValue(A[f[i][j]][f[i][j+1]+1]);//右 }else if(f[i][j+1]==A[A.length-1].length-1){ A[f[i][j]-1][f[i][j+1]]=changeValue(A[f[i][j]-1][f[i][j+1]]);//上 A[f[i][j]][f[i][j+1]-1]=changeValue(A[f[i][j]][f[i][j+1]-1]);//左 A[f[i][j]+1][f[i][j+1]]=changeValue(A[f[i][j]+1][f[i][j+1]]);//下 }else if(f[i][j+1]==0){ A[f[i][j]-1][f[i][j+1]]=changeValue(A[f[i][j]-1][f[i][j+1]]);//上 A[f[i][j]][f[i][j+1]+1]=changeValue(A[f[i][j]][f[i][j+1]+1]);//右 A[f[i][j]+1][f[i][j+1]]=changeValue(A[f[i][j]+1][f[i][j+1]]);//下 }else{ A[f[i][j]-1][f[i][j+1]]=changeValue(A[f[i][j]-1][f[i][j+1]]);//上 A[f[i][j]][f[i][j+1]-1]=changeValue(A[f[i][j]][f[i][j+1]-1]);//左 A[f[i][j]][f[i][j+1]+1]=changeValue(A[f[i][j]][f[i][j+1]+1]);//右 A[f[i][j]+1][f[i][j+1]]=changeValue(A[f[i][j]+1][f[i][j+1]]);//下 } } } return A; } public int changeValue(int number){ if(number==0){ number=1; }else if(number==1){ number=0; } return number; } }