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 ...
随机推荐
- 针对windowsserver 创建iis站点访问出错的解决方案(HTTP 错误 500.19 - Internal Server Error)
错误如下: 服务器错误 Internet信息服务 7.0 错误摘要HTTP 错误 500.19 - Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效. 详 ...
- leetcode_712. Minimum ASCII Delete Sum for Two Strings
https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/ 给定两个string s1,s2,从s1和s2中删除一些 ...
- vue 模板下只能有一个跟节点 根节点一定要是个div
<template> <div>简单说就是里面只能有一个跟的div button1.vue <template> <div> <Button> ...
- asp 读取 另外一个带参数的asp文件(服务器不支持!放弃吧!骚年!)
(服务器不支持!放弃吧!骚年!) 主要作用是为了分离数据库,灵感是这样的:收到json影响,把asp里的数据,用一个页面输出,然后用另外一个页面读取,这样就不用有数据库位置的烦恼了 代码 网上有很多, ...
- h5编写帧动画
var requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame; var ...
- Win10上 visual studio设置为本地IIS运行网站时 必须以管理员身份加载项目的解决方法
右键,选择“兼容性疑难解答”. 选择“疑难解答程序” 选择“该程序需要附加权限” 点击测试程序 点击下一步 选择 是,为此程序保存这些设置
- 10CSS高级滤镜
CSS高级滤镜 滤镜特效的应用 filter:滤镜属性(参数1,参数2,……) filter是滤镜属性选择符. 透明度——alpha opacity代表透明度等级,参数值从0到100,从完全透明 ...
- kafka可视化客户端工具(Kafka Tool)安装
参考:https://www.cnblogs.com/frankdeng/p/9452982.html
- snowflake机器标识自动绑定
首先附上snowflake源码 package com.yunyihenkey.common.idworker; /** * Twitter_Snowflake<br> * SnowFla ...
- formatDate() 格式化日期
function datefmt(milSec, format) { var oldTime = Number(milSec); //得到毫秒数 // 日期格式转换 var t = new Date( ...