给你我的代码吧,考的时候时间差一点,考完自己再补充完成的,本地运行没什么问题,代码比较长
import java.util.Scanner;
public class WanMeiShiJie {
public static final int EMPTY = Integer.MAX_VALUE;
public static final int RED = 0;
public static final int GREEN = 1;
public static final int BLUE = 2;
public static final int YELLOW = 3;
public static final int PURPLE = 4;
public static final int[][] p = {
{RED,RED,BLUE,BLUE,GREEN,YELLOW,BLUE,YELLOW,RED,PURPLE},
{GREEN,GREEN,GREEN,BLUE,RED,PURPLE,RED,YELLOW,YELLOW,BLUE},
{BLUE,RED,RED,YELLOW,YELLOW,PURPLE,BLUE,GREEN,GREEN,BLUE},
{YELLOW,RED,BLUE,YELLOW,BLUE,RED,PURPLE,GREEN,GREEN,RED},
{YELLOW,RED,BLUE,BLUE,PURPLE,GREEN,PURPLE,RED,YELLOW,BLUE},
{PURPLE,YELLOW,RED,RED,YELLOW,RED,PURPLE,YELLOW,RED,RED},
{YELLOW,YELLOW,GREEN,PURPLE,GREEN,RED,BLUE,YELLOW,BLUE,GREEN},
{RED,YELLOW,BLUE,BLUE,YELLOW,GREEN,PURPLE,RED,BLUE,GREEN},
{GREEN,GREEN,YELLOW,YELLOW,RED,RED,PURPLE,BLUE,BLUE,GREEN},
{PURPLE,BLUE,RED,RED,PURPLE,YELLOW,BLUE,RED,RED,GREEN}};
public static void main(String[] args) {
int numR, numG, numB, numY, numP;
numR = numG = numB = numY = numP = 0;
for(int i = 0; i < p.length; i++){
for(int j = 0; j < p[0].length; j++){
switch (p[i][j]) {
case RED:
numR++;
break;
case GREEN:
numG++;
break;
case BLUE:
numB++;
break;
case YELLOW:
numY++;
break;
case PURPLE:
numP++;
break;
default:
break;
}
}
}
System.out.println("inital :" + numR + " " + numG + " " + numB + " " + numY + " " + numP );
Scanner cin = new Scanner(System.in);
while(cin.hasNextLine()){
String str = cin.nextLine();
String[] nums = str.split(" ");
for(String num : nums){
int click = Integer.parseInt(num);
int x = click / 10;
int y = (click - 1) % 10;
int color = p[x][y];
int delete = countContinue(p, x, y);
switch (color) {
case RED:
numR -= delete;
break;
case GREEN:
numG -= delete;
break;
case BLUE:
numB -= delete;
break;
case YELLOW:
numY -= delete;
break;
case PURPLE:
numP -= delete;
break;
default:
break;
}
for(int i = 0; i < p[0].length; i++){
if(p[p.length - 1][i] == EMPTY)
moveCols(p, i);
}
}
System.out.println(numR + " " + numG + " " + numB + " " + numY + " " + numP );
}
}
public static int countContinue(int[][] p, int x, int y){
int color = p[x][y];
int start = x;
int count = 1;
while(start + 1 < p.length && p[start + 1][y] == color){
start ++;
}
int idx = start;
while(idx - 1 >= 0 && p[idx - 1][y] == color){
count ++;
idx--;
}
dispearCol(p, y, start, count);
int beforeY = y - 1;
int afterY = y + 1;
while(beforeY >= 0 && p[x][beforeY] == color){
dispearCol(p, beforeY, x, 1);
beforeY--;
count++;
}
while(afterY < p[0].length && p[x][afterY] == color){
dispearCol(p, afterY, x, 1);
afterY ++;
count++;
}
return count;
}
public static void dispearCol(int[][] p, int col, int start, int count){
int j = start;
for(int i = start - count; i >= 0; i--, j--){
p[j][col] = p[i][col];
}
for(; j >=0; j--)
p[j][col] = EMPTY;
}
public static void moveCols(int[][] p, int col){
for(int i = col, j = i + 1; j < p[0].length; i++, j++){
for(int k = 0; k < p.length; k++){
p[k][i] = p[k][j];
}
}
if(col < p[0].length - 1){
for(int k = 0; k < p.length; k++){
p[k][p[0].length - 1] = EMPTY;
}
}
}
}