2017多校联合第十场/HDU 6171 Admiral 双向bfs + 哈希

Admiral

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


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
  
  
1 1 2 0 2 1 2 3 3 3 3 4 4 4 4 4 5 5 5 5 5 5
 

Sample Output
  
  
3
 

Source
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   6181  6180  6179  6178  6177 
 

Statistic |  Submit |  Discuss |  Note

题意:给你一个高度为6的数塔,问最少多少步能走成如下状态。
每次只能将值为0的点与其左上方,上方,下方,右下方四个方向的一个点进行交换。 

1 1 
2 2 2 
3 3 3 3 
4 4 4 4 4 
5 5 5 5 5 5

大于20步可以直接输出“too difficult”

题解:一共21个点,每个点有6个状态,一共6^21个状态,所以状态压缩数组是开不下的,所以用hash,long long可以存的下。已知初态和终态,所以要双向bfs。先预处理终态,预处理10步,然后看初态十步只能能否和预处理的结果一样,可以用map维护,一样则输出step1+step2,否则输出too difficult。

代码:

typedef unsigned long long ull;
struct node
{
    int val[6][6];
    int r,c;//0的位置
    bool tp;
    int step;
};
ull Hash(node &t)
{
    ull tmp=0;
    for(int i=0;i<6;i++)
    {
        for(int j=0;j<=i;j++)
            tmp=tmp*6+t.val[i][j];
    }
    return tmp;
}
map<ull,int>book[2];
queue<node>q;
int dx[]={-1,-1,1,1};
int dy[]={-1,0,0,1};
int bfs(node &s,node &t)
{
    while(!q.empty()) q.pop();
    book[0].clear();
    book[1].clear();
    s.tp=0;
    t.tp=1;
    s.step=t.step=0;
    book[s.tp][Hash(s)]=0;
    book[t.tp][Hash(t)]=0;
    q.push(s);
    q.push(t);
    while(!q.empty())
    {
        node x,a=q.front();
        q.pop();
        ull tmp=Hash(a);
        if(book[!a.tp].count(tmp))
            if(book[!a.tp][tmp]+a.step<=20)
                return book[!a.tp][tmp]+a.step;
            else continue;
        if(a.step>=10) continue;
        for(int i=0;i<4;i++)
        {
            x=a;
            x.r+=dx[i];
            x.c+=dy[i];
            if(x.r>=6 || x.c>x.r || x.c<0 || x.r<0) continue;
            swap(x.val[x.r][x.c],x.val[a.r][a.c]);
            tmp=Hash(x);
            if(book[x.tp].count(tmp)) continue;
            book[x.tp][tmp]=++x.step;
            q.push(x);
        }
    }
    return -1;
}
int main()
{
    ios::sync_with_stdio(false);
    int T;
    cin>>T;
    node s,t;
    while(T--)
    {
        for(int i=0;i<6;i++)
            for(int j=0;j<=i;j++)
            {
                cin>>s.val[i][j];
                if(s.val[i][j]==0) s.r=i,s.c=j;
                t.val[i][j]=i;
            }
        t.r=t.c=0;
        int ans=bfs(s,t);
        ans==-1?cout<<"too difficult"<<endl:cout<<ans<<endl;
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值