首页 > ACM题库 > HDU-杭电 > HDU 1067 Gap-BFS-[解题报告] C++
2013
11-26

HDU 1067 Gap-BFS-[解题报告] C++

Gap

问题描述 :

Let’s play a card game called Gap.
You have 28 cards labeled with two-digit numbers. The first digit (from 1 to 4) represents the suit of the card, and the second digit (from 1 to 7) represents the value of the card.

First, you shu2e the cards and lay them face up on the table in four rows of seven cards, leaving a space of one card at the extreme left of each row. The following shows an example of initial layout.

Next, you remove all cards of value 1, and put them in the open space at the left end of the rows: "11" to the top row, "21" to the next, and so on.

Now you have 28 cards and four spaces, called gaps, in four rows and eight columns. You start moving cards from this layout.

At each move, you choose one of the four gaps and fill it with the successor of the left neighbor of the gap. The successor of a card is the next card in the same suit, when it exists. For instance the successor of "42" is "43", and "27" has no successor.

In the above layout, you can move "43" to the gap at the right of "42", or "36" to the gap at the right of "35". If you move "43", a new gap is generated to the right of "16". You cannot move any card to the right of a card of value 7, nor to the right of a gap.

The goal of the game is, by choosing clever moves, to make four ascending sequences of the same suit, as follows.

Your task is to find the minimum number of moves to reach the goal layout.

输入:

The input starts with a line containing the number of initial layouts that follow.

Each layout consists of five lines – a blank line and four lines which represent initial layouts of four rows. Each row has seven two-digit numbers which correspond to the cards.

输出:

For each initial layout, produce a line with the minimum number of moves to reach the goal layout. Note that this number should not include the initial four moves of the cards of value 1. If there is no move sequence from the initial layout to the goal layout, produce "-1".

样例输入:

4

12 13 14 15 16 17 21
22 23 24 25 26 27 31
32 33 34 35 36 37 41
42 43 44 45 46 47 11

26 31 13 44 21 24 42
17 45 23 25 41 36 11
46 34 14 12 37 32 47
16 43 27 35 22 33 15

17 12 16 13 15 14 11
27 22 26 23 25 24 21
37 32 36 33 35 34 31
47 42 46 43 45 44 41

27 14 22 35 32 46 33
13 17 36 24 44 21 15
43 16 45 47 23 11 26
25 37 41 34 42 12 31

样例输出:

0
33
60
-1

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1067

思路:学会了手写Hash。。。orz….纪念一下。

#include<iostream>
 #include<cstdio>
 #include<cstring>
 #include<cmath>
 #include<queue>
 using namespace std;
 #define MAXN 1000007
 typedef long long ll;
 ll Hash[MAXN];
 
 struct Node{
     int map[4][8],step;
     bool operator == (const Node &p) const {
         for(int i=0;i<4;i++)
             for(int j=0;j<8;j++)
                 if(map[i][j]!=p.map[i][j])
                     return false;
         return true;
     }
     //手写hash
     ll HashValue(){
         ll value=0;
         for(int i=0;i<4;i++)
             for(int j=0;j<8;j++)
                 value+=(value<<ll(1))+(ll)map[i][j];
         return value;
     }
 };
 
 Node Start,End;
 
 void Initaite(){
     memset(Hash,-1,sizeof(Hash));
     for(int i=0;i<4;i++){
         Start.map[i][0]=0;
         for(int j=1;j<8;j++){
             scanf("%d",&Start.map[i][j]);
         }
     }
     Start.step=0;
 }
 
 //最后的结果
 void GetEnd(){
     for(int i=0;i<4;i++){
         End.map[i][7]=0;
         for(int j=0;j<7;j++){
             End.map[i][j]=(i+1)*10+(j+1);
         }
     }
 }
 
 //取得value的hash值+hash判重
 bool HashInsert(ll value){
     int v=value%MAXN;
     while(Hash[v]!=-1&&Hash[v]!=value){
         v+=10;
         v%=MAXN;
     }
     if(Hash[v]==-1){
         Hash[v]=value;
         return true;
     }
     return false;
 }
 
 void bfs(){
     queue<Node>Q;
     Node p,q;
     Q.push(Start);
     HashInsert(Start.HashValue());
     while(!Q.empty()){
         p=Q.front();
         Q.pop();
         for(int i=0;i<4;i++){
             for(int j=0;j<8;j++){
                 if(!p.map[i][j]){
                     q=p;
                     q.step++;
                     int value=p.map[i][j-1]+1;//找比map[i][j-1]大1的数
                     if(value==1||value%10==8)continue;//0或者value为7的不能移动
                     int x,y,flag=true;
                     for(int k=0;k<4&&flag;k++){
                         for(int l=1;l<8&&flag;l++){
                             if(p.map[k][l]==value){
                                 x=k,y=l;
                                 flag=false;
                             }
                         }
                     }
                     if(!flag){
                         swap(q.map[i][j],q.map[x][y]);
                         ll value=q.HashValue();
                         //hash判重
                         if(HashInsert(value)){
                             if(q==End){
                                 printf("%d\n",q.step);
                                 return ;
                             }
                             Q.push(q);
                         }
                     }
                 }
             }
         }
     }
     puts("-1");
 }
 
 void Solve(){
     int k=0;
     //将11,21,31,41这四个数移到第0列
     for(int i=0;i<4;i++){
         for(int j=1;j<8;j++){
             if(Start.map[i][j]==(k+1)*10+1){
                 swap(Start.map[i][j],Start.map[k][0]);
                 k++,i=0,j=0;
             }
         }
     }
     if(Start==End){
         puts("0");//前四步不记录总步数
         return ;
     }
     bfs();
 }
 
 int main(){
     int _case;
     scanf("%d",&_case);
     GetEnd();
     while(_case--){
         Initaite();
         Solve();
     }
     return 0;
 }

 


,
  1. 换句话说,A[k/2-1]不可能大于两数组合并之后的第k小值,所以我们可以将其抛弃。
    应该是,不可能小于合并后的第K小值吧

  2. 博主您好,这是一个内容十分优秀的博客,而且界面也非常漂亮。但是为什么博客的响应速度这么慢,虽然博客的主机在国外,但是我开启VPN还是经常响应很久,再者打开某些页面经常会出现数据库连接出错的提示