zoukankan      html  css  js  c++  java
  • 搜索进阶1、八数码(HDU1043)

    http://acm.hdu.edu.cn/showproblem.php?pid=1043

    八数码八境界:

    https://www.cnblogs.com/zufezzt/p/5659276.html

    借用了MAP哈希,发现只能过hdu(249ms),poj一直TLE。

    还是先上个代码吧,以后再改用康拓展开来哈希。。

      1 #include<stdio.h>
      2 #include<algorithm>
      3 #include<string.h>
      4 #include<cstring> 
      5 #include<iostream>
      6 #include<queue>
      7 #include<set>
      8 #include<math.h>
      9 #include<vector> 
     10 #include<functional>
     11 #include<queue>
     12 #include<map>
     13 #define MAXN 100005 
     14 typedef long long ll;
     15 using namespace std;
     16 
     17 int fx[4][2]={1,0,-1,0,0,-1,0,1};
     18 char sum[400000][40]={0}; 
     19 char *lu="dulr";
     20 typedef struct Node{
     21     int x,y,num;
     22 }node;
     23 map<int,int>mp;
     24 int tot=0;
     25 
     26 void pre()
     27 {    queue<node>qe;
     28     sum[0][0]='';
     29     
     30     qe.push((node){2,2,123456789});
     31     while(!qe.empty())
     32     {
     33         node t=qe.front();
     34         qe.pop();
     35         int mt[3][3];
     36         int numt=t.num;
     37         for(int i=2;i>=0;i--)
     38         for(int j=2;j>=0;j--)
     39         {
     40             mt[i][j]=numt%10;
     41             numt/=10;
     42         }
     43         for(int i=0;i<4;i++)
     44         {
     45             int tx=t.x+fx[i][0];
     46             int ty=t.y+fx[i][1];
     47             if(!(tx>=0&&tx<=2&&ty>=0&&ty<=2)) continue;
     48             swap(mt[tx][ty],mt[t.x][t.y]);
     49             int numx=0;
     50             for(int i=0;i<3;i++)
     51             for(int j=0;j<3;j++)
     52             {
     53                 numx=numx*10+mt[i][j];
     54             }
     55             swap(mt[tx][ty],mt[t.x][t.y]);
     56             if(mp[numx]>0) continue;
     57             
     58              mp[numx]=++tot;
     59             strcpy(sum[tot],sum[mp[t.num]]);
     60             int l=strlen(sum[tot]);
     61             sum[tot][l]=lu[i],sum[tot][l+1]='';
     62             qe.push((node){tx,ty,numx});
     63             
     64         }
     65     }
     66     
     67     
     68     
     69 }
     70 void reverse(char *s)
     71 {
     72     int l=strlen(s);
     73     if(l==0)
     74     {
     75         printf("unsolvable
    ");
     76         return;
     77     }
     78     for(int i=l-1;i>=0;i--)
     79     {
     80         if(s[i]=='u') putchar('d');
     81         else if(s[i]=='d') putchar('u');
     82         else if(s[i]=='l') putchar('r');
     83         else if(s[i]=='r') putchar('l');
     84     }
     85     printf("
    ");
     86 }
     87 
     88  
     89 int main()
     90 {    pre();
     91     
     92     char s[50];
     93     while(gets(s)!=NULL)
     94     {    int num=0;
     95         for(int i=0;s[i];i++)
     96         {
     97             if(s[i]>='1'&&s[i]<='8')
     98             {
     99                 num=num*10+s[i]-'0';
    100             }
    101             if(s[i]=='x')
    102             {
    103                 num=num*10+9;
    104             }
    105         }
    106         
    107         reverse(sum[mp[num]]);
    108     }
    109     return 0;
    110 } 
  • 相关阅读:
    30 Day Challenge Day 20 | Leetcode 938. Range Sum of BST
    30 Day Challenge Day 20 | Leetcode 124. Binary Tree Maximum Path Sum
    30 Day Challenge Day 20 | Leetcode 94. Binary Tree Inorder Traversal
    idea2019版本破解
    xml文件时第一行无缘无故报错
    activeMQ的基本使用
    activeMQ的安装(Linux下)
    redis的基本用法
    redis安装(Linux下)
    redis安装(window下)
  • 原文地址:https://www.cnblogs.com/lnu161403214/p/8552294.html
Copyright © 2011-2022 走看看