hdu 3295 An interesting mobile game(bfs)
来源:互联网 发布:淘宝怎么搜苍蝇水 编辑:程序博客网 时间:2024/06/06 02:42
Can you solve it?The following picture show this problem better.
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 60 0 0 3 4 40 1 1 3 3 32 2 1 2 3 31 1 1 1 3 32 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 00 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 02 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 01 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 02 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;
}
- hdu 3295 An interesting mobile game(bfs)
- HDU-3295-An interesting mobile game(BFS+DFS)
- HDU 3295 An interesting mobile game
- hdu 3259 An interesting mobile game (dfs+bfs+模拟)
- hdu3295 An interesting mobile game (IDA*)
- HDU3295An interesting mobile game(BFS +模拟)
- BUPT OJ148 An Interesting Game
- 图论专题 An Interesting Game
- sdut2414 An interesting game 费用流
- 【SDUTOJ 2414】An interesting game(最小费用最大流)
- SDUT 2414 An interesting game(最大费用流)
- 【SDUT2414】An interesting game——最小费用
- HDUT-2414-An interesting game(最小费用最大流)
- 第三届山东ACM省赛 An interesting game
- hdu 2216 Game III(bfs)
- HDU 2351 - One is an Interesting Number (模拟)
- An Interesting Set
- Startup an interesting project
- 建立简单的哈希表
- Ubuntu 用户如何安装 Sublime Text 3文本编辑器
- poj 3468 A Simple Problem with Integers (线段树成段更新)
- 360 2015年在线笔试题:最后一个字符
- poj 1947 Rebuilding Roads 【树形DP】 【求至少删去树中 多少条边 使得树中节点数为P】
- hdu 3295 An interesting mobile game(bfs)
- 实现动态音量条
- 黑马程序员--封装--java
- linux设备模型之Kobject与Kset
- 黑马程序员—-C语言入门十重奏之九相守
- 学习java核心技术第3章的读书笔记
- OC_继承_类别_复合
- bzoj1818
- 自己动手写操作系统之参考资料