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

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>
            国产一区二区三区在线免费观看| 国产欧美一区二区三区久久 | 日韩视频永久免费| 国内精品久久久久久| 国产一区二区三区在线观看免费 | 免费观看国产成人| 欧美二区在线观看| 欧美性久久久| 国产欧美日韩高清| 在线观看欧美| 一本一本a久久| 欧美一区中文字幕| 欧美成人免费网站| 亚洲神马久久| 久久综合色天天久久综合图片| 欧美高清视频免费观看| 国产精品xxxxx| 影音欧美亚洲| 亚洲视频在线播放| 久久综合网络一区二区| 亚洲毛片在线看| 亚洲无限乱码一二三四麻| 欧美一级一区| 欧美精品一区在线播放| 国产亚洲欧美一级| 一本综合久久| 美脚丝袜一区二区三区在线观看 | 国产精品一区二区三区观看| 狠狠v欧美v日韩v亚洲ⅴ| 亚洲精品一二区| 久久精品视频在线观看| 亚洲欧洲日夜超级视频| 亚洲一区二区免费| 欧美va天堂在线| 国产女人18毛片水18精品| 亚洲欧洲一区二区三区| 久久er99精品| 一本色道久久综合狠狠躁篇怎么玩 | 亚洲性夜色噜噜噜7777| 老牛嫩草一区二区三区日本| 一区二区冒白浆视频| 噜噜爱69成人精品| 国内精品嫩模av私拍在线观看 | 在线观看亚洲专区| 久久成人久久爱| 99re热精品| 欧美激情一区二区三区全黄| 国产自产2019最新不卡| 亚洲视频在线二区| 亚洲精品综合精品自拍| 欧美成人免费网站| 亚洲精品123区| 欧美成人综合网站| 久久久久久久久久久久久9999| 国产精品一区二区久激情瑜伽| 一区二区三区色| 亚洲肉体裸体xxxx137| 免费不卡亚洲欧美| 1024精品一区二区三区| 女仆av观看一区| 狼狼综合久久久久综合网| 伊人狠狠色j香婷婷综合| 久久久久中文| 久久精品一级爱片| 尤物yw午夜国产精品视频明星| 久久久久久9999| 欧美在线免费视频| 尹人成人综合网| 亚洲第一网站免费视频| 欧美成人亚洲| 一区二区三区久久久| 一本一道久久综合狠狠老精东影业 | 亚洲第一黄网| 欧美激情一区二区三区蜜桃视频 | 欧美一区二区三区四区视频| 国产老肥熟一区二区三区| 香蕉成人啪国产精品视频综合网| 亚洲视频二区| 国产亚洲人成a一在线v站| 久久躁日日躁aaaaxxxx| 女人色偷偷aa久久天堂| 一本不卡影院| 亚洲网站视频福利| 激情六月婷婷综合| 最新成人av网站| 国产精品久久久久99| 欧美顶级艳妇交换群宴| 欧美日韩日本视频| 香蕉久久久久久久av网站| 午夜精品福利一区二区三区av| 国产亚洲精品bt天堂精选| 欧美激情1区| 国产精品久久久亚洲一区 | 亚洲午夜久久久| 亚洲欧美国产日韩天堂区| 国产女精品视频网站免费| 麻豆成人在线观看| 欧美777四色影视在线| av不卡免费看| 欧美一区三区三区高中清蜜桃| 国内偷自视频区视频综合| 亚洲黄色尤物视频| 国产日韩欧美一区| 老鸭窝91久久精品色噜噜导演| 欧美日本网站| 欧美大片18| 国产精品中文字幕在线观看| 亚洲高清不卡一区| 国语自产偷拍精品视频偷 | 亚洲一二三区精品| 久久国产免费看| 午夜免费电影一区在线观看| 久久免费视频网| 欧美在线啊v| 欧美日韩亚洲成人| 亚洲电影免费观看高清完整版在线观看 | 欧美日韩综合在线免费观看| 久久蜜桃av一区精品变态类天堂| 欧美理论在线| 亚洲第一区色| 亚洲第一天堂无码专区| 在线综合视频| 一片黄亚洲嫩模| 蜜桃av久久久亚洲精品| 美女精品在线| 亚洲丶国产丶欧美一区二区三区| 亚洲自拍电影| 亚洲一区免费网站| 欧美日韩亚洲一区三区| 亚洲国产mv| 亚洲乱码日产精品bd| 欧美成人一区在线| 欧美黄免费看| 亚洲人成网站精品片在线观看| 久久精品视频在线| 久久一区二区三区四区| 激情久久久久| 久久先锋资源| 亚洲第一综合天堂另类专| 亚洲国产精品第一区二区| 久久女同精品一区二区| 看片网站欧美日韩| 亚洲第一成人在线| 蜜臀99久久精品久久久久久软件| 老司机精品视频网站| 亚洲黄色性网站| 国产午夜精品久久久久久免费视| 一区二区三区久久久| 亚洲伊人伊色伊影伊综合网 | 国产乱码精品一区二区三| 香蕉久久a毛片| 久久视频一区| 亚洲精品视频在线观看网站| 欧美经典一区二区| 一区二区三区视频观看| 亚洲欧美日韩国产中文| 国产一区二区三区在线观看视频 | 一本色道久久综合亚洲91| 欧美调教vk| 午夜欧美大尺度福利影院在线看| 久久精品国产视频| 在线日韩欧美视频| 欧美日韩精品是欧美日韩精品| 亚洲永久精品大片| 欧美成人四级电影| 亚洲色图自拍| 国模私拍一区二区三区| 欧美顶级少妇做爰| 亚洲午夜电影网| 欧美第一黄网免费网站| 亚洲一区二区黄| 在线观看日韩国产| 欧美系列一区| 欧美成人精品一区二区| 亚洲综合大片69999| 欧美激情视频在线播放| 亚洲欧洲99久久| 亚洲激情在线激情| 国产日韩视频| 欧美日韩在线观看一区二区| 欧美影院久久久| 亚洲精品中文字幕在线| 久久漫画官网| 亚洲综合视频1区| 亚洲第一天堂av| 国产亚洲欧美一区| 国产精品jvid在线观看蜜臀| 久久久久久999| 亚洲免费一在线| 最近看过的日韩成人| 久久久青草婷婷精品综合日韩| 亚洲精品一区二区三区在线观看| 国产日产精品一区二区三区四区的观看方式 | 欧美激情成人在线| 久久国产高清| 午夜宅男欧美| 亚洲欧美中文日韩v在线观看| 亚洲激情成人网| 免费影视亚洲| 久久女同精品一区二区|