您的位置:首页 > 其它

消除类游戏

2016-04-04 17:12 176 查看
问题描述

  消除类游戏是深受大众欢迎的一种游戏,游戏在一个包含有n行m列的游戏棋盘上进行,棋盘的每一行每一列的方格上放着一个有颜色的棋子,当一行或一列上有连续三个或更多的相同颜色的棋子时,这些棋子都被消除。当有多处可以被消除时,这些地方的棋子将同时被消除。

  现在给你一个n行m列的棋盘,棋盘中的每一个方格上有一个棋子,请给出经过一次消除后的棋盘。

  请注意:一个棋子可能在某一行和某一列同时被消除。

输入格式

  输入的第一行包含两个整数n, m,用空格分隔,分别表示棋盘的行数和列数。

  接下来n行,每行m个整数,用空格分隔,分别表示每一个方格中的棋子的颜色。颜色使用1至9编号。

输出格式

  输出n行,每行m个整数,相邻的整数之间使用一个空格分隔,表示经过一次消除后的棋盘。如果一个方格中的棋子被消除,则对应的方格输出0,否则输出棋子的颜色编号。

样例输入

4 5

2 2 3 1 2

3 4 5 1 4

2 3 2 1 3

2 2 2 4 4

样例输出

2 2 3 0 2

3 4 5 0 4

2 3 2 0 3

0 0 0 4 4

样例说明

  棋盘中第4列的1和第4行的2可以被消除,其他的方格中的棋子均保留。

样例输入

4 5

2 2 3 1 2

3 1 1 1 1

2 3 2 1 3

2 2 3 3 3

样例输出

2 2 3 0 2

3 0 0 0 0

2 3 2 0 3

2 2 0 0 0

样例说明

  棋盘中所有的1以及最后一行的3可以被同时消除,其他的方格中的棋子均保留。

评测用例规模与约定

  所有的评测用例满足:1 ≤ n, m ≤ 30。

算法:

算法开始。
读入n和m。设置两个n*m大小的二位数组一个为matrix1,另一个为matrix2,并把它们的元素都初始化为0。
用双重循环读入游戏棋盘,存入matrix1。
扫描matrix1每一行,如果有连续三个甚至更多的相同颜色的棋子,则在matrix2里把对应位置设置为1。
扫描matrix1每一列,如果有连续三个甚至更多的相同颜色的棋子,则在matrix2里把对应位置设置为1。
用双重循环扫描matrix2,如果它的元素值为1,则把它在matrix1里对应位置的元素设置为0。
输出matrix1。
算法结束。
提示:可以使用栈来存储所读入的相同颜色的棋子,遇到颜色不同的棋子的时候,判断栈里的元素是否超过2个,如果是则一个个弹出栈里的元素,把在matrix2里对应位置的元素设置为1,否则清空栈。操作完栈以后,就把新的棋子压入栈。

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define SIZE 40

typedef struct node{
int x, y;
} node;

typedef struct stack{
node array[SIZE];
int top;
} stack;

void push(stack* ptr, node elem);
node pop(stack* ptr);
void flush(stack* ptr);
bool isempty(stack* ptr);
void print(int** matrix, int n, int m);

int main(int argc, const char * argv[]) {
int n, m, i, j;
int **matrix1, **matrix2;
node temp;
int prev_color;
stack* ptr = (stack*)calloc(1, sizeof(stack));
scanf("%d %d", &n, &m);
matrix1 = (int**)calloc(n, sizeof(int*));
matrix2 = (int**)calloc(n, sizeof(int*));

for(i = 0; i < n; i++){
matrix1[i] = (int*)calloc(m, sizeof(int));
matrix2[i] = (int*)calloc(m, sizeof(int));
}

for(i = 0; i < n; i++){
for(j = 0; j < m; j++){
scanf("%d", matrix1[i] + j);
}
}

for(i = 0; i < n; i++){
prev_color = matrix1[i][0];
temp.x = i;
temp.y = 0;
push(ptr, temp);
for(j = 1; j < m; j++){
if(matrix1[i][j] == prev_color){
temp.x = i;
temp.y = j;
push(ptr, temp);
}
else{
if(ptr->top > 2){
while(!isempty(ptr)){
temp = pop(ptr);
matrix2[temp.x][temp.y] = 1;
}
}
else{
flush(ptr);
}
temp.x = i;
temp.y = j;
prev_color = matrix1[i][j];
push(ptr, temp);
}
}
if(ptr->top > 2){
while(!isempty(ptr)){
temp = pop(ptr);
matrix2[temp.x][temp.y] = 1;
}
}
else{
flush(ptr);
}
}

for(i = 0; i < m; i++){
prev_color = matrix1[0][i];
temp.x = 0;
temp.y = i;
push(ptr, temp);
for(j = 1; j < n; j++){
if(matrix1[j][i] == prev_color){
temp.x = j;
temp.y = i;
push(ptr, temp);
}
else{
if(ptr->top > 2){
while(!isempty(ptr)){
temp = pop(ptr);
matrix2[temp.x][temp.y] = 1;
}
}
else{
flush(ptr);
}
temp.x = j;
temp.y = i;
prev_color = matrix1[j][i];
push(ptr, temp);
}
}
if(ptr->top > 2){
while(!isempty(ptr)){
temp = pop(ptr);
matrix2[temp.x][temp.y] = 1;
}
}
else{
flush(ptr);
}
}

for(i = 0; i < n; i++){
for(j = 0; j < m; j++){
if(matrix2[i][j]){
matrix1[i][j] = 0;
}
}
}

print(matrix1, n, m);

for(i = 0; i < n; i++){
free(matrix1[i]);
free(matrix2[i]);
}
free(matrix1);
free(matrix2);
return 0;
}

void push(stack* ptr, node elem){
ptr->array[ptr->top] = elem;
(ptr->top)++;
}

node pop(stack* ptr){
(ptr->top)--;
return ptr->array[ptr->top];
}

void flush(stack* ptr){
ptr->top = 0;
}

bool isempty(stack* ptr){
return !(ptr->top);
}

void print(int** matrix, int n, int m){
int i, j;
for(i = 0; i < n; i++){
for(j = 0; j < m - 1; j++){
printf("%d ", matrix[i][j]);
}
printf("%d\n", matrix[i][j]);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: