HDU-3295-An interesting mobile game(BFS+DFS)
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.
block or nothing.
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
思路:由于方块会越消越少,所以不是必需判重。注意消去之后。上面的会掉下来,假设某一列全为是空的。右边的会往左移。左移的时候注意连续两列为空的情况,尽管数据弱。之前没考虑也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)的更多相关文章
- HDU 1044 Collect More Jewels(BFS+DFS)
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- 【HDOJ】3295 An interesting mobile game
其实就是一道搜索模拟题.因为数据量小,用char就够了. /* 3295 */ #include <iostream> #include <cstdio> #include & ...
- 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 ...
- hdu 3295 模拟过程。数据很水
An interesting mobile game Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Ja ...
- Cleaning Robot (bfs+dfs)
Cleaning Robot (bfs+dfs) Here, we want to solve path planning for a mobile robot cleaning a rectangu ...
- POJ 2227 The Wedding Juicer (优先级队列+bfs+dfs)
思路描述来自:http://hi.baidu.com/perfectcai_/item/701f2efa460cedcb0dd1c820也可以参考黑书P89的积水. 题意:Farmer John有一个 ...
- 邻结矩阵的建立和 BFS,DFS;;
邻结矩阵比较简单,, 它的BFS,DFS, 两种遍历也比较简单,一个用队列, 一个用数组即可!!!但是邻接矩阵极其浪费空间,尤其是当它是一个稀疏矩阵的时候!!!-------------------- ...
- HDU.2612 Find a way (BFS)
HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...
- Collect More Jewels(hdu1044)(BFS+DFS)
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
随机推荐
- 引用类型 (Reference Type Matters)、扩展与派发方式
引用类型 (Reference Type Matters) 引用的类型决定了派发的方式. 这很显而易见, 但也是决定性的差异. 一个比较常见的疑惑, 发生在一个协议拓展和类型拓展同时实现了同一个函数的 ...
- CREATE LANGUAGE - 定义一种新的过程语言
SYNOPSIS CREATE [ TRUSTED ] [ PROCEDURAL ] LANGUAGE name HANDLER call_handler [ VALIDATOR valfunctio ...
- width:100px; min-width:100% 解释:宽度大于100px 就是100% 小于100px 就是100像素
<div style="width:100px; background-color: aqua; min-width:100%">kkk</div>
- centos7配置静态IP步骤
centos7按照初始安装时候的developer类型一路装好,在vmware里已经设置为bridge模式,按理说是会自动按照DHCP联网成功的,结果却发现连网卡都没有激活,这里记录下. 1:我要把L ...
- Linux修改启动界面、分辨率
初识Linux 初识Linux(Centos 7.x),积累一些小技巧. 修改命令行界面的分辨率 # 备份配置文件 # 有些系统路径是/boot/grub...或者/boot/grub/menu.ls ...
- linux与linux之间共享目录
1.安装必要的包 nfs-utils rpcbind (nfs是基于sun公司的rpc通信实现的,所以要装rpcbind) 这2包,在服务端和客户端都需要安装,并启动服务. 启动 ...
- nohup 忽略所有挂断信号
1.nohup 用途:不挂断地运行命令. 语法:nohup Command [ Arg … ] [ & ] 无论是否将 nohup 命令的输出重定向到终端,输出都将附加到当前目录的 nohup ...
- Memcache 分布式存储 【一致性Hash】crc32
class memcacheHash { private $_node = array(); private $_nodeData = array(); private $_keyNode = 0; ...
- LeetCode(55)Jump Game
题目 Given an array of non-negative integers, you are initially positioned at the first index of the a ...
- npm 发包
前几天封装了公用的locaStorage组件,当然封装后需要发布npm官网,于是摸索了一番终于搞定了,总结下来希望对大家有所帮助 npm安装的package一般支持下面几大类: 本地包 url远程包 ...