完美世界-方块消除游戏
/**
纯逻辑操作,仅抛砖引玉
测试通过88%,时间超时
**/
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class FangKuai {
static int RED = 0, GREEN = 1, BLUE = 2, YELLOW = 3, PURPLE = 4, DEL = -1;
static 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) {
Scanner in = new Scanner(System.in);
String[] arrStr = in.nextLine().trim().split(" ");
int[] arrColor = {26, 18, 22, 21, 13}; // 颜色方块计数
for (int i = 0; i < arrStr.length; i++) {
int click = Integer.parseInt(arrStr[i]);
xiaochu(p, click, arrColor);
}
for (int k = 0; k < arrColor.length; k++) // 输出结果
System.out.print(arrColor[k] + " ");
}
public static void xiaochu(int[][] p, int clickNum, int[] arrColor) {
if (p == null)
return;
Queue<Integer> queue = new LinkedList<Integer>(); // 队列存储需要环顾四周的节点位置
queue.add(--clickNum); // 当前点击点处理
int r = clickNum / 10;
int c = clickNum % 10;
int color = p[r][c];
p[r][c] = DEL;
int count = 1;
while (!queue.isEmpty()) {
int click = queue.poll(); // 处理节点上下左右四个相邻节点
int row = click / 10;
int col = click % 10;
if (row < 9 && p[row+1][col] == color) {
queue.add((row+1)*10 + col);
p[row+1][col] = DEL;
count++;
}
if (row > 0 && p[row-1][col] == color) {
queue.add((row-1)*10 + col);
p[row-1][col] = DEL;
count++;
}
if (col > 0 && p[row][col-1] == color) {
queue.add(row*10 + col-1);
p[row][col-1] = DEL;
count++;
}
if (col < 9 && p[row][col+1] == color) {
queue.add(row*10 + col+1);
p[row][col+1] = DEL;
count++;
}
}
if (count > 1) {
arrColor[color] -= count; // 减少已清理颜色
for (int i = 0; i < 10; i++) { // 数组整理,已删除项上移 for (int j = 0; j < 10; j++) {
if (p[j][i] == DEL) {
int start = j-1;
while (start >= 0 && p[start][i] != DEL) {
int tmp = p[start+1][i];
p[start+1][i] = p[start][i];
p[start][i] = tmp;
start--; }
}
} }
int[] arrTmp = new int[10]; // 验证是否有某列全清理了
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
arrTmp[i] += p[j][i];
}
}
for (int k = arrTmp.length-2; k >= 0; k--) {
if (arrTmp[k] == -10) { // 整列往左移
for (int i = k+1; i < 10; i++) {
for (int j = 0; j < 10; j++) {
int tmp = p[j][i-1];
p[j][i-1] = p[j][i];
p[j][i] = tmp; }
}
}
}
}
else
p[r][c] = color;
}
}
#完美世界##Java工程师#
