Problem Description
XQ,one of the three Sailormoon girls,is usually playing mobile games on the class.Her favorite mobile game is called “The Princess In The Wall”.Now she give you a problem about this game.

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.
 
Input
There are multiple test cases. Each case starts with two positive integer N, M,(N, M <= 6)the size of rectangular area. Then n lines follow, each contains m positive integers X.(0<= X <= 4)It means this grid have a colored square
block or nothing.
 
Output
Please output the minimum steps.
 
Sample Input
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
 
Sample Output
4
Hint
0 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
 
Author
B.A.C
 
Source

思路:由于方块会越消越少,所以不是必需判重。注意消去之后。上面的会掉下来,假设某一列全为是空的。右边的会往左移。左移的时候注意连续两列为空的情况,尽管数据弱。之前没考虑也AC了。

#include <stdio.h>

struct{
int d[6][6],step;
}que[1000000],t; int n,m,temp[6][6],nxt[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
bool vis[6][6]; void dfs(int x,int y,int num)
{
for(int i=0;i<4;i++)
{
x+=nxt[i][0];
y+=nxt[i][1]; if(x>=0 && x<n && y>=0 && y<m && !vis[x][y] && temp[x][y]==num)
{
vis[x][y]=1; t.d[x][y]=0; dfs(x,y,num);
} x-=nxt[i][0];
y-=nxt[i][1];
}
} int main()
{
int i,j,k,p,q,top,bottom;
bool flag; while(~scanf("%d%d",&n,&m))
{
for(i=0;i<n;i++) for(j=0;j<m;j++) scanf("%d",&que[0].d[i][j]); top=0;
bottom=1; que[0].step=0; while(top<bottom)
{
t=que[top]; flag=1; for(i=0;i<n && flag;i++) for(j=0;j<m && flag;j++) if(t.d[i][j]) flag=0; if(flag)
{
printf("%d\n",t.step); break;
} t.step++; for(i=0;i<n;i++) for(j=0;j<m;j++) temp[i][j]=t.d[i][j],vis[i][j]=0; for(i=0;i<n;i++) for(j=0;j<m;j++)
{
if(temp[i][j] && !vis[i][j])
{
vis[i][j]=1; t.d[i][j]=0; dfs(i,j,temp[i][j]); for(p=n-1;p>=0;p--)//向下移动
{
for(q=0;q<m;q++)
{
if(!t.d[p][q])
{
for(k=p-1;k>=0;k--)
{
if(t.d[k][q])
{
t.d[p][q]=t.d[k][q];
t.d[k][q]=0; break;
}
}
}
}
} int tt=m-1;
while(tt--)//向左移动,注意连续两列都为空的情况。
{
for(q=0;q<m-1;q++)
{
for(p=0;p<n;p++) if(t.d[p][q]) break; if(p<n) continue; for(p=0;p<n;p++)
{
t.d[p][q]=t.d[p][q+1];
t.d[p][q+1]=0;
}
}
} que[bottom++]=t; for(p=0;p<n;p++) for(q=0;q<m;q++) t.d[p][q]=temp[p][q];
}
} top++;
}
}
}

HDU-3295-An interesting mobile game(BFS+DFS)的更多相关文章

  1. HDU 1044 Collect More Jewels(BFS+DFS)

    Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  2. 【HDOJ】3295 An interesting mobile game

    其实就是一道搜索模拟题.因为数据量小,用char就够了. /* 3295 */ #include <iostream> #include <cstdio> #include & ...

  3. xtu summer individual 1 A - An interesting mobile game

    An interesting mobile game Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on H ...

  4. hdu 3295 模拟过程。数据很水

    An interesting mobile game Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Ja ...

  5. Cleaning Robot (bfs+dfs)

    Cleaning Robot (bfs+dfs) Here, we want to solve path planning for a mobile robot cleaning a rectangu ...

  6. POJ 2227 The Wedding Juicer (优先级队列+bfs+dfs)

    思路描述来自:http://hi.baidu.com/perfectcai_/item/701f2efa460cedcb0dd1c820也可以参考黑书P89的积水. 题意:Farmer John有一个 ...

  7. 邻结矩阵的建立和 BFS,DFS;;

    邻结矩阵比较简单,, 它的BFS,DFS, 两种遍历也比较简单,一个用队列, 一个用数组即可!!!但是邻接矩阵极其浪费空间,尤其是当它是一个稀疏矩阵的时候!!!-------------------- ...

  8. HDU.2612 Find a way (BFS)

    HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...

  9. Collect More Jewels(hdu1044)(BFS+DFS)

    Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

随机推荐

  1. day25-2 OSI协议和socket抽象层

    目录 OSI协议 物理层 数据链路层 以太网协议 Mac地址 广播地址 网络层 获取对方Mac地址(ARP协议) 传输层 TCP协议 UDP协议 应用层 socket抽象层 OSI协议 互联网的本质就 ...

  2. day21-5 类的多态与多态性

    类的多态与多态性 多态 多态指的是一类事物有多种形态,如动物有多种形态:人.狗.猪 import abc class Animal(metaclass=abc.ABCMeta): # 同一类事物:动物 ...

  3. java_线程分类

    线程分为守护线程和用户线程,如java虚拟机的回收机制就是守护线程,线程开始运行它就启动,线程结束它就结束 用户线程变守护线程:Thread(线程).setDaemon(true)

  4. JS_类数组

    [目录] 什么是数组 非类数组 类数组对象转化为数组 [类数组] 什么是类数组? 定义: 不具有数组的所具有的方法 拥有length属性,其属性(索引)为非负整数 类数组 var obj = { 0 ...

  5. IIS部署SSL证书后提示不可信的解决方案

    IIS部署SSL证书后提示不可信的解决方案   本帖最后由 wosign-support3 于 2015-7-17 17:18 编辑 第一步:打开mmc——点击文件——添加删除管理单元——证书——计算 ...

  6. 图解C/C++多级指针与多维数组

    声明:本文转自 chenyang_yao ,欢迎阅读原文. 指针与数组是C/C++编程中非常重要的元素,同时也是较难以理解的.其中,多级指针与“多维”数组更是让很多人云里雾里,其实,只要掌握一定的方法 ...

  7. Linux内核0.11体系结构 ——《Linux内核完全注释》笔记打卡

    0 总体介绍 一个完整的操作系统主要由4部分组成:硬件.操作系统内核.操作系统服务和用户应用程序,如图0.1所示.操作系统内核程序主要用于对硬件资源的抽象和访问调度. 图0.1 操作系统组成部分 内核 ...

  8. wepy 编译警告去除办法

    如果你用过wepy打包小程序的话,那么你一定碰到了很多坑,(什么也不用说,抱一下吧)下面记录的是本人遇到的一个小坑, 编译的时候出现了黄色警告 如果你出现了上图这样的话,相信你一定也知道什么意思,就是 ...

  9. selenium实战演练

    利用selenium以及pyquery,爬取当当网图书信息,并且将数据存入文件以及MongoDB数据库中. 配置文件: key="python" MONGO_URL='localh ...

  10. numpy——基础数组与计算

    In [1]: import numpy as np In [11]: # 创建数组 a = np.array([1,2,3,4,5]) In [12]: a Out[12]: array([1, 2 ...