hdu 6171 Admiral

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
一般来说,双向宽搜都是用map来记,有例外的情况就像hdu的棋盘那道题目,但是那个也是可以用一个数组记下,
如果你用双向宽搜t了,要么是不是用这个做,要么是想法不够好。
这道题就不能用id来判断,我开了一个二维的map来记他的步数.

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<queue>
#include<map>
using namespace std;
const int mod=1e9+7;
map<int,int>map1[2];
struct node
{
    int a[10][10],step,x,y,flag,id;
}sta,fin;
int xmov[]={-1,-1,1,1};
int ymov[]={-1,0,0,1};
int Hash(node a)
{
    int ans=0;
    for(int i=0;i<6;i++)
    {
        for(int j=0;j<6;j++)
        {
            ans=(ans*17+a.a[i][j])%mod;
        }
    }
    return ans;
}
int bfs()
{
    queue<node>Q;
    map1[0].clear();
    map1[1].clear();
    Q.push(sta);
    Q.push(fin);
    map1[0][Hash(sta)]=1;
    int ans=Hash(fin);
    if(map1[0][ans]==1)
        return 0;
    map1[1][ans]=2;
    while(!Q.empty())
    {
        node v,ne;
        v=Q.front();
        Q.pop();
        for(int i=0;i<4;i++)
        {
            ne=v;
            ne.x=v.x+xmov[i];
            ne.y=v.y+ymov[i];
            if(ne.x<0||ne.x>=6||ne.y<0||ne.y>=6)
                continue;
            swap(ne.a[ne.x][ne.y],ne.a[v.x][v.y]);
            int temp=Hash(ne);
            ne.step+=1;
            if(map1[ne.flag][temp])
                continue;
            else if(map1[ne.flag^1][temp]!=0)
                return map1[ne.flag^1][temp]+ne.step;
            map1[ne.flag][temp]=ne.step;
            if(ne.step<10)
                Q.push(ne);
        }
    }
    return -1;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        for(int i=0;i<6;i++)
        {
            for(int j=0;j<=i;j++)
            {
                scanf("%d",&sta.a[i][j]);
                if(sta.a[i][j]==0)
                    sta.x=i,sta.y=j;
                fin.a[i][j]=i;
            }
        }
        fin.x=fin.y=0;
        sta.step=fin.step=0;
        sta.flag=0,fin.flag=1;
        sta.id=1,fin.id=2;
        int ans=bfs();
        if(ans==-1)
            printf("too difficult\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值