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

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 閱讀(2260) 評論(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>
            这里只有精品视频| 免费观看30秒视频久久| 亚洲激情影视| 欧美主播一区二区三区美女 久久精品人 | 欧美在线视频a| 亚洲深夜影院| 欧美精品亚洲| 亚洲黑丝在线| 激情久久久久久久| 性欧美video另类hd性玩具| 欧美视频在线观看 亚洲欧| 欧美大片免费观看| 精久久久久久久久久久| 欧美一区二区三区免费观看视频| 亚洲午夜免费福利视频| 欧美不卡激情三级在线观看| 亚洲国产精品久久人人爱蜜臀| 午夜精品久久久久久99热软件 | 亚洲日本va午夜在线电影 | 欧美在线播放一区| 欧美专区在线播放| 国产精品美女久久久久久免费| 亚洲精品中文字幕有码专区| 亚洲日本aⅴ片在线观看香蕉| 久久夜色精品| 美女精品在线观看| 在线精品一区| 免费观看成人www动漫视频| 欧美承认网站| av成人黄色| 欧美视频一区二区三区…| 亚洲欧美日韩一区| 国产精品自拍视频| 欧美一区免费视频| 狂野欧美激情性xxxx| 在线观看国产成人av片| 你懂的国产精品| 亚洲经典视频在线观看| 一区二区激情| 国产精品视频精品| 久久国产主播| 欧美国产亚洲视频| 一区二区精品在线观看| 国产精品人人做人人爽 | 一区二区三区视频观看| 午夜欧美精品久久久久久久| 国产欧美日韩精品a在线观看| 欧美在线视频一区| 亚洲高清成人| 亚洲欧美日本日韩| 极品少妇一区二区三区| 欧美激情日韩| 午夜精品免费在线| 欧美韩日一区二区| 午夜国产一区| 亚洲国产日韩一区二区| 亚洲网站视频| 免费久久99精品国产自| 亚洲午夜影视影院在线观看| 国产自产精品| 日韩午夜免费| 久久视频免费观看| 在线视频亚洲一区| 激情久久五月天| 欧美日韩在线免费观看| 久久精品日产第一区二区| 亚洲精品久久视频| 欧美日韩免费在线视频| 欧美国产一区二区在线观看| 亚洲一区www| 伊人狠狠色丁香综合尤物| 欧美日韩人人澡狠狠躁视频| 久久大综合网| 亚洲午夜精品久久| 亚洲韩国精品一区| 久久蜜桃av一区精品变态类天堂| 99在线视频精品| 女仆av观看一区| 午夜性色一区二区三区免费视频| 亚洲福利视频二区| 久久婷婷国产综合精品青草| 亚洲小视频在线| 亚洲日本va午夜在线影院| 国产亚洲亚洲| 国产精品久久久一区二区| 欧美国产欧美亚洲国产日韩mv天天看完整| 午夜精品成人在线| 亚洲天堂av综合网| 99视频日韩| 亚洲国产精品成人va在线观看| 国产日韩欧美中文在线播放| 欧美视频在线观看免费网址| 美玉足脚交一区二区三区图片| 欧美亚洲日本国产| 亚洲欧美久久| 亚洲女性裸体视频| 亚洲午夜精品一区二区三区他趣| 亚洲理论在线| 亚洲看片网站| 亚洲一区日韩在线| 一本大道久久a久久精品综合| 在线精品福利| 亚洲国产精品第一区二区三区| 狠狠色丁香久久婷婷综合_中| 国产伦精品一区二区三区四区免费 | 久久亚洲春色中文字幕| 久久久久久久一区| 久久蜜臀精品av| 国产欧美日韩亚洲一区二区三区| 国产精品久久一区主播| 国产精品户外野外| 国产精品毛片大码女人| 国产精品一二一区| 国产一区二区精品丝袜| 国产在线国偷精品产拍免费yy| 国产一区二区三区最好精华液| 国产欧美日韩一区| 一区福利视频| 亚洲国产专区校园欧美| 亚洲精品一区二区三区婷婷月| 日韩一区二区久久| 亚洲综合色网站| 久久久www成人免费无遮挡大片| 久久精品视频在线观看| 美女网站久久| 亚洲欧洲中文日韩久久av乱码| 一区二区不卡在线视频 午夜欧美不卡'| 亚洲区一区二区三区| 亚洲素人一区二区| 久久精品国产精品亚洲综合| 欧美.日韩.国产.一区.二区| 欧美日韩一区二区视频在线| 国产精品毛片a∨一区二区三区| 国产亚洲网站| 99国产精品国产精品毛片| 亚洲男人的天堂在线| 久久综合给合久久狠狠色| 欧美激情小视频| 亚洲午夜女主播在线直播| 欧美在线观看一区| 欧美顶级艳妇交换群宴| 国产精品亚洲人在线观看| 精品成人在线观看| 中文日韩电影网站| 久久免费视频观看| 99v久久综合狠狠综合久久| 欧美亚洲免费在线| 欧美日本在线观看| 狠狠色狠狠色综合日日五| 妖精成人www高清在线观看| 欧美中文字幕精品| 亚洲人体一区| 久久久免费精品视频| 亚洲一区二区视频在线观看| 久久午夜激情| 国产精品羞羞答答| 日韩小视频在线观看| 久久婷婷久久| 欧美一区二区三区日韩视频| 亚洲福利一区| 久久久99爱| 国产精品羞羞答答xxdd| 日韩视频免费大全中文字幕| 久久久亚洲欧洲日产国码αv| 一本久道综合久久精品| 你懂的一区二区| 激情丁香综合| 久久精品日韩欧美| 亚洲一区二区黄色| 亚洲色诱最新| 欧美日本在线视频| 亚洲国产一区在线观看| 久久漫画官网| 亚洲尤物在线| 欧美日韩综合在线免费观看| 亚洲精品小视频在线观看| 免费亚洲婷婷| 久久精品欧美| 国产一区二区看久久| 亚洲欧美久久久| 一本色道久久88综合亚洲精品ⅰ| 亚洲精品视频在线观看免费| 免费观看不卡av| 91久久久久久久久久久久久| 美日韩精品免费观看视频| 欧美中文字幕在线| 国产综合视频| 久久久久一区二区三区| 篠田优中文在线播放第一区| 国产精品一区二区在线| 亚洲欧美日韩一区二区| 亚洲一区国产一区| 国产精品无码专区在线观看 | 亚洲综合视频一区| 亚洲另类视频| 欧美视频在线观看一区| 欧美亚洲系列| 欧美亚洲日本一区| 伊人精品视频| 欧美激情久久久久久|