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

224 篇文章 2 订阅

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
  
  
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编号的在点(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);
    }

}












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
字符串哈希滑动窗口是一种用于处理字符串的算法。它主要用于在给定的字符串中找到满足特定条件的子串。 在字符串哈希滑动窗口算法中,我们首先计算原始字符串哈希值。然后,我们使用一个滑动窗口来遍历字符串,每次滑动一个固定长度的窗口。我们可以通过比较每个窗口内的子串的哈希值来判断是否满足条件。 具体而言,我们可以使用BKDRHash等哈希函数来计算字符串哈希值。然后,我们枚举每个可能的起点,并使用滑动窗口来计算窗口内的子串的哈希值。通过比较窗口内的子串的哈希值,我们可以判断是否满足条件。 对于滑动窗口的移动,如果窗口内的子串满足条件,我们可以继续将窗口往右移动一个固定的长度。如果窗口内的子串不满足条件,我们将窗口的右边界移到最右端,并依次比较新窗口内的子串的哈希值。 综上所述,字符串哈希滑动窗口算法是通过计算字符串哈希值,并使用滑动窗口来遍历字符串,以找到满足特定条件的子串。这个算法可以高效地处理字符串,并且能够应用于各种字符串相关的问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [String (字符串哈希+滑动窗口)](https://blog.csdn.net/weixin_43872264/article/details/107571742)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [【字符串hash+滑动窗口】String HDU - 4821](https://blog.csdn.net/qq_45599865/article/details/111143633)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值