方块消除游戏

如下图,有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}};

输出

对于每个测试实例,要求输出连续各次点击全部完成之后,红、绿、蓝、黄、紫色方块的数量; 每个测试实例的输出占一行。



#include <string>
#include <iostream>
#include <queue>
#include <sstream>
#include <vector>

using namespace std;

struct Node{
	int row, col;
	Node(int _row, int _col) :col(_col), row(_row){}
};

const int RED = 0, GREEN = 1, BLUE = 2, YELLOW = 3, PURPLE = 4, EMPTY = 5;
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 } };


void bfs(vector<vector<int> > &p, int x, int y){
	if (x > p.size() || y > p[x].size())
		return;
	queue<Node> nodes;
	int dirct[4][2] = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } };
	nodes.push(Node(x,y));
	int coler = p[x][y];
	p[x][y] = EMPTY;
	int cnt = 0;
	while (!nodes.empty()){
		Node node = nodes.front();
		nodes.pop();
		for (int i = 0; i < 4; i++){
			int row = node.row + dirct[i][1];
			int col = node.col + dirct[i][0];
			bool cango = (row >= 0 && col >= 0 && row < p.size() && col < p[row].size() && p[row][col] == coler);
			if (cango){
				nodes.push(Node(row, col));
				p[row][col] = EMPTY;
				cnt++;
			}
		}
	}
	if (cnt == 0){
		p[x][y] = coler;
	}
}

void down(vector<vector<int> > &p){
	for (int x = 0; x < p.size(); x++){
		for (auto it = p[x].begin(); it != p[x].end();){
			if (*it == EMPTY){
				it = p[x].erase(it);
				if (it == p[x].end())
					break;
				continue;
			}
			it++;
		}
		if (p[x].empty())
			p.erase(p.begin() + x);
	}
}

void getcount(vector<vector<int> > &map, int ans[]){
	for (int i = 0; i < map.size(); i++){
		for (int j = 0; j < map[i].size(); j++){
			ans[map[i][j]]++;
		}
	}
}

int _tmain(int argc, _TCHAR* argv[])
{

	int m;
	int ans[6] = { 0 };
	stringstream ss;
	string str;
	
	while(getline(cin, str)){
		vector<vector<int> > map(10, vector<int>(10));
		ss.clear();
		ss << str;
		for (int i = 0; i < 10; i++){
			for (int j = 0; j <10; j++)
				map[i][9-j] = p[j][i];
		}
		while (ss >> m){
			int x, y;
			x = (m - 1) / 10;
			y = m - 10 * x - 1;
			x = 9 - x;
			bfs(map, y, x);
			down(map);
		}
		memset(ans, 0, sizeof(ans));
		getcount(map, ans);
		for (int i = 0; i < 4; i++)
			cout << ans[i] << " ";
		cout << ans[4] << endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值