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

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 閱讀(2253) 評論(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>
            国产欧美一区二区三区沐欲| 性久久久久久久久久久久| 亚洲自拍另类| 免费在线看一区| 狠狠色丁香久久综合频道| 午夜亚洲视频| 中文欧美日韩| 欧美精品色综合| 亚洲精品免费网站| 欧美 日韩 国产一区二区在线视频| 亚洲制服丝袜在线| 国产精品一区二区三区乱码| 欧美亚洲午夜视频在线观看| 亚洲一区精品电影| 国产免费观看久久| 久久精品国产亚洲aⅴ| 亚洲欧美网站| 国产一区二区三区日韩| 久久精品夜色噜噜亚洲aⅴ| 亚洲综合不卡| 国产热re99久久6国产精品| 久久精品av麻豆的观看方式 | 一个人看的www久久| 久久免费视频网| 欧美激情精品久久久久久久变态| 亚洲国产精品传媒在线观看 | 欧美日韩成人综合在线一区二区 | 久久久久久久久久码影片| 夜夜爽av福利精品导航| 欧美日韩第一区日日骚| 亚洲欧美电影在线观看| 亚洲一区二区免费在线| 国产在线麻豆精品观看| 男女精品网站| 欧美日韩ab| 欧美在线视屏| 久久久久国产一区二区三区| 亚洲国语精品自产拍在线观看| 亚洲经典自拍| 欧美四级伦理在线| 久久精品在线观看| 欧美福利在线| 久久精品1区| 欧美黑人多人双交| 亚洲欧美国产精品va在线观看| 欧美一级专区| 一个人看的www久久| 亚洲欧美日韩国产一区二区| 亚洲国产精品成人综合色在线婷婷| 亚洲人线精品午夜| 国产一级精品aaaaa看| 亚洲福利视频网站| 国产精品一区视频网站| 亚洲第一在线综合在线| 国产美女高潮久久白浆| 亚洲国产精品999| 国产日韩欧美不卡| 亚洲人午夜精品免费| 国产人妖伪娘一区91| 亚洲欧洲日本专区| 国产一区二区三区黄| 日韩视频在线一区| 亚洲大片av| 亚洲欧美一区二区激情| 日韩午夜剧场| 久久久久久香蕉网| 性欧美大战久久久久久久免费观看| 免费成人av资源网| 久久亚洲精品欧美| 国产精品黄色在线观看| 亚洲精品1区| 亚洲激情一区二区| 久久久久.com| 久久精品人人做人人爽| 国产精品久久久一区二区三区| 亚洲国产日韩综合一区| 亚洲第一在线| 久久婷婷蜜乳一本欲蜜臀| 欧美在线视频观看| 9l视频自拍蝌蚪9l视频成人| 欧美一区二区高清在线观看| 亚洲精品自在久久| 久久久久久久尹人综合网亚洲| 翔田千里一区二区| 欧美日韩中文在线| 亚洲免费精彩视频| 中文在线资源观看网站视频免费不卡 | 亚洲一区二区精品在线| 欧美日本国产| 亚洲美女中文字幕| 一本色道久久综合亚洲精品不卡| 欧美不卡一卡二卡免费版| 欧美成人精品不卡视频在线观看| 国产日韩精品入口| 亚洲欧美在线免费| 久久国产加勒比精品无码| 国产农村妇女毛片精品久久麻豆| 亚洲淫性视频| 久久久精品性| 伊人久久综合97精品| 麻豆成人av| 亚洲国产精品久久久久秋霞不卡 | 久久久久国色av免费看影院| 国产精品免费网站在线观看| 亚洲欧美精品伊人久久| 欧美在线观看视频| 黄色影院成人| 欧美激情久久久久| 亚洲精品欧美| 午夜综合激情| 国产一区二区成人| 美女在线一区二区| 亚洲精品久久久久| 亚洲欧美日韩一区二区三区在线观看 | 夜夜嗨av一区二区三区四区| 亚洲影院色在线观看免费| 国产伦精品一区二区三区视频孕妇 | 欧美不卡视频一区发布| 99re在线精品| 久久精品亚洲乱码伦伦中文 | 日韩一级不卡| 国产精品毛片| 久久中文欧美| 亚洲视频免费| 美女日韩在线中文字幕| 久久裸体艺术| 亚洲激情视频网站| 亚洲欧美另类综合偷拍| 国产专区精品视频| 91久久久精品| 亚洲尤物视频在线| 午夜影视日本亚洲欧洲精品| 老司机一区二区三区| 亚洲国产精品一区二区三区| 日韩亚洲不卡在线| 欧美在线免费观看| 欧美激情视频一区二区三区不卡| 亚洲精品中文在线| 久久久久一区二区| 国产精品视频999| 亚洲视频在线免费观看| 国产精品日韩欧美大师| 久久艳片www.17c.com| 亚洲激情二区| 久久久国产91| 亚洲小视频在线| 亚洲国产99| 国产欧美日韩一区二区三区在线| 免费一区视频| 久久精品国产精品| 99视频一区| 亚洲第一精品夜夜躁人人爽| 欧美在线播放高清精品| 在线视频日韩精品| 亚洲国产欧美不卡在线观看| 国产视频精品网| 欧美色精品在线视频| 欧美激情视频在线免费观看 欧美视频免费一 | 欧美日韩亚洲不卡| 久久精品视频一| 亚洲欧美日韩网| 91久久久国产精品| 欧美大片一区二区三区| 欧美在线国产| 亚洲午夜av电影| 亚洲九九精品| 亚洲第一视频| 樱桃成人精品视频在线播放| 国产精品女人网站| 欧美三级小说| 欧美看片网站| 欧美日韩91| 欧美色视频在线| 欧美午夜精品久久久久久孕妇| 欧美日本在线观看| 欧美母乳在线| 欧美人与性动交cc0o| 欧美黄色网络| 欧美日韩专区| 国产精品中文字幕欧美| 国产精品一区二区a| 亚洲小说春色综合另类电影| 亚洲日本在线观看| 亚洲精品美女91| 亚洲三级网站| 一本色道久久综合亚洲二区三区| 一本色道久久综合一区| 99re6热在线精品视频播放速度| 亚洲精品在线三区| 99re6这里只有精品视频在线观看| 99riav国产精品| 亚洲午夜羞羞片| 亚洲综合三区| 性欧美大战久久久久久久免费观看| 午夜日本精品| 国产在线精品成人一区二区三区| 久久亚洲精品一区二区| 欧美国产一区二区在线观看| 玖玖综合伊人| 亚洲黄色小视频|