消消乐,相除相邻地一样地值。
/*
5 4
1 2 1 4 1
1 1 1 1 3
2 4 1 2 3
2 5 1 3 2
5 2 1 5 5
1 2
1 3
1 5
3 4
*/
#include <iostream>
#include <vector>
//#include <algorithm>
using namespace std;
int arrs[202][202] = { 0 };
int N = 0;
int deleteNode = 0;
void helper(int x, int y, int& num)
{
bool mark = false;
for (int i = 1; i <= 4; i++)
{
if (x > 0 && arrs[x - 1][y] == num){
arrs[x-1][y] = 0;
deleteNode++;
mark = true;
helper(x-1, y, num);
}
if (y > 0 && arrs[x][y - 1] == num) {
arrs[x][y - 1] = 0;
mark = true;
deleteNode++;
helper(x, y - 1, num);
}
if (y + 1 < N && arrs[x][y + 1] == num) {
mark = true;
deleteNode++;
arrs[x][y + 1] = 0;
helper(x, y + 1, num);
}
if(x + 1 < N && arrs[x+1][y] == num){
mark = true;
deleteNode++;
arrs[x + 1][y] = 0;
helper(x + 1, y, num);
}
}
if (mark == true)
arrs[x][y] = 0;
}
void printf()
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
cout << arrs[i][j] << " ";
cout << endl;
}
}
void fun()
{
for (int k = 0; k < N; k++)
{
for (int j = 0; j < N; j++)
{
for (int i = N - 1; i > 0; i--)
{
if (arrs[i][j] == 0)
swap(arrs[i][j], arrs[i-1][j]);
}
}
}
for (int k = 0; k < N; k++)
{
for (int j = 0; j < N; j++)
{
int count = 0;
for (int i = N - 1; i >= 0; i--)
{
if (arrs[i][j] == 0)
count++;
}
if (count == N)
{
for (int i = 0; i <= N - 1; i++)
swap(arrs[i][j], arrs[i][j + 1]);
}
}
}
}
int main()
{
int m, num, x, y;
cin >> N >> m;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
cin >> num;
arrs[i][j] = num;
}
}
for (int i = 0; i < m; i++)
{
deleteNode = 0;
cin >> x >> y;
num = arrs[x-1][y-1];
if (num == 0)
continue;
helper(x-1, y-1, num);
cout << deleteNode << endl;
fun();
printf();
}
return 0;
}
版权声明:本文为Up_junior原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。