Skip to content

Instantly share code, notes, and snippets.

@riceluxs1t
Created November 25, 2015 01:26
Show Gist options
  • Save riceluxs1t/cb7dca661baf5591885b to your computer and use it in GitHub Desktop.
Save riceluxs1t/cb7dca661baf5591885b to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
char board[12][6];
int processed[12][6];
char dir[4][2] = {
{-1,0}, //up
{1,0}, //down
{0,1}, //right
{0,-1}, //left
};
int is_out(int i, int j) {
if (i < 0 | i > 11 | j<0 | j>5) return 1;
return 0;
}
int is_valid(int i, int j, char compareMe) {
if (is_out(i,j) | board[i][j] != compareMe) return 0;
return 1;
}
void clear(int i, int j, char previous) {
if (is_out(i,j)) return; // out of board.
if (board[i][j] =='.') return;
if (board[i][j] != previous) return;
if (board[i][j] == previous) {
char prev = board[i][j];
board[i][j] = '.';
int k, dx, dy;
for (k=0;k<4;k++) {
dx = dir[k][0];
dy = dir[k][1];
clear(i+dx, j+dy, prev);
}
}
return;
}
int remove(int i, int j, char previous, int init) {
if (is_out(i,j)) return 0; // out of board.
if (processed[i][j]) return 0; // already visited
if (board[i][j] != previous) return 0; // diff piece
processed[i][j] = 1; // mark it
int k, dx, dy;
int can_remove = 1;
for (k=0;k<4;k++) {
dx = dir[k][0];
dy = dir[k][1];
can_remove += remove(i+dx, j+dy, board[i][j], 0);
}
if (init == 1 & can_remove >= 4) {
clear(i, j, board[i][j]);
return 1;
}
if (init == 1 & can_remove<4) return 0;
return can_remove;
}
void move() {
int i,j,k;
for (j=0; j<6; j++) {
k = 11;
for (i=11; i>=0; i--) {
if (board[i][j] != '.') {
board[k][j] = board[i][j];
if (k > i) board[i][j] = '.';
k--;
}
}
}
}
bool cascade() {
int is_done = 0;
int i,j;
for (i=0; i<12; i++)
for (j=0; j<6; j++) {
if (board[i][j] != '.') {
is_done += remove(i,j, board[i][j], 1);
}
}
return is_done;
}
int main(void) {
//read in the board
int i,j;
for (i=0; i<12; i++)
for (j=0; j<6; j++) {
cin >> board[i][j];
}
int is_done = 1;
int count = 0;
while (is_done > 0) {
is_done=cascade();
move();
for (i=0; i<12; i++)
for (j=0; j<6; j++) {
processed[i][j]=0;
}
count += is_done;
}
cout << count << endl;
}
소스코드
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment