算法实战1:爱消除(Elimination Game)

have you played the elimination game before? When you hit a rectangle, other rectangles which are adjacent and in same color will be cleared. We could use a n*m matrix to indicate the game board. Numbers in the matrix indicate different color. Same number means same color.
For example: this is a 5*4 game board:
1 1 2 4
2 3 3 3
2 2 3 5
3 3 3 3
1 2 3 4
The matrix index starts from 0. After you hit the rectangle at (3, 3), all the adjacent rectangle with number 3 will be cleared. We use number 0 to mark the cleared rectangle. The result will be: 
1 1 2 4
2 0 0 0
2 2 0 5
0 0 0 0
1 2 0 4
Put gravity into consideration, rectangles which locates on top of the 0-rect will fall down and the result will be like this:
0 0 0 0
1 0 0 0
2 1 0 4
2 2 0 5
1 2 2 4
The question is, given a m*n matrix and a (x,y) indicate which rectangle is hit, compute the result of this hit.
Input:
4 // the number of the columns
5 // the number of the rows
1 1 2 4 // each line of the metrix. Number are separated by space.
2 3 3 3
2 2 3 5
3 3 3 3
1 2 3 4
3 // the x index of the hit position
3 // the y index of the hit position
Output:
0 0 0 0 
1 0 0 0
2 1 0 4
2 2 0 5
1 2 2 4
The output should be the matrix status line by line. Within each line, the numbers are separate by space.

-------------------------------------------------

这道题很适合现在火爆了的爱消除!(Eliminating Game)

分两步骤:

(1)首先点击某个数字后,希望能够删除掉颜色相近的

(2)对数组重新排列

第一步如何消除呢?我们知道,点击了某个块,我们需要遍历它的四周四个方块,如果数值相同那么也要继续遍历,

到这里很明显有两个思路:一是可以采用递归的方式,二是可以采用队列或者栈的形式来找出所有要删除的方块

第二步就是一个下沉的过程,其实就是个冒泡过程把0冒泡上去,这里要的是稳定的冒泡,不能乱了顺序

所以解法见代码咯

//============================================================================
// Name        : EliminatingGame.cpp
// Author      : YLF
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;

//简单起见,先假设最大行数,列数20
#define ROW_NUM 20
#define COL_NUM 20

void EliminateBlock(int block[ROW_NUM][COL_NUM], int hitRow, int hitCol, int rowNum, int colNum);
void ReOrder(int block[ROW_NUM][COL_NUM], int rowNum, int colNum);
void PrintBlock(int block[ROW_NUM][COL_NUM], int rowNum, int colNum);

int main() {
	int rowNum = 0;
	int colNum = 0;
	//input
	cin>>colNum;
	cin>>rowNum;
	cout<<"input block"<<endl;
	int block[ROW_NUM][COL_NUM];
	int i = 0, j = 0;
	for(i=0;i<rowNum;i++)
		for(j=0;j<colNum;j++)
			cin>>block[i][j];

	int hitCol = 0;
	int hitRow = 0;
	cin>>hitCol;
	cin>>hitRow;
	//eliminate
	EliminateBlock(block, hitRow, hitCol, rowNum, colNum);
	ReOrder(block, rowNum, colNum);
	PrintBlock(block, rowNum, colNum);
	return 0;
}

/*
 * using for eliminate
 */
void EliminateBlock(int block[ROW_NUM][COL_NUM], int hitRow, int hitCol, int rowNum, int colNum){
	int value = block[hitRow][hitCol];
	block[hitRow][hitCol] = 0;
	//up
	if(hitRow-1>=0 && block[hitRow-1][hitCol]==value)
		EliminateBlock(block, hitRow-1, hitCol, rowNum, colNum);
	//left
	if(hitCol-1>=0 && block[hitRow][hitCol-1]==value)
		EliminateBlock(block, hitRow, hitCol-1, rowNum, colNum);
	//down
	if(hitRow+1<rowNum && block[hitRow+1][hitCol]==value)
		EliminateBlock(block, hitRow+1, hitCol, rowNum, colNum);
	//right
	if(hitCol+1<colNum && block[hitRow][hitCol+1]==value)
		EliminateBlock(block, hitRow, hitCol+1, rowNum, colNum);
}

/*
 * using for re-order
 * bottom-up left-right
 */
void ReOrder(int block[ROW_NUM][COL_NUM], int rowNum, int colNum){
	int j = 0;
	int index1 = rowNum-1, index2 = rowNum - 1;
	for(j=0;j<colNum;j++){
		index1 = rowNum-1;
		while(index1>=0){
			while(index1>=0 && block[index1][j] != 0)
				index1--;
			index2 = index1-1;
			while(index2>=0 && block[index2][j] == 0)
				index2--;
			if(index2>=0){
				block[index1][j] = block[index2][j];
				block[index2][j] = 0;
			}
			index1--;
		}
	}
}

/*
 * print block
 */
void PrintBlock(int block[ROW_NUM][COL_NUM], int rowNum, int colNum){
	int i = 0, j = 0;
	cout<<endl;
	for(i=0;i<rowNum;i++){
		for(j=0;j<colNum;j++)
			cout<<block[i][j]<<" ";
		cout<<endl;
	}
}


这道题给我的思考:

让我想起了之前遇到的一个面试题,考官问到类似这题的题目,当时问我除了递归还有别的非递归方法吗?

我那是绞尽脑汁,也紧张,一是半会没想出,于是的于是。。。。其实用队列是很好实现的现将我们需要删除的元素添加进去,出队列的时候把它周围值相等的加入,但是前提,加入队列后这个元素值必须置为0,否则会出现死循环了。。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值