青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

infinity

  C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
  36 隨筆 :: 0 文章 :: 25 評論 :: 0 Trackbacks
http://acm.pku.edu.cn/JudgeOnline/problem?id=1077

Description

The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've seen it. It is constructed with 15 sliding tiles, each with a number from 1 to 15 on it, and all packed into a 4 by 4 frame with one tile missing. Let's call the missing tile 'x'; the object of the puzzle is to arrange the tiles so that they are ordered as:
 1  2  3  4

5 6 7 8
9 10 11 12
13 14 15 x

where the only legal operation is to exchange 'x' with one of the tiles with which it shares an edge. As an example, the following sequence of moves solves a slightly scrambled puzzle:
 1  2  3  4    1  2  3  4    1  2  3  4    1  2  3  4

5 6 7 8 5 6 7 8 5 6 7 8 5 6 7 8
9 x 10 12 9 10 x 12 9 10 11 12 9 10 11 12
13 14 11 15 13 14 11 15 13 14 x 15 13 14 15 x
r-> d-> r->

The letters in the previous row indicate which neighbor of the 'x' tile is swapped with the 'x' tile at each step; legal values are 'r','l','u' and 'd', for right, left, up, and down, respectively.

Not all puzzles can be solved; in 1870, a man named Sam Loyd was famous for distributing an unsolvable version of the puzzle, and
frustrating many people. In fact, all you have to do to make a regular puzzle into an unsolvable one is to swap two tiles (not counting the missing 'x' tile, of course).

In this problem, you will write a program for solving the less well-known 8-puzzle, composed of tiles on a three by three
arrangement.

Input

You will receive a description of a configuration of the 8 puzzle. The description is just a list of the tiles in their initial positions, with the rows listed from top to bottom, and the tiles listed from left to right within a row, where the tiles are represented by numbers 1 to 8, plus 'x'. For example, this puzzle
 1  2  3

x 4 6
7 5 8

is described by this list:

1 2 3 x 4 6 7 5 8

Output

You will print to standard output either the word ``unsolvable'', if the puzzle has no solution, or a string consisting entirely of the letters 'r', 'l', 'u' and 'd' that describes a series of moves that produce a solution. The string should include no spaces and start at the beginning of the line.

Sample Input

 2  3  4  1  5  x  7  6  8 

Sample Output

ullddrurdllurdruldr

Source

South Central USA 1998


八數碼 經典的BFS啊, 我就直接用的單向的BFS,也過了,雙向的應該會快很多。
判重的hash函數看的discuss里的,用逆序數,9!種情況,一一對應,不會有重復的。


 

