方块消除游戏(完美世界2017秋招真题)
题目描述如下图,有10*10个不同颜色的方块,每个方块可能是红、绿、蓝、黄、紫5种颜色之一。当点击其中某一个方块时,如果它有相邻的同颜色方块,则将所有与此方块连续同颜色相邻的方块消除;剩下的方块中,如果下方有空位则向下移动,如果左侧整列都为空位则向左移动。
输入
输入数据有多组,每组占一行,包括一个或多个正整数,取值范围为1~100。每个数代表一次点击,数值为点击的方块编号。 上图中的方块初始值定义已为你写好,可以直接粘贴使用: const int RED = 0, GREEN = 1, BLUE = 2, YELLOW = 3, PURPLE = 4; int p[10][10] = { {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}};
|
样例输入
6 6 1
|
输出
对于每个测试实例,要求输出连续各次点击全部完成之后,红、绿、蓝、黄、紫色方块的数量; 每个测试实例的输出占一行。
|
样例输出
26 18 22 21 13 24 18 22 21 13
|
时间限制
C/C++语言:1000MS
其它语言:3000MS
|
内存限制
C/C++语言:65536KB
其它语言:589824KB
|
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* 作者:张宇翔 创建日期:2017年7月27日 上午11:35:04 描述:
*/
public class Main {
private static final int Max = (int) (10);
private static int FLAG = -1;
private static String s;
private static int[] a;
private static int len;
private static final int RED = 0, GREEN = 1, BLUE = 2, YELLOW = 3, PURPLE = 4;
private static ArrayList> list;
private 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 } };
private static int[][] dir = new int[][] { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
public static void main(String[] args) {
InitData();
}
private static void InitData() {
Scanner cin = new Scanner(System.in);
s = new String();
while (cin.hasNext()) {
s = cin.nextLine();
String[] ans = s.split(" ");
len = ans.length;
a = new int[len];// 每行输入的数
for (int i = 0; i < len; i++) {
a[i] = Integer.parseInt(ans[i]);
}
list=new ArrayList<>();
for(int i=0;i(10));
}
for(int i=0;i=list.size()||y>=list.get(x).size()){
continue;
}
boolean ok=false;
for(int j=0;j<4;j++){
int newx=x+dir[j][0];
int newy=y+dir[j][1];
if(Check(newx, newy)&&list.get(newx).get(newy)==list.get(x).get(y)){
ok=true;
break;
}
}
//如果ok=true,说明有星星可以消除
if(!ok){
continue;
}
dfs( x, y, list.get(x).get(y));
for(int j=0;js==FLAG);
}
list.removeIf(s->s.isEmpty()==true);
}
int k[]=new int[5];
for(int i=0;i= 0 && x < list.size() && y >= 0 && y < list.get(x).size())
return true;
return false;
}
//搜索,标记
private static void dfs(int x,int y,int color) {
list.get(x).set(y, FLAG);
for(int i=0;i<4;i++){
int newx=x+dir[i][0];
int newy=y+dir[i][1];
if(Check(newx, newy)&&list.get(newx).get(newy)==color){
dfs(newx, newy, color);
}
}
}
}