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

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>
            免费在线视频一区| 狠狠色综合色区| 久久久亚洲人| 国产一区二区欧美日韩| 久久久噜噜噜久噜久久| 亚洲国产另类久久久精品极度| 永久久久久久| 久久亚洲国产成人| 在线精品国产欧美| 国产精品大片免费观看| 久久精品女人| 亚洲影视在线播放| 亚洲另类在线一区| 欧美jizz19性欧美| 在线一区观看| 亚洲精品影院在线观看| 国产精品日日摸夜夜摸av| 在线一区亚洲| 亚洲毛片av| 欧美影院久久久| 亚洲在线成人| 国产亚洲综合精品| 在线观看国产日韩| 亚洲一线二线三线久久久| 国产精品视频专区| 国产精品盗摄久久久| 欧美午夜一区二区| 欧美电影打屁股sp| 牛人盗摄一区二区三区视频| 亚洲国产一区二区三区青草影视 | 国产麻豆精品在线观看| 欧美日韩国产成人在线观看| 欧美激情欧美激情在线五月| 欧美激情网友自拍| 欧美亚州一区二区三区 | 久久久久国产精品人| 久久久精品欧美丰满| 两个人的视频www国产精品| 欧美激情亚洲视频| 国产精品麻豆va在线播放| 一区在线影院| 一区二区三区久久| 久久综合亚州| 免费的成人av| 亚洲午夜精品久久| 久久欧美中文字幕| 欧美精品电影在线| 一色屋精品视频免费看| 亚洲欧美日韩中文视频| 免费人成精品欧美精品| 亚洲欧美日韩在线播放| 欧美精品少妇一区二区三区| 欧美 日韩 国产 一区| 国产女主播在线一区二区| 一本久道久久综合狠狠爱| 美女亚洲精品| 久久综合久久综合这里只有精品| 国产精品亚洲综合久久| 夜夜嗨av一区二区三区中文字幕| 欧美在线999| 久久狠狠婷婷| 亚洲第一免费播放区| 亚洲欧美大片| 亚洲一区二区视频| 欧美性色aⅴ视频一区日韩精品| 亚洲国产精品成人| 国内精品久久久久影院 日本资源| 亚洲精品国精品久久99热一| 一个人看的www久久| 亚洲一区在线直播| 亚洲精品一区二区三区av| 亚洲视频视频在线| 在线亚洲伦理| 免费一级欧美片在线观看| 性色一区二区| 国产精品v欧美精品v日韩| 欧美激情一二区| 影音先锋亚洲电影| 欧美一二三视频| 久久av红桃一区二区小说| 国产精品久久久久久久久免费樱桃 | 在线亚洲精品福利网址导航| 久久香蕉精品| 久久综合亚州| 亚洲黄色性网站| 欧美 亚欧 日韩视频在线| 欧美freesex交免费视频| 伊人蜜桃色噜噜激情综合| 久久精品99久久香蕉国产色戒| 欧美一区二区三区免费在线看 | 国产一区二区精品| 亚洲女人av| 久久综合一区| 午夜精品久久久久久久99黑人| 欧美性色视频在线| 欧美在线观看日本一区| 老司机免费视频一区二区三区| 久久精品国产精品| 女人天堂亚洲aⅴ在线观看| 在线观看日韩av先锋影音电影院| 久久久久一本一区二区青青蜜月| 国产欧美综合在线| 免费观看成人鲁鲁鲁鲁鲁视频| 91久久精品一区| 欧美一区在线视频| 亚洲欧洲一区二区在线播放| 国产精品高潮呻吟久久av无限| 亚洲在线视频免费观看| 欧美二区不卡| 久久久精品一区| 午夜日韩在线| 亚洲调教视频在线观看| 亚洲国产你懂的| 精品1区2区3区4区| 国产精品久久久久久av福利软件 | 久久久蜜桃一区二区人| 亚洲桃花岛网站| 亚洲日韩成人| 亚洲国内高清视频| 在线观看欧美精品| 国产在线精品二区| 国产在线一区二区三区四区| 国产精品入口福利| 欧美啪啪一区| 欧美成人dvd在线视频| 久久综合狠狠综合久久激情| 欧美一区二区国产| 久久香蕉精品| 欧美黄污视频| 老司机午夜精品视频在线观看| 久久久综合网| 久久免费的精品国产v∧| 久久免费视频在线观看| 久久久一区二区三区| 久久野战av| 欧美丰满高潮xxxx喷水动漫| 亚洲国产成人精品女人久久久 | 欧美一级视频| 午夜精品电影| 欧美日韩另类字幕中文| 国产日韩一级二级三级| 亚洲国内精品| 久久免费高清视频| 一区二区三区日韩欧美| 性18欧美另类| 欧美性猛交xxxx乱大交蜜桃| 极品裸体白嫩激情啪啪国产精品| 亚洲国产成人久久综合| 亚洲欧美综合v| 亚洲美女视频| 免费成人av在线| 国产一区二区你懂的| 亚洲欧美影音先锋| 亚洲精品女人| 欧美影院成人| 极品尤物久久久av免费看| 午夜在线视频观看日韩17c| 欧美体内谢she精2性欧美| 日韩一级在线| 最新国产成人在线观看| 久久亚洲精品欧美| 亚洲国产欧美一区二区三区久久| 久久激五月天综合精品| 久久aⅴ国产紧身牛仔裤| 国产精品永久免费视频| 香蕉久久夜色精品国产| 欧美大片免费观看| 亚洲成色777777在线观看影院| 久久国产精品久久w女人spa| 国产精品日本一区二区| 久久精品官网| 美国十次了思思久久精品导航| 亚洲国产高清在线| 日韩一级黄色片| 国产精品一区二区在线观看| 99国产麻豆精品| 99这里只有久久精品视频| 亚洲第一视频| 国产精品麻豆欧美日韩ww| 久久久久久久综合| 欧美日韩免费在线观看| 久久综合久色欧美综合狠狠| 欧美激情精品久久久| 欧美一级片一区| 欧美成人a∨高清免费观看| 欧美在线免费观看| 欧美精品在线一区| 可以免费看不卡的av网站| 欧美日本高清| 亚洲黄色成人久久久| 亚洲国产成人久久综合| 性做久久久久久久免费看| 亚洲夜间福利| 欧美视频在线播放| 亚洲黄色三级| 中文国产成人精品久久一| 免费人成网站在线观看欧美高清| 久久久免费av| 亚洲电影av在线| 牛牛国产精品|