欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【多校训练】hdu 6171 Admiral 双向bfs+hash

程序员文章站 2022-06-03 23:46:38
...

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.
【多校训练】hdu 6171 Admiral 双向bfs+hash

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
 

题意:

给你一个塔,要你弄成第一行全是0的,第二行全是1,第三行全是2,以此类推,求最小步数,每一步只能把0移动到上方、左上方、下方、右下方

思路:

双向bfs暴搜,hash状态。


//
//  main.cpp
//  1001
//
//  Created by zc on 2017/8/31.
//  Copyright © 2017年 zc. All rights reserved.
//

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<cmath>
#include<algorithm>
#include<map>
#include<vector>
#define ll long long 
using namespace std;

map<string,int>mp1,mp2;
char c[2];
struct node
{
    string s;
    int p,step,pos;
};
queue<node>q;
vector<int>r[22];
int main(int argc, const char * argv[]) {
    int T;
    scanf("%d",&T);
    int sum=0;
    for(int i=1;i<6;i++)
        for(int j=1;j<=i;j++)
        {
            sum++;
            r[sum].push_back(sum+i);
            r[sum].push_back(sum+i+1);
            r[sum+i].push_back(sum);
            r[sum+i+1].push_back(sum);
        }
    while(T--)
    {
        string s="";
        int pos=0;
        for(int i=0;i<21;i++)
        {
            scanf("%s",c);
            if(c[0]=='0')   pos=i+1;
            s=s+c[0];
        }
        mp1.clear();mp2.clear();
        while(!q.empty())   q.pop();
        node t;
        t.s=s;t.p=0;t.step=1;t.pos=pos;
        q.push(t);
        s="";
        for(char i='0';i<'6';i++)
            for(char j='0';j<=i;j++)
                s+=i;
        t.s=s;t.p=1;t.pos=1;
        q.push(t);
        int ans=-1;
        while(!q.empty())
        {
            node t=q.front();//cout<<t.step<<endl;
            q.pop();
            if((t.p==0&&mp1[t.s]>0)||(t.p==1&&mp2[t.s]>0))  continue;
            if(t.step>11)   break;
            if(t.p==0&&mp2[t.s]>0)
            {
                ans=t.step+mp2[t.s]-2;
                break;
            }
            if(t.p==1&&mp1[t.s]>0)
            {
                ans=t.step+mp1[t.s]-2;
                break;
            }
            if(t.p==0)  mp1[t.s]=t.step;
            else    mp2[t.s]=t.step;
            for(int i=0;i<r[t.pos].size();i++)
            {
                string st=t.s;
                swap(st[t.pos-1],st[r[t.pos][i]-1]);
                if((t.p==0&&mp1[st]>0)||(t.p==1&&mp2[st]>0))    continue;
                node tp=t;
                tp.s=st;tp.pos=r[t.pos][i];tp.step++;
                q.push(tp);
            }
        }
        if(ans==-1||ans>20) printf("too difficult\n");
        else    printf("%d\n",ans);
    }
}