POJ 1077 Eight

POJ1077 Eight A* 题目链接 http://poj.org/problem?id=1077 分析 直接用BFS进行状态扩展会超时,需要设计估价函数跑A*算法。 每次只能使一个数位置改变,因此未来的操作次数不会小于每个数当前位置于最终位置的曼哈顿距离之和。 用这个作为估价函数,用康托展开将排列映射成整数来记录某一状态是否访问过。 有一结论,如果列数为奇数,那么初始状态和最终状态的逆序对数奇偶性相同。 AC代码 // 调... 阅读详情

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与康托展开状态判重进行最优解的寻找,同时记录路径。

AC代码:

#include <stdio.h>
#include <string.h>
struct node {
	int op;
	int state[9];
	int parent;
	int dis;
};
struct node queue[362880];
//一些阶乘常数
long long factory[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
int vis[362880];
int dir[4][2] = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}}; // r l u d
int operation[362880];
int start[9], goal[9] = {1, 2, 3, 4, 5, 6, 7, 8, 0};
int Cantor(int str[], int n) {
	long long res = 0;
	for (int i = 0; i < n; i++) {
		int counted = 0;
		for (int j = i + 1; j < n; j++) {
			if (str[i] > str[j])
				++counted;
		}
		res += counted * factory[n - i - 1];
	}
	if (!vis[res]) {
		vis[res] = 1;
		return 1;
	} else {
		return 0;
	}
}
void print_way(int n) {
	int top = 0;
	operation[top] = n;
	while (queue[operation[top]].parent != -1) {
		top++;
		operation[top] = queue[operation[top - 1]].parent;
	}
	for (int i = top; i >= 0; i--) {
		switch (queue[operation[i]].op) {
			case 0: {
				printf("r");
			}
			break;
			case 1: {
				printf("l");
			}
			break;
			case 2: {
				printf("u");
			}
			break;
			case 3: {
				printf("d");
			}
			break;
		}
	}
}
int bfs() {
	int front = 0, rear = 1;
	memcpy(queue[front].state, start, sizeof(queue[front].state));
	queue[front].dis = 0;
	queue[front].op = -1;
	queue[front].parent = -1;
	int x, y, z;
	while (rear - front) {

		struct node head  = queue[front];
		for (int i = 0; i < 9; i++) {
			if (head.state[i] == 0) z = i;
		}
		x = z % 3;
		y = z / 3;
		int newx, newy, newz;
			if (!memcmp(head.state, goal, sizeof(goal))) {				
							print_way(front);
							return 0;
						}

		for (int i = 0; i < 4; i++) {
			newx = x + dir[i][0];
			newy = y + dir[i][1];
			newz = newy * 3 + newx;
			if (newx >= 0 && newx < 3 && newy >= 0 && newy < 3) {
				int temp_;
				struct node temp;
				memcpy(temp.state, head.state, sizeof(temp.state));
				temp_ = temp.state[z];
				temp.state[z] = temp.state[newz];
				temp.state[newz] = temp_;
				temp.dis = head.dis + 1;
				temp.op = i;
				temp.parent = front;
				if (Cantor(temp.state,9)) {
					queue[rear++] = temp;
				}
			
			}
		}
		front++;
	}
	printf("unsolvable\n");
	return -1;
}
int main() {
	char ch;
	for (int i = 0; i < 9; i++) {
		scanf("%c", &ch);
		if (ch == ' ') {
			i--;
			continue;
		}
		if (ch == 'x') start[i] = 0;
		else if (ch != ' ') start[i] = ch - '0';
	}
	bfs();
}

POJ1077 Eight —— 反向BFS 主页面:http://write.blog.youkuaiyun.com/postedit/77825569 代码一:以数组充当队列,利用结构体中的pre追溯上一个状态在数组(队列)中的下标: #include #include #include #include #include #include #include #include #include #incl 阅读详情

相关推荐

POJ1077 Eight —— 正向BFS

主页面:http://write.blog.youkuaiyun.com/postedit/77825569 代码一:以数组充当队列,利用结构体中的pre追溯上一个状态在数组(队列)中的下标: #include #include #include #include #include #include #include #include #include #in

A man can be destroyed, but not defeated. 1473

poj 1077 Eight

// poj 1077 Eight /* 普通的广搜题,因为忘了标记爆了好多次空间,吸取教训! */ #include #include #include using namespace std; const int inf = 1=0 && v=0 && w

birdforever的专栏 672

Unity动画状态机实战:Trigger和Bool参数到底怎么选?附5个常见场景解析

本文深入解析Unity动画状态机中Trigger和Bool参数的核心差异与应用场景。通过对比两者在描述瞬时事件与持续状态上的本质区别,并结合角色移动、攻击连招、跳跃落地、受击死亡及环境交互等5个实战场景,提供了清晰的选择逻辑与避坑指南,帮助开发者构建更健壮、易维护的动画逻辑。

weixin_29009501的博客 121

POJ 1077 Eight(BFS:输出路径)

