Can you solve it?The following picture show this problem better.
![](https://i-blog.csdnimg.cn/blog_migrate/6b8ceea0ea474827009c2d59ba1c35a9.jpeg)
This game is played on a rectangular area.This area is divided into some equal square grid..There are N rows and M columns.For each grid,there may be a colored square block or nothing.
Each grid has a number.
“0” represents this grid have nothing.
“1” represents this grid have a red square block.
“2” represents this grid have a blue square block.
“3” represents this grid have a green square block.
“4” represents this grid have a yellow square block.
1. Each step,when you choose a grid have a colored square block, A group of this block and some connected blocks that are the same color would be removed from the board. no matter how many square blocks are in this group.
2. When a group of blocks is removed, the blocks above those removed ones fall down into the empty space. When an entire column of blocks is removed, all the columns to the right of that column shift to the left to fill the empty columns.
Now give you the number of the row and column and the data of each grid.You should calculate how many steps can make the entire rectangular area have no colored square blocks at least.
5 6 0 0 0 3 4 4 0 1 1 3 3 3 2 2 1 2 3 3 1 1 1 1 3 3 2 2 1 4 4 4
4Hint0 0 0 3 4 4 0 0 0 4 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 3 3 3 0 0 3 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2 1 2 3 3 0 0 3 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 3 3 2 2 2 3 3 0 2 2 2 4 4 0 2 2 0 0 0 0 0 0 0 0 0 0 2 2 1 4 4 4 2 2 4 4 4 0 2 2 4 4 4 0 2 2 2 0 0 0 0 0 0 0 0 0
题意:一个棋盘,0表示没放棋子,1 2 3 4表示放了该颜色的棋子,每次选择一个棋子消掉,同时跟它相连的同色棋子也会被消掉,消掉棋子后形成空格0,空格上方的棋子会掉下来,填补空格。如果某列全为0,右边整体向左移,填补空列。
-
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
struct node
{
int map[6][6];
int step;
node()
{
step=0;
}
}a,temp;
int n,m;
int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
bool vis[6][6];
void dfs(int x,int y,int val)//找连通块
{
for (int i=0;i<4; i++)
{
x+=dir[i][0];
y+=dir[i][1];
if(x>=0 && x<n && y>=0 && y<m &&a.map[x][y]==val && !vis[x][y])
{
vis[x][y]=1;
a.map[x][y]=0;
dfs(x,y,val);
}
x-=dir[i][0];
y-=dir[i][1];
}
}
void bfs()
{
queue<node>q;
a.step=0;
q.push(a);
bool flag;
while (!q.empty())
{
a=q.front();
q.pop();
flag=1;
for (int i=0; i<m; i++)//检测是否全为0
{
if(a.map[n-1][i])
{
flag=0;
break;
}
}
if(flag)
{
printf("%d\n",a.step);
return;
}
a.step++;
temp=a;
memset(vis,false, sizeof(vis));
for (int i=0; i<n; i++) //下移
{
for (int j=0; j<m; j++)
{
if(temp.map[i][j] && !vis[i][j])
{
vis[i][j]=1;
a.map[i][j]=0;
dfs(i, j, temp.map[i][j]);
for (int s=n-1; s>=0; s--)
{
for (int w=0; w<m; w++)
{
if(!a.map[s][w])
{
for (int k=s-1; k>=0; k--)
{
if(a.map[k][w])
{
a.map[s][w]=a.map[k][w];
a.map[k][w]=0;
break;
}
}
}
}
}
for (int s=0; s<m; s++)//左移
{
if(a.map[n-1][s]==0)
{
for (int w=s; w<m-1; w++)
{
for (int k=0; k<n; k++)
{
a.map[k][w]=a.map[k][w+1];
}
}
}
}
q.push(a);
a=temp;
}
}
}
}
}
int main()
{
while (scanf("%d%d",&n,&m)!=EOF)
{
for (int i=0; i<n; i++)
{
for (int j=0; j<m; j++)
scanf("%d",&a.map[i][j]);
}
bfs();
}
return 0;
}