HDU-3278 Puzzle (BFS+压缩+打表)

题录链接
Problem Description
One day, Resty gets a very interesting puzzle. Eve said that she will make a cake for Resty if he solved this puzzle, so Resty asks you to help him to solve the puzzle as soon as possible.
As the figure, the puzzle is a rectangle divided into 4 * 6 = 24 squares. Each cell has a color of white / black/ grey. There are exactly 8 cells of each color. Our purpose is to make the middle 8 cells(which are not on the edge) have the same color by minimal steps.
在这里插入图片描述
Each step, you could choose a row or a column to shift it for 1 bit. As following:

Now, given the puzzle, you should help Resty find the minimal steps he must use to solve it.

Input
The first line is a number T, the number of test case.
Each case contains 4 lines, each line contains a 6-length string, describe a puzzle. B for black color, W for white and G for grey.

Output
For each test, output on line in the Format “Case ID: ANS”, ANS should be the minimal number of steps to solve the puzzle.

Sample Input
3
GWGGWW BBBBBW GBBBGW WGGGWW
GWGGWW BWBBBB GBBBGW WGGGWW
WWWWWW BGGGGB WGGGGW BBBBBB

Sample Output
Case 1: 2
Case 2: 3
Case 3: 0

题意:给一个3*8长方形,一共有三种颜色,每种颜色有八个小方块。可以将任意一行左右移动或者是将任意一列上下移动,提问怎么以最小的步数使中间的八个方块是同一种颜色。
在这里插入图片描述

这道题难想到的我觉得有以下几点:

  1. 如何存图?显然,进行BFS的时候我们需要纪录每个状态是否已经出现过。如果我们构建的存图数组是vis[4][6][ 3 ^ 24],这个数组是肯定会超内存的!我们可以用三进制的原理来构建一个函数,将这个图转化为数字。0、1、2分别代表G、W、B。不同的位数,代表不同的格子。比如三进制下的210210(也就是十进制下的588)就可以代表BWGBWG。那么,我们就将存图的数组优化成了vis[3 ^ 24].但是这样显然不是最优的。因为最后的结果是中间八个方块是同样的颜色,换言之,最后的状态分为中间八格的颜色另外的颜色,而且另外的颜色是如何分布的对结果没有影响。因此,可以把除了中间八格的颜色都看作是一种颜色。所以最后的颜色只有两种颜色。因此可以只使用二进制来压缩这个图,即vis[2 ^ 24];
  2. 打表。这道题的末状态都是一样的(中间八格颜色相同)。因此,我们可以逆向打表,求出末状态到所有的状态所需要的最小步数。存步数要用char构建数组不然就会Memory Limit Exceeded

AC代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define ll long long
#define MT(a,b) memset(a,b,sizeof(a))
const int maxn=1E5+5;
const int ONF=-0x3f3f3f3f;
const int INF=0x3f3f3f3f;
struct node{
    bool mp[6][8];
}head,tmp;
char MP[5][7];
int Mp[5][7];
char step[17000000];

int get_hush(node a){
    int hush=0;
    for (int i=1;i<=4;i++){
        for (int j=1;j<=6;j++){
            hush<<=1;
            if (a.mp[i][j]== true) hush+=1;
        }
    }
    return hush;
}

void get_mp(int hush){
    for (int i=4;i>=1;i--){
        for (int j=6;j>=1;j--){
            tmp.mp[i][j]=hush&1;
            hush>>=1;
        }
    }
}

void bfs (){
    int hush_tmp;
    MT(step, -1);
    queue<int>q;
    for (int i=1;i<=4;i++){
        for (int j=1;j<=6;j++){
            if (i>=2&&i<=3&&j>=2&&j<=5) tmp.mp[i][j]= true;
            else tmp.mp[i][j]=false;
        }
    }
    int hush=get_hush(tmp);
    step[hush]=0;
    q.push(hush);
    while (!q.empty()){
        hush=q.front();
        q.pop();
        get_mp(hush);
        head=tmp;
        for (int i=1;i<=4;i++){
            tmp=head;
            for (int j=1;j<=6;j++){
                tmp.mp[i][j]=head.mp[i][j-1];
                if (j==1) tmp.mp[i][j]=head.mp[i][6];
            }
            hush_tmp=get_hush(tmp);
            if (step[hush_tmp]== -1){
                step[hush_tmp]= step[hush]+1;
                q.push(hush_tmp);
            }
            tmp=head;
            for (int j=1;j<=6;j++){
                tmp.mp[i][j]=head.mp[i][j+1];
                if (j==6) tmp.mp[i][j]=head.mp[i][1];
            }
            hush_tmp=get_hush(tmp);
            if (step[hush_tmp]== -1){
                step[hush_tmp]= step[hush]+1;
                q.push(hush_tmp);
            }
        }
        for (int j=1;j<=6;j++){
            tmp=head;
            for (int i=1;i<=4;i++){
                tmp.mp[i][j]=head.mp[i+1][j];
                if (i==4) tmp.mp[i][j]=head.mp[1][j];
            }
            hush_tmp=get_hush(tmp);
            if (step[hush_tmp]== -1){
                step[hush_tmp]= step[hush]+1;
                q.push(hush_tmp);
            }
            tmp=head;
            for (int i=1;i<=4;i++){
                tmp.mp[i][j]=head.mp[i-1][j];
                if (i==1) tmp.mp[i][j]=head.mp[4][j];
            }
            hush_tmp=get_hush(tmp);
            if (step[hush_tmp]== -1){
                step[hush_tmp]= step[hush]+1;
                q.push(hush_tmp);
            }
        }
    }
    return ;
}
int main (){
    int t,hush_W,hush_G,hush_B;
    bfs();
    scanf("%d",&t);
    for (int turn=1;turn<=t;turn++){
        for (int i=1;i<=4;i++){
            scanf("%s",MP[i]);
        }
        node W,G,B;
        for (int i=1;i<=4;i++){
            for (int j=0;j<=5;j++){
                if (MP[i][j]=='G'){
                    G.mp[i][j+1]= true;
                } else G.mp[i][j+1]= false;
                if (MP[i][j]=='W'){
                    W.mp[i][j+1]= true;
                } else W.mp[i][j+1]= false;
                if (MP[i][j]=='B'){
                    B.mp[i][j+1]= true;
                } else B.mp[i][j+1]= false;
            }
        }
        hush_W=get_hush(W);
        hush_B=get_hush(B);
        hush_G=get_hush(G);
        int ans=min(step[hush_W],min(step[hush_B],step[hush_G]));
        printf("Case %d: %d\n",turn,ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值