八数码问题——HDU 1043

2024-09-05 04:32
文章标签 问题 hdu 数码 1043

本文主要是介绍八数码问题——HDU 1043,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

对应杭电题目: 点击打开链接


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  45  6  7  89 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  45  6  7  8     5  6  7  8     5  6  7  8     5  6  7  89  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  xr->            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
康拓展开判重,反向BFS
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
#include<cstring>
#include<string>
#include<iostream>
const int MAXN=500000+10;
const int MAXNHASH=500000+10;
using namespace std;
typedef int State[9];
State st[MAXN];
int goal[9];
int vis[370000];
int fact[9];
int fa[MAXN];
int dir[MAXN];
int codestart,codeend;
const int dx[]={-1,1,0,0};
const int dy[]={0,0,-1,1};
char cal[5]="durl";void init_lookup_table()
{fact[0]=1;for(int i=1; i<9; i++){fact[i]=fact[i-1]*i;}
}int Code(State &s)
{int code=0;for(int i=0; i<9; i++){int cnt=0;for(int j=i+1; j<9; j++){if(s[j]<s[i]) cnt++; }code+=fact[8-i]*cnt;}return code;
}void bfs()
{memset(fa,0,sizeof(fa));memset(vis,0,sizeof(vis));int front=1, rear=2;while(front<rear){//cout<<front<<endl;State& s=st[front];//if(memcmp(goal, s, sizeof(s))==0) return front;int z;for(z=0; z<9; z++) if(!s[z]) break;int x=z/3, y=z%3;for(int i=0; i<4; i++){int newx=x+dx[i];int newy=y+dy[i];int newz=newx*3+newy;if(newx>=0 && newx<3 && newy>=0 && newy<3){State&t =st[rear];memcpy(t,s,sizeof(s));t[newz]=s[z];t[z]=s[newz];int code=Code(t);int code1=Code(s);if(!vis[code]){vis[code]=1;fa[code]=code1;dir[code]=i;rear++;}}}front++;}
}void print(int num)
{if(num!=codeend){cout<<cal[dir[num]];print(fa[num]);}
}int main()
{//freopen("in.txt","r",stdin);init_lookup_table();char ch;for(int i=0; i<8; i++) st[1][i]=i+1;st[1][8]=0;codeend=Code(st[1]);vis[codeend]=1;bfs();while(cin>>ch){if(ch=='x') goal[0]=ch-120;else goal[0]=ch-'0';for(int i=1; i<9; i++){cin>>ch;if(ch=='x') goal[i]=ch-120;else goal[i]=ch-'0';}codestart=Code(goal);if(vis[codestart]){print(codestart);cout<<endl;}else cout<<"unsolvable"<<endl;}return 0;
}


这篇关于八数码问题——HDU 1043的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1137952

相关文章

MybatisGenerator文件生成不出对应文件的问题

《MybatisGenerator文件生成不出对应文件的问题》本文介绍了使用MybatisGenerator生成文件时遇到的问题及解决方法,主要步骤包括检查目标表是否存在、是否能连接到数据库、配置生成... 目录MyBATisGenerator 文件生成不出对应文件先在项目结构里引入“targetProje

C#使用HttpClient进行Post请求出现超时问题的解决及优化

《C#使用HttpClient进行Post请求出现超时问题的解决及优化》最近我的控制台程序发现有时候总是出现请求超时等问题,通常好几分钟最多只有3-4个请求,在使用apipost发现并发10个5分钟也... 目录优化结论单例HttpClient连接池耗尽和并发并发异步最终优化后优化结论我直接上优化结论吧,

Java内存泄漏问题的排查、优化与最佳实践

《Java内存泄漏问题的排查、优化与最佳实践》在Java开发中,内存泄漏是一个常见且令人头疼的问题,内存泄漏指的是程序在运行过程中,已经不再使用的对象没有被及时释放,从而导致内存占用不断增加,最终... 目录引言1. 什么是内存泄漏?常见的内存泄漏情况2. 如何排查 Java 中的内存泄漏?2.1 使用 J

numpy求解线性代数相关问题

《numpy求解线性代数相关问题》本文主要介绍了numpy求解线性代数相关问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 在numpy中有numpy.array类型和numpy.mat类型,前者是数组类型,后者是矩阵类型。数组

解决systemctl reload nginx重启Nginx服务报错:Job for nginx.service invalid问题

《解决systemctlreloadnginx重启Nginx服务报错:Jobfornginx.serviceinvalid问题》文章描述了通过`systemctlstatusnginx.se... 目录systemctl reload nginx重启Nginx服务报错:Job for nginx.javas

Redis缓存问题与缓存更新机制详解

《Redis缓存问题与缓存更新机制详解》本文主要介绍了缓存问题及其解决方案,包括缓存穿透、缓存击穿、缓存雪崩等问题的成因以及相应的预防和解决方法,同时,还详细探讨了缓存更新机制,包括不同情况下的缓存更... 目录一、缓存问题1.1 缓存穿透1.1.1 问题来源1.1.2 解决方案1.2 缓存击穿1.2.1

vue解决子组件样式覆盖问题scoped deep

《vue解决子组件样式覆盖问题scopeddeep》文章主要介绍了在Vue项目中处理全局样式和局部样式的方法,包括使用scoped属性和深度选择器(/deep/)来覆盖子组件的样式,作者建议所有组件... 目录前言scoped分析deep分析使用总结所有组件必须加scoped父组件覆盖子组件使用deep前言

解决Cron定时任务中Pytest脚本无法发送邮件的问题

《解决Cron定时任务中Pytest脚本无法发送邮件的问题》文章探讨解决在Cron定时任务中运行Pytest脚本时邮件发送失败的问题,先优化环境变量,再检查Pytest邮件配置,接着配置文件确保SMT... 目录引言1. 环境变量优化:确保Cron任务可以正确执行解决方案:1.1. 创建一个脚本1.2. 修

Python 标准库time时间的访问和转换问题小结

《Python标准库time时间的访问和转换问题小结》time模块为Python提供了处理时间和日期的多种功能,适用于多种与时间相关的场景,包括获取当前时间、格式化时间、暂停程序执行、计算程序运行时... 目录模块介绍使用场景主要类主要函数 - time()- sleep()- localtime()- g

SpringBoot项目删除Bean或者不加载Bean的问题解决

《SpringBoot项目删除Bean或者不加载Bean的问题解决》文章介绍了在SpringBoot项目中如何使用@ComponentScan注解和自定义过滤器实现不加载某些Bean的方法,本文通过实... 使用@ComponentScan注解中的@ComponentScan.Filter标记不加载。@C