[JZOJ3422] aqua dance

description

Aqua has been a lattice-shaped colorful carpet as a birthday gift, and more specifically, the color of the carpet on the grid can be changed with the stampede.

In order to please her idol Rainbow Cat, Aqua decided to dance a dance of light on the carpet to sell Moe ~ ~ ~

Grid on carpet N rows and N columns, each grid with a number between 0 to 5, representative of its color.

Aqua can choose the color of between 0 to 5, and then gently beating step, China Unicom block the upper left corner of the carpet in the grid where the grid will become all that color she chooses. Here the communication is defined as: a common edge of two cells, and the same color.

Because Aqua is cast dodge to dance, in order not to consume too many infuriating, she wanted to know how many steps in order at least to put all the grid colors become the same.


analysis

  • Positive solution \ (IDA * \) , a rise of knowledge

  • \ (IDDFS \) is iterative deepening search is specifically limit the depth of the search tree, listening to the slow but soon

  • \ (A * \) is the current step number plus the valuation function to search normally use Heap

  • First, every time the search of FIG before dyeing, \ (1 \) mark the current color block Unicom, \ (2 \) marker point on the outer boundary of the current block color Unicom, the remaining points labeled \ (0 \)

  • Evaluation function takes the current Unicom remaining block number of colors of different color, if it is \ (0 \), i.e. the whole has been dyed in one color in FIG.

  • If the current step number plus depth cost function is greater than the limit, it \ (return \)

  • Search enumerate a color stain, if Unicom block size has not changed, that this stain does not make sense

  • This problem can be solved, so we still have some more knowledge Pianmen


code

#pragma GCC optimize("O3")
#pragma G++ optimize("O3")
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define ll long long
#define reg register ll
#define fo(i,a,b) for (reg i=a;i<=b;++i)
#define fd(i,a,b) for (reg i=a;i>=b;--i)

using namespace std;

ll fx[4]={0,0,1,-1},fy[4]={1,-1,0,0};
ll a[10][10],b[10];
ll bz[10][10];
bool flag;
ll n,ans;

inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while (ch<'0' || '9'<ch){if (ch=='-')f=-1;ch=getchar();}
    while ('0'<=ch && ch<='9')x=x*10+ch-'0',ch=getchar();
    return x*f;
}
inline void color(ll x,ll y,ll z)
{
    bz[x][y]=1;
    fo(i,0,3)
    {
        ll xx=x+fx[i],yy=y+fy[i];
        if (xx<1 || xx>n || yy<1 || yy>n || bz[xx][yy]==1)continue;
        bz[xx][yy]=2;
        if (a[xx][yy]==z)color(xx,yy,z);
    }
}
inline ll H()
{
    ll tmp=0;
    memset(b,0,sizeof(b));
    fo(i,1,n)fo(j,1,n)if (bz[i][j]!=1 && !b[a[i][j]])b[a[i][j]]=1,++tmp;
    return tmp;
}
inline bool judge(ll x)
{
    ll tmp=0;
    fo(i,1,n)fo(j,1,n)if (a[i][j]==x && bz[i][j]==2){++tmp,color(i,j,x);}
    return tmp>0;
}
inline bool a_star(ll x)
{
    ll tmp=H(),t[10][10];
    if (tmp==0)return 1;
    else if (tmp+x>ans)return 0;
    fo(i,0,5)
    {
        memcpy(t,bz,sizeof(t));
        if (judge(i) && a_star(x+1))return 1;
        memcpy(bz,t,sizeof(bz));
    }
    return 0;
}
int main()
{
    freopen("T1.in","r",stdin);
    for (n=read();n!=0;n=read())
    {
        memset(bz,0,sizeof(bz)),flag=0;
        fo(i,1,n)fo(j,1,n)a[i][j]=read();
        color(1,1,a[1][1]);
        for (ans=0;ans<=n*n;++ans)if (a_star(0))break;
        printf("%lld\n",ans);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/horizonwd/p/11285019.html