Source Code
Problem: 1077
User: lovecanon
Memory: 9572K
Time: 125MS
Language: GCC
Result: Accepted
  • Source Code
  • 
        
    #include<stdio.h>
    #include
    <string.h>
    #include
    <stdlib.h>
    struct node
    {
        
    int state[3][3];
        
    int pre;
        
    int dir;
    }queue[
    362881];
    int hash[362881];
    int step[362881];
    int a[3][3];

    int fac(int i)
    {
        
    switch(i)
        {
            
    case 0return 1;
            
    case 1return 1;
            
    case 2return 2;
            
    case 3return 6;
            
    case 4return 24;
            
    case 5return 120;
            
    case 6return 720;
            
    case 7return 5040;
            
    case 8return 40320;
        }
        
    return 0;
    }

    int HASH()
    {
        
    int i,j,k=0,b[9],ret=0,num=0;
        
    for(i=0;i<3;i++)
            
    for(j=0;j<3;j++)
                b[k
    ++]=a[i][j];
        
    for(i=0;i<9;i++)
        {
            num
    =0;
            
    for(j=0;j<i;j++)
                
    if(b[j]>b[i])  num++;
            ret
    +=fac(i)*num;
        }
        
    return ret;
    }

    void output(int len)
    {
        
    int i;
        
    for(i=len;i>=0;i--
        {
            
    if(step[i]==1) printf("l");
            
    if(step[i]==2) printf("r");
            
    if(step[i]==3) printf("u");
            
    if(step[i]==4) printf("d");
        }
        printf(
    "\n");
    }

    int main()
    {
        
    char s[10];
        
    int i,j,rear=0,front=0,tag=0;

        rear
    ++;
        
    for(i=0;i<3;i++)
            
    for(j=0;j<3;j++)
            {
                scanf(
    "%s",s);
                
    if(s[0]=='x')  s[0]='9';
                queue[rear].state[i][j]
    =s[0]-'0';
            }
        queue[rear].pre
    =0;queue[rear].dir=0;
        
    for(i=0;i<3;i++)
            
    for(j=0;j<3;j++)
                a[i][j]
    =queue[rear].state[i][j];

        hash[HASH()]
    =1;
        
    while(front<rear)
        {
            
    int e,f,tmp,cntdir,len;
            front
    ++;
            
    for(i=0;i<3;i++)
                
    for(j=0;j<3;j++)
                {
                    a[i][j]
    =queue[front].state[i][j];
                    
    if(a[i][j]==9) {e=i;f=j;}
                }
            
    if(f-1>=0)
            {
                cntdir
    =1;
                tmp
    =a[e][f];
                a[e][f]
    =a[e][f-1];
                a[e][f
    -1]=tmp;
                tmp
    =HASH();
                
    if(tmp==0
                {
                    
    int t=front;
                    len
    =0;
                    step[len
    ++]=cntdir;
                    
    while(queue[t].pre)  
                    {
                        step[len
    ++]=queue[t].dir;
                        t
    =queue[t].pre;
                    }
                    output(len);
                    
    return 0;
                }
                
    if(!hash[tmp]) 
                {
                    rear
    ++;
                    
    for(i=0;i<3;i++)
                        
    for(j=0;j<3;j++)
                            queue[rear].state[i][j]
    =a[i][j];
                    queue[rear].dir
    =cntdir;queue[rear].pre=front;
                    hash[tmp]
    =1;

                }
                tmp
    =a[e][f];
                a[e][f]
    =a[e][f-1];
                a[e][f
    -1]=tmp;
            }
            
    if(f+1<3)
            {
                cntdir
    =2;
                tmp
    =a[e][f];
                a[e][f]
    =a[e][f+1];
                a[e][f
    +1]=tmp;
                tmp
    =HASH();
                
    if(tmp==0
                {
                    
    int t=front;
                    len
    =0;
                    step[len
    ++]=cntdir;
                    
    while(queue[t].pre)  
                    {
                        step[len
    ++]=queue[t].dir;
                        t
    =queue[t].pre;
                    }
                    output(len);
                    
    return 0;
                }
                
    if(!hash[tmp]) 
                {
                    rear
    ++;
                    
    for(i=0;i<3;i++)
                        
    for(j=0;j<3;j++)
                            queue[rear].state[i][j]
    =a[i][j];
                    queue[rear].dir
    =cntdir;queue[rear].pre=front;
                    hash[tmp]
    =1;
                }
                tmp
    =a[e][f];
                a[e][f]
    =a[e][f+1];
                a[e][f
    +1]=tmp;
            }
            
    if(e-1>=0)
            {
                cntdir
    =3;
                tmp
    =a[e][f];
                a[e][f]
    =a[e-1][f];
                a[e
    -1][f]=tmp;
                tmp
    =HASH();
                
    if(tmp==0
                {
                    
    int t=front;
                    len
    =0;
                    step[len
    ++]=cntdir;
                    
    while(queue[t].pre)  
                    {
                        step[len
    ++]=queue[t].dir;
                        t
    =queue[t].pre;
                    }
                    output(len);
                    
    return 0;
                }
                
    if(!hash[tmp]) 
                {
                    rear
    ++;
                    
    for(i=0;i<3;i++)
                        
    for(j=0;j<3;j++)
                            queue[rear].state[i][j]
    =a[i][j];
                    queue[rear].dir
    =cntdir;queue[rear].pre=front;
                    hash[tmp]
    =1;

                }
                tmp
    =a[e][f];
                a[e][f]
    =a[e-1][f];
                a[e
    -1][f]=tmp;
            }
            
    if(e+1<3)
            {
                cntdir
    =4;
                tmp
    =a[e+1][f];
                a[e
    +1][f]=a[e][f];
                a[e][f]
    =tmp;
                tmp
    =HASH();
                
    if(tmp==0
                {
                    
    int t=front;
                    len
    =0;
                    step[len
    ++]=cntdir;
                    
    while(queue[t].pre)  
                    {
                        step[len
    ++]=queue[t].dir;
                        t
    =queue[t].pre;
                    }
                    output(len);
                    
    return 0;
                }
                
    if(!hash[tmp]) 
                {
                    rear
    ++;
                    
    for(i=0;i<3;i++)
                        
    for(j=0;j<3;j++)
                            queue[rear].state[i][j]
    =a[i][j];
                    queue[rear].dir
    =cntdir;queue[rear].pre=front;
                    hash[tmp]
    =1;

                }
                tmp
    =a[e+1][f];
                a[e
    +1][f]=a[e][f];
                a[e][f]
    =tmp;
            }
        }
        printf(
    "unsolvable\n");
        
    return 0;
    }

posted on 2008-09-20 04:18 infinity 閱讀(2243) 評論(0)  編輯 收藏 引用 所屬分類: acm
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            夜久久久久久| 久久久久免费视频| 亚洲视频日本| 欧美成人一区二免费视频软件| 欧美色视频在线| 日韩亚洲成人av在线| 久久久精品五月天| 午夜精品福利一区二区蜜股av| 欧美午夜精品电影| 中文久久精品| 99在线观看免费视频精品观看| 久久久久久高潮国产精品视| 亚洲影视综合| 国产日韩av在线播放| 午夜精品久久久久久久蜜桃app| 99精品视频免费在线观看| 欧美激情一区二区三区四区 | 国产欧美日韩在线观看| 亚洲欧美国产不卡| 亚洲一区在线观看免费观看电影高清| 欧美视频国产精品| 亚洲欧美国产77777| 一区二区三区欧美日韩| 国产精品分类| 欧美在线视频播放| 久久精品91| …久久精品99久久香蕉国产| 欧美91精品| 欧美激情亚洲激情| 亚洲视频在线观看网站| 亚洲视频免费在线| 激情欧美一区二区| 亚洲国产精品高清久久久| 欧美精品激情在线观看| 亚洲一级免费视频| 欧美在线视频免费观看| 在线观看亚洲视频| 亚洲精品九九| 麻豆久久精品| 亚洲在线国产日韩欧美| 国产欧美日韩一区二区三区| 久久久久在线| 欧美国产一区二区三区激情无套| 一区二区三区免费看| 亚洲影视在线| 亚洲激情视频在线播放| 99成人免费视频| 国产有码一区二区| 亚洲欧洲中文日韩久久av乱码| 欧美日本在线| 久久久久高清| 欧美日韩亚洲网| 久久一区亚洲| 欧美精品一区二区精品网| 欧美一区二区精品在线| 牛牛影视久久网| 欧美一级一区| 欧美精品国产精品| 免费中文日韩| 国产私拍一区| 亚洲高清不卡一区| 99精品国产99久久久久久福利| 狠狠久久婷婷| 亚洲午夜一区二区三区| 欧美精品在欧美一区二区少妇| 久久精品欧美日韩| 欧美日在线观看| 亚洲第一精品在线| 国产日韩欧美一区二区三区四区| 亚洲国产一区二区精品专区| 国产一区二区三区奇米久涩| 99在线精品视频在线观看| 亚洲精品欧美激情| 美乳少妇欧美精品| 久久久视频精品| 国产精品欧美风情| 99视频超级精品| 亚洲日本乱码在线观看| 久久久欧美一区二区| 欧美一区午夜精品| 国产精品国色综合久久| 亚洲精品免费在线播放| 亚洲人成高清| 久久综合成人精品亚洲另类欧美| 久久久久久久综合| 国产午夜亚洲精品羞羞网站| 中日韩美女免费视频网址在线观看| 亚洲欧洲一区二区在线播放| 亚洲女人天堂av| 欧美一区二区精品久久911| 欧美视频官网| 一区二区欧美在线观看| 亚洲视频观看| 国产精品久久看| 一二三区精品福利视频| 亚洲无限av看| 欧美视频三区在线播放| 日韩午夜在线| 午夜电影亚洲| 国产日韩精品在线观看| 亚洲欧美中文日韩v在线观看| 欧美亚洲免费在线| 国产亚洲第一区| 久久精品在线观看| 91久久精品网| 亚洲经典三级| 99伊人成综合| 国产精品九九| 翔田千里一区二区| 久热re这里精品视频在线6| 在线播放亚洲| 欧美噜噜久久久xxx| 一本色道久久综合亚洲精品婷婷| 亚洲欧美日韩在线不卡| 国产亚洲精品v| 蜜臀久久99精品久久久久久9 | 欧美国产日韩精品| 亚洲国产精品热久久| 免费精品视频| 9久草视频在线视频精品| 午夜一区二区三区在线观看 | 午夜精品短视频| 国内精品一区二区| 你懂的成人av| 亚洲一区二区三区免费视频| 久久久久久一区| 夜夜嗨一区二区三区| 国产精品一区免费在线观看| 久久久综合网站| 一区二区三区 在线观看视频| 欧美一区二区三区四区在线观看| 激情亚洲一区二区三区四区| 欧美精品成人在线| 久久久久九九九| 99在线|亚洲一区二区| 久久精品国产96久久久香蕉| 最新国产精品拍自在线播放| 国产精品资源在线观看| 欧美bbbxxxxx| 欧美在线观看一区| 日韩视频在线免费| 麻豆乱码国产一区二区三区| 亚洲四色影视在线观看| 黑人巨大精品欧美一区二区小视频 | 亚洲欧洲综合另类| 久久精品国产一区二区三| 日韩亚洲欧美成人| 黑丝一区二区| 国产精品无码专区在线观看| 欧美成人在线影院| 亚洲欧美清纯在线制服| 亚洲毛片在线观看.| 欧美成人精品一区| 欧美一区二区网站| 亚洲欧美日韩精品久久| 9色porny自拍视频一区二区| 悠悠资源网亚洲青| 国产夜色精品一区二区av| 国产精品久久网| 欧美系列精品| 欧美日韩一级片在线观看| 玖玖精品视频| 久久成人免费网| 午夜精品www| 一本高清dvd不卡在线观看| 欧美成人午夜影院| 麻豆国产精品777777在线| 久久成人一区二区| 亚洲欧美精品| 亚洲欧美精品在线观看| 在线一区二区日韩| 午夜精品久久99蜜桃的功能介绍| 亚洲激情视频网| 欧美日韩精品一区二区天天拍小说| 久久精品国产久精国产爱| 亚洲欧美国产另类| 亚洲中字黄色| 午夜激情一区| 亚洲影视中文字幕| 亚洲综合视频一区| 亚洲欧美激情一区二区| 亚洲午夜高清视频| 亚洲综合首页| 欧美一乱一性一交一视频| 欧美亚洲免费在线| 久久xxxx| 免费欧美日韩| 欧美激情一区二区久久久| 欧美国产精品v| 欧美四级在线观看| 国产欧美日韩不卡免费| 国产酒店精品激情| 国产在线观看精品一区二区三区 | 欧美一区网站| 老司机午夜精品| 亚洲电影免费| 99天天综合性| 久久丁香综合五月国产三级网站| 久久精品一区二区三区不卡| 麻豆亚洲精品|