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;
}
}