码迷,mamicode.com
首页 > 其他好文 > 详细

poj 2046 Gap(bfs+hash)

时间:2015-08-30 19:28:00      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

Description

Lets 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.

 

Input

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. 

 

Output

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".

 

Sample Input

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

 

Sample Output

0
33
60
-1

 

Source

 
这题的关键在用hash来保存状态,其他的是bfs基础了。。。
 
技术分享
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<queue>
  5 #include<algorithm>
  6 #include<stdlib.h>
  7 using namespace std;
  8 #define M 1000007
  9 #define ll long long
 10 ll aimNum;
 11 ll hash[M];
 12 struct Node{
 13     ll x[5],y[5];//存空格的横纵坐标
 14     ll mp[6][9];//存整张地图
 15     long long time;//存时间
 16 }tmp;
 17 ll flag;
 18 ll aim[6][9]={
 19     11,12,13,14,15,16,17,0,
 20     21,22,23,24,25,26,27,0,
 21     31,32,33,34,35,36,37,0,
 22     41,42,43,44,45,46,47,0
 23 };
 24 ll base[33]={1};
 25 
 26 bool inserNum(ll ans){//hash的插入,看看是否跟之前的状态相同,其实跟vis数组标记一个意思
 27     ll val=ans%M;
 28     while(hash[val]!=-1 && hash[val]!=ans){
 29         val=(val+10)%M;
 30     }
 31     if(hash[val]==-1){
 32         hash[val]=ans;
 33         return true;//可以插入返回true
 34     }
 35     return false;//否则返回false
 36 }
 37 
 38 bool work(Node cnt){
 39     ll ans=0;
 40     for(ll i=0;i<4;i++){
 41         for(ll j=0;j<8;j++){
 42             ans=ans+cnt.mp[i][j]*base[i*8+j];//ans为整张图的hash值
 43         }
 44     }
 45     if(ans==aimNum){
 46         flag=1;
 47     }
 48     if(inserNum(ans))
 49         return true;
 50     return false;
 51 }
 52 
 53 ll bfs(){
 54     queue<Node>q;
 55     q.push(tmp);
 56     Node t1,t2;
 57     while(!q.empty()){
 58         t1=q.front();
 59         q.pop();
 60 
 61         for(ll k=0;k<4;k++){//4个空格依次遍历
 62             t2=t1;
 63             ll tx=t2.x[k];
 64             ll ty=t2.y[k];
 65             for(ll i=0;i<4;i++){//遍历整张图,寻找符合的数
 66                 for(ll j=0;j<8;j++){
 67                     if(t2.mp[i][j]==0) continue;//如果要调换的还是空格,则不行
 68                     if(t2.mp[i][j]!=t2.mp[tx][ty-1]+1) continue;//需要填入的数为前一个+1
 69 
 70 
 71                     swap(t2.mp[i][j],t2.mp[tx][ty]);
 72                     if(work(t2)){//判断是否可以继续往下走
 73                         t2.time=t1.time+1;
 74                         t2.x[k]=i;//将新的空格的横纵坐标保存下来
 75                         t2.y[k]=j;
 76                         q.push(t2);
 77                         if(flag)
 78                             return t2.time;
 79                     }
 80 
 81                 }
 82             }
 83         }
 84     }
 85     return -1;
 86 }
 87 int main()
 88 {
 89 
 90     for(ll i=1;i<33;i++){
 91         base[i]=base[i-1]*2;
 92     }
 93     aimNum=(ll)98430874871;//aimNum是通过事先计算得出的
 94 
 95     int t;
 96     scanf("%d",&t);
 97 
 98     while(t--){
 99 
100         memset(hash,-1,sizeof(hash));
101 
102         tmp.mp[0][0]=tmp.mp[1][0]=tmp.mp[2][0]=tmp.mp[3][0]=0;
103 
104         int k=0;
105         for(int i=0;i<4;i++){
106             for(int j=1;j<8;j++){
107                 scanf("%I64d",&tmp.mp[i][j]);
108 
109                 if(tmp.mp[i][j]==11) {
110                     swap(tmp.mp[i][j],tmp.mp[0][0]);
111                     tmp.x[k]=i;
112                     tmp.y[k++]=j;
113                 }
114                 if(tmp.mp[i][j]==21) {
115                     swap(tmp.mp[i][j],tmp.mp[1][0]);
116                     tmp.x[k]=i;
117                     tmp.y[k++]=j;
118                 }
119                 if(tmp.mp[i][j]==31) {
120                     swap(tmp.mp[i][j],tmp.mp[2][0]);
121                     tmp.x[k]=i;
122                     tmp.y[k++]=j;
123                 }
124                 if(tmp.mp[i][j]==41) {
125                     swap(tmp.mp[i][j],tmp.mp[3][0]);
126                     tmp.x[k]=i;
127                     tmp.y[k++]=j;
128                 }
129             }
130         }
131 
132         tmp.time=0;//时间初始化为0
133         flag=0;
134         work(tmp);//先判断一遍是否可以不用调换就可以达到目的图
135         if(flag){
136             printf("0\n");
137         }else{
138             printf("%I64d\n",bfs());
139         }
140     }
141     return 0;
142 }
View Code

 

 

poj 2046 Gap(bfs+hash)

标签:

原文地址:http://www.cnblogs.com/UniqueColor/p/4771258.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!