方块消除游戏(完美世界2017秋招真题)

5 篇文章 0 订阅

方块消除游戏(完美世界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<ArrayList<Integer>> 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<Max;i++){
				list.add(new ArrayList<Integer>(10));
			}
			for(int i=0;i<Max;i++){
				for(int j=0;j<Max;j++){
					list.get(i).add(p[9-j][i]);
				}
			}
			GetAns();
		}
	};

	private static void GetAns() {
		for (int i = 0; i < len; i++) {
			int x=(a[i]-1)%10;
			int y=9-(a[i]-1)/10;
			if(x>=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;j<list.size();j++){
				list.get(j).removeIf(s->s==FLAG);
			}
			list.removeIf(s->s.isEmpty()==true);
		}
		int k[]=new int[5];
		for(int i=0;i<list.size();i++){
			for(int j=0;j<list.get(i).size();j++){
				k[list.get(i).get(j)]++;
			}
		}
		for(int i=0;i<5;i++){
			if(i==0){
				System.out.print(k[i]);
			}else{
				System.out.print(" "+k[i]);
			}
		}
		System.out.println();
	};

	// 检查是否合法
	private static boolean Check(int x, int y) {
		if (x >= 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);
			}
		}
	}
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值