Hdu 6171 Admiral【双向Bfs+字符串哈希】

来源:互联网 发布:西门子数控编程软件 编辑:程序博客网 时间:2024/05/21 06:31

Admiral

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 153428/153428 K (Java/Others)
Total Submission(s): 203    Accepted Submission(s): 52


Problem Description
Suppose that you are an admiral of a famous naval troop. Our naval forces have got 21 battleships. There are 6 types of battleships.
First, we have got one flagship in which the admiral must be and it is denoted by number 0. Others are denoted by number from 1 to 5, each of them has 2, 3, 4, 5, 6 ships of its kind. So, we have got 21 battleships in total and we must take a giant battle against the enemy. Hence, the correct strategy of how to arrange each type of battleships is very important to us.
The shape of the battlefield is like the picture that is shown below.
To simplify the problem, we consider all battleships have the same rectangular shape.

Fortunately, we have already known the optimal state of battleships.
As you can see, the battlefield consists of 6 rows. And we have 6 types of battleship, so the optimal state is that all the battleships denoted by number i are located at the i-th row. Hence, each type of battleship corresponds to different color.
You are given the initial state of battlefield as input. You can change the state of battlefield by changing the position of flagship with adjacent battleship.
Two battleships are considered adjacent if and only if they are not in the same row and share parts of their edges. For example, if we denote the cell which is at i-th row and j-th position from the left as (i,j), then the cell (2,1) is adjacent to the cells (1,0), (1,1), (3,1), (3,2).
Your task is to change the position of the battleships minimum times so as to reach the optimal state.
Note: All the coordinates are 0-base indexed.
 

Input
The first line of input contains an integer T (1 <= T <= 10), the number of test cases.
Each test case consists of 6 lines. The i-th line of each test case contains i integers, denoting the type of battleships at i-th row of battlefield, from left to right.
 

Output
For each test case, if you can’t reach the goal in no more than 20 moves, you must output “too difficult” in one line. Otherwise, you must output the answer in one line.
 

Sample Input
112 02 1 23 3 3 34 4 4 4 45 5 5 5 5 5
 

Sample Output
3

题目大意:


现在给出一个三角矩阵,我们如果0编号的在点(x,y)的话,可以和(x+1,y),(x-1,y),(x+1,y+1),(x-1,y-1)这些点进行交换。

我们每一次只能对0点和其他点进行交换。问最少步数,使得最终变成:

0

1 1

2 2 2

3 3 3 3

4 4 4 4 4

5 5 5 5 5 5


思路:


双向Bfs,我们正向预处理10步,然后根据读入的数据再走十步,当我们根据读入的数据走的时候,如果能够遇到之前预处理走到的点的话,那么ans=steppre+stepnow;

维护最小即可。


考虑到每个点的状态有6个,那么对应6^21的vis数组我们是开不出来的,所以我们需要字符串哈希一下判定一个点是否走过即可。


Ac代码:

#include<stdio.h>#include<iostream>#include<string.h>#include<map>#include<queue>using namespace std;map<unsigned long long int,int>ss,ss2;unsigned long long int h[45];struct node{    int x,y,step;    char temp[25];}now,nex;int a[8][8];int output;int poss[8][8];int fx[4]={1,-1,1,-1};int fy[4]={0,0,1,-1};int getpos(int x,int y){    return poss[x][y];}unsigned long long int Getval(char s[]){    int n=strlen(s);    unsigned long long int x=1;    for(int i=0;i<n;i++)    {        h[i]=h[i-1]+x*(s[i]-'0');        x=x*19260817;    }    return h[n-1];}void Bfs(){    queue<node>s;    now.x=1;    now.y=1;    now.step=0;    int cnt=0;    for(int i=1;i<=6;i++)    {        for(int j=1;j<=i;j++)        {            poss[i][j]=cnt;            now.temp[cnt++]=i+'0'-1;        }    }    now.temp[cnt]='\0';    s.push(now);    while(!s.empty())    {        now=s.front();s.pop();        ss[Getval(now.temp)]=now.step;        if(now.step>=10)break;        for(int i=0;i<4;i++)        {            nex=now;            nex.x+=fx[i];            nex.y+=fy[i];            nex.step++;            if(nex.x>=1&&nex.x<=6&&nex.y>=1&&nex.y<=nex.x)            {                int pos1=getpos(now.x,now.y);                int pos2=getpos(nex.x,nex.y);                swap(nex.temp[pos1],nex.temp[pos2]);                unsigned long long int nexval=Getval(nex.temp);                if(ss[nexval]==0)                {                    ss[nexval]=nex.step;                    s.push(nex);                }            }        }    }}void Bfs2(){    ss2.clear();    queue<node>s;    output=0x3f3f3f3f;    int cnt=0;    for(int i=1;i<=6;i++)    {        for(int j=1;j<=i;j++)        {            now.temp[cnt++]=a[i][j]+'0';            if(a[i][j]==0)            {                now.x=i;                now.y=j;                now.step=0;            }        }    }    now.temp[cnt]='\0';    s.push(now);    while(!s.empty())    {        now=s.front();s.pop();        ss2[Getval(now.temp)]=now.step;        if(now.step>=10)break;        for(int i=0;i<4;i++)        {            nex=now;            nex.x+=fx[i];            nex.y+=fy[i];            nex.step++;            if(nex.x>=1&&nex.x<=6&&nex.y>=1&&nex.y<=nex.x)            {                int pos1=getpos(now.x,now.y);                int pos2=getpos(nex.x,nex.y);                swap(nex.temp[pos1],nex.temp[pos2]);                unsigned long long int nexval=Getval(nex.temp);                if(ss2[nexval]==0)                {                    ss2[nexval]=nex.step;                    s.push(nex);                }                if(ss[nexval]>0)                {                    output=min(output,nex.step+ss[nexval]);                }            }        }    }    if(output==0x3f3f3f3f)printf("too difficult\n");    else printf("%d\n",output);}int main(){    ss.clear();    Bfs();    int t;    scanf("%d",&t);    while(t--)    {        int len=0;        char bb[55];        char aa[55];        for(int i=1;i<=6;i++)        {            for(int j=1;j<=i;j++)            {                bb[len]=i-1+'0';                scanf("%d",&a[i][j]);                aa[len++]=a[i][j]+'0';            }        }        bb[len]='\0';        aa[len]='\0';        if(strcmp(aa,bb)==0)        {            printf("0\n");            continue;        }        int anssss=ss[Getval(aa)];        if(anssss==0)        {            Bfs2();        }        else printf("%d\n",anssss);    }}












原创粉丝点击