HDU1043 Eight[bfs]

本文介绍了一种解决8-Puzzle问题的有效算法。通过预先计算并利用康拓展开的哈希值来加速搜索过程,确保了解决方案的快速查找。文章详细解释了如何通过宽度优先搜索(BFS)构建解决方案路径,并提供了完整的实现代码。


A - Eight
Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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, several descriptions of configuration of the 8 puzzle. One 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. Do not print a blank line between cases. 
 

Sample Input

     
2 3 4 1 5 x 7 6 8
 

Sample Output

     
ullddrurdllurdruldr
 


题意:

不断跟x交换位置 使得变为1 2 3 4 5 6 7 8 x


要做预处理,不然会超时.

注意 因为是从1 2 3 4 5 6 7 8 x 这个情况开始打的表, 记得x是的移动是相反的 

用康拓展开来计算hash值,然后用数组去保存结果

记得保存路径的时候 先是dir的在前然后再加上已经走了的路径 因为是从终点走回去起点.




#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
using namespace std;
const int N=4e5;
int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
int fac[]={1,1,2,6,24,120,720,5040,40320,362880};
const int aim=46233;
bool mark[N];
string ans[N];
char dis[]="dlur";//注意反过来...   从12345678x开始的  所以向下是向上 向左是向右
struct node
{
    int num[10];
    int x,ha;
    string path;
};
int Cantor(int num[])// 用康拓展开计算hash值
{
    int sum=0;
    for (int i=0 ; i<9 ; i++)
    {
        int cnt=0;
        for (int j=i+1; j<9 ; j++)
            if (num[i]>num[j])
                cnt++;
        sum+=cnt*fac[9-i-1];
    }
    return sum;
}
queue<node>q;
void bfs(node start)
{
    while (!q.empty())
        q.pop();
    node later;
    q.push(start);
    while (!q.empty())
    {
        start=q.front();
        q.pop();
        int x=start.x/3;
        int y=start.x%3;
        for (int i=0 ; i<4 ; i++)
        {
            int nx=x+dir[i][0];
            int ny=y+dir[i][1];
            if (nx<0 || nx>=3 || ny<0 || ny>=3)
                continue;
            later=start;
            later.x=nx*3+ny;
            later.num[start.x]=later.num[later.x];
            later.num[later.x]=0;
            later.ha=Cantor(later.num);
            if (!mark[later.ha])
            {
                mark[later.ha]=1;
                later.path=dis[i]+later.path;
                ans[later.ha]=later.path;
                q.push(later);
            }
        }
    }
}
int main()
{
    //printf("%d\n",Cantor(num)); //答案的哈希值 1,2,3,4,5,6,7,8,0
    char ch;
    memset(mark,0,sizeof(mark));
    node now;
    for (int i=0 ; i<8 ; i++)
        now.num[i]=i+1;
    now.num[8]=0;
    now.x=8;
    now.path="";
    ans[aim]=now.path;
    mark[aim]=1;
    bfs(now);
    while (cin>>ch)//scanf一直输入会出现错误...
    {
        if (ch=='x')
            now.num[0]=0;
        else
            now.num[0]=ch-'0';
        for (int i=1 ; i<9 ; i++)
        {
            cin>>ch;
            if (ch=='x')
                now.num[i]=0;
            else
                now.num[i]=ch-'0';
        }
        now.ha=Cantor(now.num);
        if (mark[now.ha])
            cout<<ans[now.ha]<<endl;
        else
            printf("unsolvable\n");
    }
    return 0;
}







数据集介绍:电力线目标检测数据集 一、基础信息 数据集名称:电力线目标检测数据集 图片数量: 训练集:2898张图片 验证集:263张图片 测试集:138张图片 总计:3299张图片 分类类别: 类别ID: 0(电力线) 标注格式: YOLO格式,包含对象标注信息,适用于目标检测任务。 数据格式:JPEG/PNG图片,来源于空中拍摄或监控视觉。 二、适用场景 电力设施监控与巡检: 数据集支持目标检测任务,帮助构建能够自动识别和定位电力线的AI模型,用于无人机或固定摄像头巡检,提升电力设施维护效率和安全性。 能源与公用事业管理: 集成至能源管理系统中,提供实时电力线检测功能,辅助进行风险 assessment 和预防性维护,优化能源分配。 计算机视觉算法研究: 支持目标检测技术在特定领域的应用研究,促进AI在能源和公用事业行业的创新与发展。 专业培训与教育: 数据集可用于电力行业培训课程,作为工程师和技术人员学习电力线检测与识别的重要资源。 三、数据集优势 标注精准可靠: 每张图片均经过专业标注,确保电力线对象的定位准确,适用于高精度模型训练。 数据多样性丰富: 包含多种环境下的电力线图片,如空中视角,覆盖不同场景条件,提升模型的泛化能力和鲁棒性。 任务适配性强: 标注格式兼容YOLO等主流深度学习框架,便于快速集成和模型开发,支持目标检测任务的直接应用。 实用价值突出: 专注于电力线检测,为智能电网、自动化巡检和能源设施监控提供关键数据支撑,具有较高的行业应用价值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值