HDU 3295 An interesting mobile game

IDA*水题。。。。

An interesting mobile game

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 281    Accepted Submission(s): 132


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
 



#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
using namespace std;
#define ll long long
#define ERR puts("=======here========");
#define prt(k) cout<<#k"="<<k<<" "
#include<algorithm>

int n,m;
int dx[]={-1,1,0,0};
int dy[]={0,0,1,-1};
bool ok(int x,int y) { return 0<=x&&x<n&&0<=y&&y<m; }
struct MAZE
{
    int a[6][6];
    bool vis[6][6];
    int color[6][6];
    int cnt;
    int H()
    {
        int c[7]; memset(c,0,sizeof c); for(int i=0;i<n;i++) for(int j=0;j<m;j++) c[a[i][j]]=1;
        int t=0; for(int i=1;i<=4;i++) t+=c[i]; return t;
    }
    MAZE()
    {
        memset(vis,0,sizeof vis); memset(a,0,sizeof a); memset(color,0,sizeof color);
    }
    bool isgoal()
    {
        for(int i=0;i<n;i++) for(int j=0;j<m;j++) if(a[i][j]>0) return 0;return 1;
    }
    void dfs(int x,int y)
    {
        vis[x][y]=1;  color[x][y]=cnt;
        for(int i=0;i<4;i++)
        {
            int xx=x+dx[i],yy=y+dy[i];
            if(ok(xx,yy)&&a[xx][yy]==a[x][y]&&!vis[xx][yy]) dfs(xx,yy);
        }
    }
    int h()
    {
        memset(vis,0,sizeof vis);
        memset(color,0,sizeof color);
        cnt=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(a[i][j]>0&&!vis[i][j])
                {
                    ++cnt;
                    dfs(i,j);
                }
            }
        }
        return cnt;
    }
    void in()
    {
        for(int i=0;i<n;i++) for(int j=0;j<m;j++) cin>>a[i][j];
    }
    void out()
    {
        for(int i=0;i<n;i++){ for(int j=0;j<m;j++)printf("%d ",a[i][j]); putchar(10); }  putchar(10);
    }
    void remove(int c)
    {
        for(int i=0;i<n;i++)for(int j=0;j<m;j++)if(color[i][j]==c)a[i][j]=0;
    }
    void eq(MAZE b)
    {
        for(int i=0;i<n;i++)for(int j=0;j<m;j++) a[i][j]=b.a[i][j];
    }
    void adjust()
    {
        for(int j=0;j<m;j++)
        {
            for(int i=n-1;i>=0;i--)
            {
                if(a[i][j]==0)
                {
                    int k;
                    for( k=i-1;k>=0;k--) if(a[k][j]>0) break;
                    if(k>=0)
                    {
                        a[i][j]=a[k][j];
                        a[k][j]=0;
                    }
                }
            }
        }
        for(int j=0;j<m;j++)
        {
            if(a[n-1][j]==0)
            {
                for(int x=0;x<n;x++)
                {
                    for(int y=j;y<m-1;y++)
                    {
                        a[x][y]=a[x][y+1];
                        a[x][y+1]=0;
                    }
                }
            }
        }
       // out();
    }
};
MAZE maze;
int limit;
int dfs(int dep,MAZE u)
{
    if(dep==limit)
    {
        return u.isgoal();
    }
    bool vis[7][7];
    int block=u.h();
   // printf("H(u)=%d dep=%d limit=%d\n",u.H(),dep,limit);
    if(u.H()+dep>limit) return 0;
   // u.out();
    for(int i=1;i<=block;i++)
    {
        MAZE v=u;;  //v.eq(u);
        v.remove(i);
        v.adjust();    memset(v.color,0,sizeof v.color);
        if(dfs(dep+1,v))
            return 1;
    }
    return 0;
}
int main()
{
    while(cin>>n>>m)
    {
        maze.in();
        limit=0;
        while(1)
        {
            if(dfs(0,maze)) break;
            limit++;
            if(limit>11) break;
        }
        printf("%d\n",limit);
    }
}
/**
3 3
1 3 1
1 2 1
1 2 1

*/


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值