• 蓝桥杯OJ PREV-19 九宫重排



    题目描写叙述:


      历届试题 九宫重排  
    时间限制:1.0s   内存限制:256.0MB
          
    问题描写叙述
      如以下第一个图的九宫格中,放着 1~8 的数字卡片。另一个格子空着。与空格子相邻的格子中的卡片能够移动到空格中。经过若干次移动。能够形成第二个图所看到的的局面。

      我们把第一个图的局面记为:12345678.
      把第二个图的局面记为:123.46758
      显然是按从上到下,从左到右的顺序记录数字,空格记为句点。
      本题目的任务是已知九宫的初态和终态,求最少经过多少步的移动能够到达。假设不管多少步都无法到达,则输出-1。
    输入格式
      输入第一行包括九宫的初态,第二行包括九宫的终态。

    输出格式
      输出最少的步数,假设不存在方案,则输出-1。

    例子输入
    12345678.
    123.46758
    例子输出
    3
    例子输入
    13524678.
    46758123.
    例子输出
    22

    题解: BFS+HASH~~ 第一次用hash,之前一直超时。


    代码:


    #include<iostream>
    #include<cstdio>
    char star[10],send[10];
    int nodedir=0,exdir=0;
    bool visit[200000000]={0};
    int dir[4][2]={
        {0,1},
        {0,-1},
        {1,0},
        {-1,0},
    };
    const int Max=200000;
    struct Node{
        int a[3][3],x,y,step;
    } map[Max],temp,end;
    int hash(Node x)
    {
        int num=1,sum=0;
        for(int i=0;i<3;i++)
        for(int j=0;j<3;j++)
        if(x.a[i][j])
        {
        num=x.a[i][j];
        for(int k=0;k<i*3+j;k++)
        num*=8;
        sum+=num;
        }
        return sum;
    }
    int main()
    {
        scanf("%s%s",star,send);
         for(int i=0;i<3;i++)
         for(int j=0;j<3;j++)
         {
         if(star[i*3+j]>'0'&&star[i*3+j]<'9')
         map[0].a[i][j]=star[i*3+j]-'0';
         else if(star[i*3+j]=='.'){
         map[0].a[i][j]=0;
         map[0].x=i; map[0].y=j;
         }
         if(send[i*3+j]>'0'&&send[i*3+j]<'9')
         end.a[i][j]=send[i*3+j]-'0';
         else{ end.a[i][j]=0;
        end.x=i; end.y=j;
         }
         }
         visit[hash(end)]=1;
         visit[hash(map[0])]=1;
         map[0].step=0;
         while(nodedir<=exdir&&exdir<Max)
         {
         for(int i=0;i<4;i++)
         {
         temp=map[nodedir];
         int dx=temp.x+dir[i][0],dy=temp.y+dir[i][1];
         if(dx>=0&&dx<3&&dy>=0&&dy<3)
         {
         int  t;
         t=temp.a[temp.x][temp.y];
         temp.a[temp.x][temp.y]=temp.a[dx][dy];
         temp.a[dx][dy]=t;
         temp.x=dx; temp.y=dy; temp.step++;
         if(hash(temp)==hash(end))
         {
         printf("%d
    ",temp.step);
         return 0;
         }
         if(!visit[hash(temp)])
         map[++exdir]=temp;
         visit[hash(temp)]=1;
         }
         }
         nodedir++;
         }
         printf("-1
    ");
    return 0;
    }



  • 相关阅读:
    跟踪spring MVC的请求
    spring的Java注解方式
    spring IOC理解
    spring aop
    spring mvc开发过程中的乱码问题
    springmvc的Controller里实现转发的同时弹出提示对话框
    C++程序加速的12个方法
    VSCode官方配置C/C++
    VSCode配置C/C++环境
    Numba 0.44 中文文档
  • 原文地址:https://www.cnblogs.com/slgkaifa/p/7230765.html
Copyright © 2020-2023  润新知