POJ 1077 Eight(BFS:输出路径) http://poj.org/problem?id=1077 题意:给你一个八数码3*3的网格,要你输出解的路径. 分析:参考刘汝佳入门经典P132.        首先对于每个网格状态,我们把它转化为一个9位的十进制数.然后用state[][9]二维数组来保存所有的我们已经发现的状态st,且用state[]来表示当前我们用的栈.

code 1468

WSL2不能上网,Failed to connect to github.com port 443问题解决方案

WSL2不能上网: 问题:ping外网无效 例如:ping www.baidu.com失败 解决方案: 在wsl: $sudo vi /etc/wsl.conf wsl.conf: [network] generateResolvConf = false 在PowerShell: $sudo vi /etc/etc/resolv.conf resolv.conf: # This file was automatically generated by WSL. To stop automatic gen

Jason_Chen__的博客 9527

基于yolov5的鸟类图像准确识别

本项目主要基于yolov5进行鸟类准确识别,主要识别白鹭家族,红脚鹬,反嘴鹬,环颈珩,小白鹭以及夜鹭六类鸟

weixin_46064184的博客 3823

POJ - 1077 Eight(A∗算法)

POJ - 1077 Eight(A∗算法) #include<iostream> #include<algorithm> #include<map> #include<queue> #define x first #define y second using namespace std; typedef pair<int,string> PIS; typedef pair<char,string> PCS; int f(

00后的博客 515

从零到一:在Ubuntu 20.04上搭建ROS1与PX4无人机开发环境

本文详细指导如何在Ubuntu 20.04系统上搭建完整的ROS1与PX4无人机开发环境。内容涵盖ROS Noetic安装、PX4固件源码编译、MAVROS通信桥接以及QGroundControl地面站的配置,并通过Gazebo仿真完成全链路联调测试,为无人机算法开发与验证提供稳定可靠的基础平台。

weixin_29292431的博客 167

poj1077Eight

Eight Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21880   Accepted: 9674   Special Judge Description The 15-puzzle has been around for over 100 years;

Min_lala的专栏 1035

Ta-Lib源码解析(三):蜡烛图指标 (Candlestick Indicator) #(附Python重构代码和示意图)(补充中)

蜡烛图指标为识别K线形态的指标,笔者认为重点在于定义方式所提供的思路,背后的投资逻辑可以借鉴但不建议直接参考文档的翻译内容和源码逻辑存在不一致的现象,笔者分别列出中文百科中的定义和源码实际逻辑,供大家参考技术分析理论中的一些模糊词,ta-lib中给出了精确的定义源码

量化就是探索生活 3254

链路状态路由协议 OSPF (一)

本章将会学习链路状态路由协议OSPF,理解OSPF的基本概念,工作原理,掌握OSPF配置基本命令。本章重点,OSPF的基本概念,OSPF配置。

yj11290301的博客 3475

POJ1077Eight

康托逆展开写错还AC的渣渣...

H992109898的博客 788

02-Redis 核心特性与数据结构详解:揭秘高性能的技术内核

本文深度拆解 Redis 高性能的技术内核:揭秘其凭“内存存储 + C 语言开发” 实现微秒级延迟、10 万 + QPS 的性能优势;详解 5 种基础数据类型(String/Hash 等)与 4 种扩展数据类型(Stream/Geospatial 等)的特性及场景;还介绍多语言支持、RDB/AOF 持久化、主从复制 / 哨兵 / 集群等高可用方案,助读者掌握 Redis 技术核心与实践逻辑。

蜗牛老师,专注编程领域 996

POJ1077 Eight —— 经典的搜索问题

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 Eight Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 25408    Accepted Submiss

A man can be destroyed, but not defeated. 3460

自然语言处理在智能客服系统中的应用

引言与背景 1.1 书籍的目的和内容概述 《自然语言处理在智能客服系统中的应用》旨在探讨自然语言处理(NLP)技术在智能客服系统中的应用,帮助读者理解NLP的核心概念及其在解决实际业务问题中的关键作用。随着人工智能技术的快速发展,自然语言处理成为构建智能客服系统的核心模块之一。本书将深入解析NLP

AI天才研究院 968

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

poj1077 /hdoj 1043 Eight

这道题用启发式搜索,我在北大网站上可以过,杭电网站上超时,看来杭电的数据有点变态了。 #include #include #include #include using namespace std; int target_pos[10][2]; char buf[100]; struct state { int pos_i, pos_j; unsigned char m[4

皓首不倦 不负少年 882

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

POJ 1077 Eight A*算法求解

题目描述         在一个3×3的网格中,1~8这8个数字和一个“X”恰好不重不漏地分布在这3×3的网格中。 在游戏过程中,可以把“X”与其上、下、左、右四个方向之一的数字交换(如果存在)。 我们的目的是通过交换,使得网格变为如下排列(称为正确排列): 例如,示例中图形就可以通过让“X”先后与右、下、右三个方向的数字交...

正月看雪花的博客 3562

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

上一篇: POJ 3126 Prime Path
下一篇: ALU设计实验 alu_tb.v代码
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值