[程式設計挑戰]彩色石子,僅供參考
阿新 • • 發佈:2018-12-09
public class ColoredStoneTest { /** * @param args * 有一行彩色的棋子,每個棋子的顏色是k種顏色之一。 * 你不能改變棋子的順序,但是可以移走一些棋子。 * 問至少移走多少個石子,才能使得兩個同色的石子之間沒有其他顏色的棋子? * 輸入格式: 多組資料,每組資料兩行,第一行是兩個整數n和k, 1<=n<=100, 1<=k<=5 * 下一行是n個在[1..k]範圍內的正整數,代表每個棋子的顏色。 輸出格式: * 每組測試資料輸出一行包含一個整數,表示至少移走的石子數。 * 注:可以移走第2個第7個棋子 * 首先只是針對兩種顏色來進行求解
,使用動態規劃演算法。 */ public static void main(String[] args) { int color = 2; int []colorArray = new int[]{1,2,1,1,1,2,2,1,1,2}; System.out.println(colorStone(colorArray)); } public static int colorStone(int []colorArray){ int len = colorArray.length; //動態規範陣列,儲存計算結果 int dynamic[][] = new int[len][len]; //儲存最後一個石頭是否是最小移動石頭中可能的一個,為0表示不是,1表示是可選,2表示必須是 int lastStone[][] = new int[len][len]; for(int i=0;i<len;i++){ dynamic[i][i] = 0; if(i<len-1 && colorArray[i]==colorArray[i+1]){ dynamic[i][i+1] = 0; lastStone[i][i+1] = 0; }else if(i<len-1 ){ dynamic[i][i+1] = 1; lastStone[i][i+1] = 1; } } for(int j=2;j<len;j++){ for(int i=0;i<len;i++){ if(i+j<len){ if(colorArray[i+j] == colorArray[i+j-1]){ if(lastStone[i][i+j-1] == 0){ dynamic[i][i+j] = dynamic[i][i+j-1]; lastStone[i][i+j] = 0; }else if(lastStone[i][i+j-1] == 1){ dynamic[i][i+j] = dynamic[i][i+j-1]; lastStone[i][i+j] = 0; }else if(lastStone[i][i+j-1] == 2){ dynamic[i][i+j] = dynamic[i][i+j-1]-1; lastStone[i][i+j] = 0; }else{ return -1; } }else{ if(lastStone[i][i+j-1] == 0){ dynamic[i][i+j] = dynamic[i][i+j-1]+1; lastStone[i][i+j] = 2; }else if(lastStone[i][i+j-1] == 1){ dynamic[i][i+j] = dynamic[i][i+j-1]; lastStone[i][i+j] = 0; }else if(lastStone[i][i+j-1] == 2){ dynamic[i][i+j] = dynamic[i][i+j-1]; lastStone[i][i+j] = 0; }else{ return -1; } } } } } prtArray(dynamic); return dynamic[0][len-1]; } public static void prtArray(int [][]dynamic){ int row = dynamic.length; int col = dynamic[0].length; System.out.println(row+","+col); for(int i=0;i<row;i++){ for(int j=0;j<col;j++){ System.out.print(dynamic[i][j] + " "); } System.out.println(); } } }
public class ColoredStoneTest {
/**
* @param args
* 有一行彩色的棋子,每個棋子的顏色是k種顏色之一。
* 你不能改變棋子的順序,但是可以移走一些棋子。
* 問至少移走多少個石子,才能使得兩個同色的石子之間沒有其他顏色的棋子?
* 輸入格式: 多組資料,每組資料兩行,第一行是兩個整數n和k, 1<=n<=100, 1<=k<=5
* 下一行是n個在[1..k]範圍內的正整數,代表每個棋子的顏色。 輸出格式:
* 每組測試資料輸出一行包含一個整數,表示至少移走的石子數。
* 注:可以移走第2個第7個棋子
* 對2種以上的顏色進行處理,程式碼目前沒有進行優化,看起來估計不太容易理解。
*/
public static void main(String[] args) {
int color = 2;
int []colorArray = new int[]{1,2,1,1,1,2,2,1,1,2};
System.out.println(colorStone(colorArray,color));
int color2 = 5;
int []colorArray2 = new int[]{1,1,2,3,4,5,1,1,3,4,3};
System.out.println(colorStone(colorArray2,color2));
int color3 = 5;
int []colorArray3 = new int[]{1,2,3,4,5};
System.out.println(colorStone(colorArray3,color3));
}
public static int colorStone(int []colorArray,int colMax){
int len = colorArray.length;
//動態規範陣列,儲存計算結果
int dynamic[][] = new int[len][len];
//儲存最後一個石頭是否是最小移動石頭中可能的一個,為0表示不是,1表示是可選,2表示必須是
int lastStone[][] = new int[len][len];
for(int i=0;i<len;i++){
dynamic[i][i] = 0;
if(i<len-1 && colorArray[i]==colorArray[i+1]){
dynamic[i][i+1] = 0;
lastStone[i][i+1] = 0;
}else if(i<len-1 ){
dynamic[i][i+1] = 1;
lastStone[i][i+1] = 1;
}
}
for(int j=2;j<len;j++){
for(int i=0;i<len;i++){
if(i+j<len){
if(colorArray[i+j] == colorArray[i+j-1]){
if(lastStone[i][i+j-1] == 0){
dynamic[i][i+j] = dynamic[i][i+j-1];
lastStone[i][i+j] = 0;
}else if(lastStone[i][i+j-1] == 1){
dynamic[i][i+j] = dynamic[i][i+j-1];
lastStone[i][i+j] = 0;
}else if(lastStone[i][i+j-1] == 2){
dynamic[i][i+j] = dynamic[i][i+j-1]-1;
lastStone[i][i+j] = 0;
}else{
return -1;
}
}else{
if(lastStone[i][i+j-1] == 0){
dynamic[i][i+j] = dynamic[i][i+j-1]+1;
lastStone[i][i+j] = 2;
}else if(lastStone[i][i+j-1] == 1){
if(findColor(colorArray,colMax,i+j-1,colorArray[i+j])){
dynamic[i][i+j] = dynamic[i][i+j-1];
lastStone[i][i+j] = 0;
}else{
dynamic[i][i+j] = dynamic[i][i+j-1]+1;
lastStone[i][i+j] = 1;
}
}else if(lastStone[i][i+j-1] == 2){
if(findColor(colorArray,colMax,i+j-1,colorArray[i+j])){
dynamic[i][i+j] = dynamic[i][i+j-1]-1;
lastStone[i][i+j] = 0;
}else{
dynamic[i][i+j] = dynamic[i][i+j-1]+1;
lastStone[i][i+j] = 2;
}
}else{
return -1;
}
}
}
}
}
prtArray(dynamic);
return dynamic[0][len-1];
}
public static void prtArray(int [][]dynamic){
int row = dynamic.length;
int col = dynamic[0].length;
System.out.println(row+","+col);
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
System.out.print(dynamic[i][j] + " ");
}
System.out.println();
}
}
public static boolean findColor(int []colorArray,int colorMax, int position, int color){
if(position > colorArray.length){
return false;
}
int delPos = position - colorMax-1;
if(delPos < 0) delPos = 0;
for(int i=position; i>delPos; i--){
if(colorArray[i]==colorArray[i-1]){
delPos = i+1;
break;
}
}
for(int i=delPos;i<position;i++){
if(colorArray[i] == color){
return true;
}
}
return false;
}
}