code

/*
work by:Ariel_
*/
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <algorithm>
#define ll long long
#define rg register
using namespace std;
int x = 0,f = 1; char c = getchar();
while(c < '0'||c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') {x = x*10 + c - '0'; c = getchar();}
return x*f;
}
struct Ans{
int x, y, d;
}ans[6];
int n, k, mp[6][8], fag, last[6][6][8], Step;//mp[i][j]: x = i, y = j; 起始从(1, 1)开始，按照数学坐标系模拟
bool res[6][8];
void down() {//掉落
for (int i = 1; i <= 5; i++) {
int x = 0;
for (int j = 1; j <= 7; j++) {
if (!mp[i][j]) x++;
else {
if (x == 0) continue;
mp[i][j - x] = mp[i][j], mp[i][j] = 0;
}
}
}
}
bool delet() {//消除操作
int flag = 0;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 7; j++) {
if (i - 1 >= 1 && i + 1 <= 5 && mp[i - 1][j] == mp[i][j] && mp[i + 1][j] == mp[i][j] && mp[i][j]) {
res[i - 1][j] = 1, res[i][j] = 1, res[i + 1][j] = 1, flag = 1;
}
if(j - 1 >= 1 && j + 1 <= 7 && mp[i][j - 1] == mp[i][j] && mp[i][j] == mp[i][j + 1] && mp[i][j]) {
res[i][j] = 1, res[i][j - 1] = 1, res[i][j + 1] = 1, flag = 1;
}
}
}
if (flag == 0) return false;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 7; j++) {
if (res[i][j]) mp[i][j] = 0, res[i][j] = 0;
}
}
return true;
}
void move(int x, int y, int d) {
swap(mp[x][y], mp[x + d][y]);
down();
while(delet()) down();//连续消
}
bool check() {
for (int i = 1; i <= 5; i++) if (mp[i][1]) return 0;
return 1;
}
void copy(int x) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 7; j++) {
last[x][i][j] = mp[i][j];
}
}
}
void dfs(int step) {
if (check()) {
for (int i = 1; i <= n; i++) {
printf("%d %d %d\n", ans[i].x, ans[i].y, ans[i].d);
}
exit(0);
}
if (step == n + 1) return ;//超步数
copy(step);
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 7; j++) {
if (mp[i][j]) {
if (i + 1 <= 5 && mp[i][j] != mp[i + 1][j]){//右侧没有方块
move(i, j, 1);
ans[step].x = i - 1, ans[step].y = j - 1, ans[step].d = 1;
dfs(step + 1);
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 7; j++) {
mp[i][j] = last[step][i][j];
}
}
ans[step].x = -1, ans[step].y = -1, ans[step].d = -1;
}
if (i - 1 >= 1 && mp[i - 1][j] == 0) {
move(i, j, -1);
ans[step].x = i - 1, ans[step].y = j - 1, ans[step].d = -1;
dfs(step + 1);
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 7; j++) {
mp[i][j] = last[step][i][j];
}
}
ans[step].x = -1, ans[step].y = -1, ans[step].d = -1;
}
}
}
}
}
int main(){