HDU3295An interesting mobile game(BFS +模拟)

题意:消灭星星= =,

解法:由于每次会减少一些,故不需要判重,全部转移的状态入队,直接暴力模拟操作

#include<iostream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
#include<set>
#include<stack>
#define cl(a,b) memset(a,b,sizeof(a));
#define LL long long
#define P pair<int,int>
#define X first
#define Y second
#define pb push_back
#define out(x) cout<<x<<endl;
using namespace std;
const int maxn=6;
const int inf=9999999;
const int mod=100007;

int n,m;
int a[maxn][maxn];

LL encode(int tu[][6]){///对图编码
    LL code=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            code+=tu[i][j]*(1LL<<(i*6+j));
        }
    }
    return code;
}
int dir[][2]={1,0,0,1,-1,0,0,-1};
bool vis[maxn][maxn];
int dfs_a[maxn][maxn];
void dfs(int x,int y,int color){///查找color连通块并相除
    if(vis[x][y]||dfs_a[x][y]!=color)return ;
    vis[x][y]=true;
    dfs_a[x][y]=0;
    for(int i=0;i<4;i++){
        int xx=x+dir[i][0];
        int yy=y+dir[i][1];
        if(xx<0||xx>=n||yy<0||yy>=m)continue;
        dfs(xx,yy,color);
    }
}

int work_a[maxn][maxn];
void work(){///查找空白行并移动覆盖
    for(int t=0;t<10;t++)
    for(int i=1;i<n;i++){///行检查空行
        for(int j=0;j<m;j++){
            if(work_a[i][j]==0&&work_a[i-1][j]!=0){
                work_a[i][j]=work_a[i-1][j];
                work_a[i-1][j]=0;
            }
        }
    }
    for(int t=0;t<10;t++)
    for(int j=0;j<m;j++){///查找空白列
        bool ok=true;
        for(int i=0;i<n;i++){
            if(work_a[i][j]!=0){
                ok=false;
                break;
            }
        }
        if(ok){
            for(int col=j;col<m-1;col++){///左移空白列
                for(int i=0;i<n;i++){
                    work_a[i][col]=work_a[i][col+1];
                    work_a[i][col+1]=0;
                }
            }
        }
    }
}
struct node{
    int a[maxn][maxn];
    int step;
    node(){
        step=0;
        cl(a,0);
    }
};

int bfs(){///广搜最短路
    queue<node> q;
    node tmp;
    memcpy(tmp.a,a,sizeof(a));
    tmp.step=0;
    q.push(tmp);

    while(!q.empty()){

        node s=q.front();q.pop();
        if(encode(s.a)==0)return s.step;
        for(int k=1;k<=4;k++){
            node x=s;
            cl(vis,0);
            for(int i=0;i<n;i++){
                for(int j=0;j<m;j++)if(s.a[i][j]==k&&!vis[i][j]){
                    memcpy(dfs_a,s.a,sizeof(s.a));
                    dfs(i,j,k);
                    memcpy(work_a,dfs_a,sizeof(dfs_a));
                    work();
                    LL tt=encode(work_a);
                    if(tt==0LL)return s.step+1;
                    memcpy(x.a,work_a,sizeof(work_a));
                    x.step=s.step+1;
                    q.push(x);
                }
            }
        }
    }
}

int main(){
    while(~scanf("%d%d",&n,&m)){
        cl(a,0);
        cl(dfs_a,0);
        cl(work_a,0);
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                scanf("%d",&a[i][j]);
            }
        }
        printf("%d\n",bfs());
    }

    return 0;
}

/*
4 5
1 0 0 2 3
2 0 0 4 1
3 0 0 4 3
4 0 0 1 2

*/

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-11 21:58:16

HDU3295An interesting mobile game(BFS +模拟)的相关文章

HDU-3295-An interesting mobile game(BFS)

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 pictur

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 HDU. Original ID: 329564-bit integer IO format: %I64d      Java class name: Main XQ,one of the three Sailormoon girls,is usually playing mobile games o

Hdu 5336 XYZ and Drops (bfs 模拟)

题目链接: Hdu 5336 XYZ and Drops 题目描述: 有一个n*m的格子矩阵,在一些小格子里面可能会有一些水珠,每个小水珠都有一个size.现在呢,游戏开始咯,在一个指定的空的小格子里面有一个将要向四周爆裂的水珠,在下一面分别向上,下,左,右四个方向发射一个小水滴,(小水滴与水珠同,小水滴没有size),当小水滴走向一个格子,这个格子如果是空或者有其他的小水滴同时走到这个格子的情况下,对小水滴的运动轨迹是不影响的.但是遇到水珠的话,小水滴就会被吸收,水珠每次吸收一个小水滴size

BFS+模拟 ZOJ 3865 Superbot

题目传送门 1 /* 2 BFS+模拟:dp[i][j][p] 表示走到i,j,方向为p的步数为多少: 3 BFS分4种情况入队,最后在终点4个方向寻找最小值:) 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <algorithm> 8 #include <cstring> 9 #include <string> 10 #include <queue> 11 usi

CUGBACM_Summer_Tranning3 2013长沙现场赛(二分+bfs模拟+DP+几何)

A题:二分 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4791 用lower_bound可以轻松解决,不过比赛的时候逗逼了. 刚开始没有预处理,所以队友给出一组数据的时候没通过,然后一时紧张又想不出什么好的解决办法,所以就没再继续敲代码.实在有点可惜了. #include<iostream> #include<cstring> #include<cstdio> #include<cmath> #includ

【HDOJ】3295 An interesting mobile game

其实就是一道搜索模拟题.因为数据量小,用char就够了. 1 /* 3295 */ 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <cstdlib> 6 #include <queue> 7 using namespace std; 8 9 typedef struct { 10 char m[6][6]; 11 char x, y; 12 ch

D - Interesting Calculator 【BFS+优先队列】

There is an interesting calculator. It has 3 rows of buttons. Row 1: button 0, 1, 2, 3, ..., 9. Pressing each button appends that digit to the end of the display. Row 2: button +0, +1, +2, +3, ..., +9. Pressing each button adds that digit to the disp

CSU 1336: Interesting Calculator(BFS啊 湖南省第九届大学生计算机程序设计竞赛)

题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1336 1336: Interesting Calculator Description There is an interesting calculator. It has 3 rows of buttons. Row 1: button 0, 1, 2, 3, ..., 9. Pressing each button appends that digit to the end of

HDU 5336 BFS+模拟

模拟十滴水游戏 r*c矩阵中,共有N个大水滴,求T秒后这N个水滴的状态 在0秒时在s_x,s_y位置有个水滴爆炸,生成向四周移动的小水滴,每个大水滴>4会爆炸,生成向四周移动的小水滴 把所有小水滴入队列,进行BFS即可,注意处理多个小水滴同时到达同一个大水滴的情况 #include "stdio.h" #include "string.h" #include "queue" using namespace std; const int di