UVA 10085 The most distant state

【MNE脑电信号处理学习笔记1】数据的读取、可视化、剪裁、滤波和保存 MNE之读取原始数据、可视化数据、剪裁数据、滤波和保存 Reading & visualizaing raw data, cropping, filtering, and saving 原视频教程:Pybrain: M/EEG analysis with MNE Python 原笔记和材料:pybrain_mne 本文根据视频和个人理解对原笔记进行细微修改。 Import the few packages we’ll need import matplotlib import pathlib i 阅读详情

分析

八数码,规则只能在3×3的棋盘里移动棋子,棋子只能移到空位。
本题需要求的是在移动的棋盘不重复的情况下可以移动的方式以及棋面。

这是一题隐式图搜索。
给定一个棋面状态,向空白处移动,可以视为空白处向四个方向移动。这样以空白处为研究对象,搜路,直到搜遍整个棋盘。可以发现,相对这题更合适bfs,广度优先搜索而不是深度优先搜索,因为题目所需要的只是最后一个可达状态。

下面简要的提出思路。

从队列中取出一个状态
求空白处
将空白处向四个方向移动得到新的状态
   遍历状态无重复,压入队列,记录路径(父节点)

如何储存状态?这里的棋盘是3×3,开辟一个3×3的数组,其实也是长度为9的数,因为每一个棋盘都是独一无二。这里再加以可以使用hash法,将数排列。

这里使用了链表来存储。打印路径时,利用父节点递归到根节点再打印即可。

代码

#include <cstdio>
#include <cstring>
#define MAX 1000003
typedef int State[9];
State st[MAX];
int front, rear, fa[MAX], path[MAX], head[MAX], next[MAX];
int dir[][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
char cdir[] = "UDLR";

void print(int c) { if (c > 1) { print(fa[c]); printf("%c", cdir[path[c]]); } }

bool insert(int s)
{
    int h = 0; for (int i = 0; i < 9; i++) h = h * 10 + st[s][i]; h %= MAX;
    int u = head[h];
    while (u) {
        if (memcmp(st[u], st[s], sizeof(st[s])) == 0) return false;
        u = next[u];
    }
    next[s] = head[h];
    head[h] = s;
    return true;
}

void bfs()
{
    front = 1, rear = 2;
    while (front < rear) {
        State &s = st[front];
        int x, y, z;
        for (z = 0; z < 9; z++) if (!s[z]) break;
        x = z / 3, y = z % 3;
        for (int d = 0; d < 4; d++) {
            int nx = x + dir[d][0];
            int ny = y + dir[d][1];
            int nz = nx * 3 + ny;
            if (nx >= 0 && nx < 3 && ny >= 0 && ny < 3) {
                State &t = st[rear];
                memcpy(&t, &s, sizeof(s));
                t[nz] = s[z]; t[z] = s[nz];
                if (insert(rear)) { fa[rear] = front; path[rear] = d; rear++; }
            }
        }
        front++;
    }
}

void solve()
{
    memset(head, 0, sizeof(head));
    bfs();
    for (int i = 0; i < 9; i++) {
        printf("%d", st[rear-1][i]);
        if ((i+1) % 3) printf(" ");
        else           printf("\n");
    }
    print(rear-1);
    printf("\n\n");
}

int main()
{
    int T, cas = 0;
    scanf("%d", &T);
    while (T--) {
        for (int i = 0; i < 9; i++) scanf("%d", &st[1][i]);
        printf("Puzzle #%d\n", ++cas);
        solve();
    }
}

题目

Description

The 8-puzzle is a square tray in which eight square tiles are placed. The remaining ninth square is uncovered. Each tile has a number on it. A tile that is adjacent to the blank space can be slid into that space. A game consists of a starting state and a specified goal state. The starting state can be transformed into the goal state by sliding (moving) the tiles around. The 8-puzzle problem asks you to do the transformation in minimum number of moves.

283123
164=>84
7 5765
StartGoal

However, our current problem is a bit different. In this problem, given an initial state of the puzzle your are asked to discover a goal state which is the most distant (in terms of number of moves) of all the states reachable from the given state.

Input

The first line of the input file contains an integer representing the number of test cases to follow. A blank line follows this line.

Each test case consists of 3 lines of 3 integers each representing the initial state of the puzzle. The blank space is represented by a 0 (zero). A blank line follows each test case.

Output

For each test case first output the puzzle number. The next 3 lines will contain 3 integers each representing one of the most distant states reachable from the given state. The next line will contain the shortest sequence of moves that will transform the given state to that state. The move is actually the movement of the blank space represented by four directions: U (Up), L (Left), D (Down) and R (Right).

After each test case output an empty line.

Sample Input

1
2 6 4
1 3 7
0 5 8

Sample Output

Puzzle #1
8 1 5
7 3 6
4 0 2
UURDDRULLURRDLLDRRULULDDRUULDDR

Windowns下Git环境配置-命令行方式 文章目录一、配置Git1.1 下载Git并安装1.2 配置本地Git用户名和邮箱1.3 注册GitHub二、生成本地公钥,放到GitHub上-SSH Key2.1 生成本地公钥命令2.2 查看生成的公钥2.3 在GitHub放上公钥并测试连通性三、创建项目实操四、可能出现的问题4.1.push不成功4.2.提示SSL的问题4.3.域名不能解析的问题4.4.如果github连接不上的问题 一、配置Git 1.1 下载Git并安装 Git官网 默认安装,根据需要修改安装路径 1.2 配置本地Git用户名和邮箱 阅读详情

相关推荐

解决SolidWorks启动报错问题:SolidWorks2020显示无法获得下列许可 SolidWorks standard 期望的SolidWorks监控程序已经关闭(-97,121,0)

解决SolidWorks启动报错问题:SolidWorks2020显示无法获得下列许可 SolidWorks standard 期望的SolidWorks监控程序已经关闭(-97,121,0) 问题描述 打开SolidWorks2020时,显示SolidWorks2020显示无法获得下列许可 SolidWorks standard 期望的SolidWorks监控程序已经关闭(-97,121,0) (急需看模型的我心里万马奔腾…) 截图如下: 有问题咱就上 看了其他博主的回答,自己尝试了几个,发现出现了新的

qq_44606358的博客 7万+

uva 10085 The most distant state

uva 10085 The most distant state 广搜 哈希 康拓展开

@fei 770

RK3568-MIPI屏幕调试实战:LCD初始化时序解析与转换

本文详细解析RK3568平台MIPI屏幕调试中的LCD初始化时序,重点介绍DCS Write与Generic Write数据类型的转换方法,并提供厂家时序到RK格式的实战转换案例。通过理解上电初始化时序的重要性,帮助开发者快速解决屏幕不亮、花屏等常见调试问题,提升RK3568硬件开发效率。

d6e7f8g9h的博客 186

uva:10085 - The most distant state

10085 - The most distant state 题目大意:就和八码数问题类似,只是题目没有给最终的状态,要求你自己写出最终的状态,并且给出最短的路径。 解题思路:和八码数的解题思路是相同的,只是不给最终的状态,而是让它自己去bfs()直到最后已经不能在走的地步(再走下去就会重复的情况),这里的判重用了哈希判重。 然后就是存储路径问题:开了一个数组dis【】用来存

882

QGIS效率提升神器:qgis_mcp插件+VSCode联合工作流配置全解析

本文详细解析了如何通过配置qgis_mcp插件与VSCode的联合工作流,实现语音控制和代码自动化来大幅提升QGIS操作效率。该方案将自然语言交互与现代编辑器生态结合,为地理信息专业人士构建了智能、高效的全新工作模式,彻底改变传统GIS操作方式。

weixin_29228203的博客 191

如何找回mysql删除的表_mysql误删表如何恢复,恢复被删除表的步骤说明

小编寄语想必大家都知道,Oracle ACE李真旭(Roger)是国内最专业的Oracle 数据库恢复专家。但知识都是触类旁通,真正的专家,从来不会局限在一个方向上。今天分享的内容,是他在MySQL数据恢复上所做的尝试。本文主要分享在没有备份的情况下,MySQL数据库如何恢复被删除的表。包含两个主要的场景:1、drop table后的恢复2、truncate table后的恢复正文:我们都知道,M...

weixin_35725389的博客 6784

uva 10085 - The most distant state

Problem A The Most Distant State Input: standard input Output: standard output   The 8-puzzle is a square tray in which eight square tiles are placed. The remaining ninth square is uncovered. E

uva code 1275

经典损失函数——均方误差(MSE)和交叉熵误差(CEE)的python实现

损失函数(loss function)用来表示当前的神经网络对训练数据不拟合的程度。这个损失函数有很多,但是一般使用均方误差和交叉熵误差等。 1.均方误差(mean squared error) 先来看一下表达式: 用于将的求导结果变成,是神经网络的输出,是训练数据的标签值,k表示数据的维度。 用python实现: def MS...

Answer3664的博客 1万+

UVA The most distant state

题目如下: Problem A The Most Distant State Input: standard input Output: standard output   The 8-puzzle is a square tray inwhich eight square tiles are placed. The remaining ninth square is uncove

hexiecs的技术专栏 947

一文吃透 Tools for Applying SAP Notes:从 SNOTE、TCI 到 Solution Manager 与 SAP Cloud ALM

本文系统梳理了SAP技术与运维团队应用SAP Notes的高效工具与方法,涵盖On-Premise与云环境。文章首先阐述了精研SAP Notes工具的必要性,指出其是实施错误修复、法务变更和性能改进的关键载体。随后详细介绍了SNOTE、TCI、Solution Manager、Cloud ALM和Maintenance Planner等核心工具的定位与适用场景,并通过采购异常案例展示了标准更正流程。针对复杂修复场景,文章提出了TCI的批量处理方案,并强调将Note实施纳入变更治理流程的重要性。最后总结了三种

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 354

8、音频运行时系统的架构设计

本文深入探讨了音频运行时系统的分层设计及其优势,包括逻辑层和物理层的功能划分。通过分层设计,开发者可以更轻松地管理和优化音频资源,确保其在不同平台上的高效运行。文章还介绍了平台适配与优化、错误处理与调试以及未来发展方向等内容,为开发者提供了宝贵的参考和指导。

juice的博客 463

UVa 10085 - The most distant state

八数码问题的变形,bfs+哈希,打印路径时花了点时间,用哈希表next数组的原理进行回退就可以将问题解决。 代码如下: #include #include #include #include #include #include using namespace std; const int MAXSIZE = 1000003; const int dx[4] = {-1, 1, 0

GooMaple 924

微搭低代码实现用户登录及注册功能

在小程序开发中,我们可以拿到微信用户的openid,openid可以作为用户的唯一标识,在查询数据的时候可以进行数据过滤实现查询自己提交的数据。那微搭中是否也有openid这个概念,可以获取用户自己提交的数据呢? 答案是肯定的,那要怎么去做呢?思路是先创建一个数据源方法,在数据源中返回openid,然后低代码中可以调用数据源的方法并且赋值给全局变量,这样在后续的业务中从全局变量就可以获取openid了,方便做数据权限的控制。 为了实现这个目的,我们从创建数据源开始。 创建数据源 我们先创建一个用户管理的数据

低代码布道师的博客 3000

uva 10085 The most distant state

没有什么好的办法,广搜遍历状态树,最后一个状态就是答案,运行都3s多了居然还可以过,这道题好像没什么意义啊,没看出这道题的价值在哪。 #include #include #include #include using namespace std; struct state { int blank_i, blank_j; char m[4][4]; int step; cha

皓首不倦 不负少年 794

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

Camera2Demo

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

uva 10085-The most distant state

//死的暴力搜索,解答树最深层即解答,所以直接搜索到最深处,最后一个节点即答案,当然效率也不好,想不到啥好方法,实现上和上面的隐式图搜索是差不多的 #include #include //using namespace std; #define MAX_SIZE 1000000 #define MAX_STATE 362880 #define L 0 #define R 1 #define

无题 801

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

uva 10085 The most distant state

此题就是状态搜索题;使用bfs来获得最远的那个状态,然后打印出来,中间记录一下父节点就可以了。 此题需要注意到的技巧: 1.使用数组代替栈 2.hash函数的使用,将每个状态用整数表示。 3.memcpy整块内存的复制,memcmp整块内存的比较。 // // main.cpp // uva10085 The Most Distant State // // Cre

最爱她! 361

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

uva10085 - The most distant state

bfs  +  哈希技术,,, 超经典,超高效代码::: #include #include #define MAX 1000000 int ans, st[MAX][9], head[MAX], next[MAX], posi[MAX], fa[MAX], path[MAX]; int dx[4] = {-1,1,0,0}, dy[4] = {0,0,-1,1}; char dir[

稻草人 617

UVa 10085 - The Most Distant State

传送门UVa 10085 - The Most Distant State

一只冰熊 910

UVA 10085 - The most distant state

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=110&page=show_problem&problem=1026 类型: 隐式图搜索 原题: The 8-puzzle is a square tray in which eight square t

D_Double's Journey 1583

UVA - 10085 The most distant state

题目大意:求所给图达到最远状态的时候,移动最少次的移动顺序。 解题思路:bfs+判重。定义一个结构体存放当前图,空白位置的坐标,移动次数即移动到该图的步骤。每改变一次就判断,然后放入队列。 依旧是参考别人题解的咸鱼。#include<iostream> #include<cstdio> #include<cmath> #include<string.h> #include<stdlib.h>

life kicks in right now 314

uva 10085 The most distant state(BFS + HASH)

uva 10085 The most distant state The 8-puzzle is a square tray in which eight square tiles are placed. The remaining ninth square is uncovered. Each tile has a number on it. A tile that is

总有那么一群人喜欢盯着我的博客看,估计这是他们见过最长的博客名,我必须得告诉他们博客名最长可以写多长 687

uva10085 The most distant state(BFS)

Problem A The Most Distant State Input: standard input Output: standard output   The 8-puzzle is a square tray in which eight square tiles are placed. The remaining ninth square is uncovered. Eac

WONDERZY 925

UVa10085 - The most distant state

#include #include #define N 3 #define MAX 3000000 #define HASHSIZE 1000003 #define DIRSIZE 100 typedef struct node { int x, y, step; int a[N][N]; char dir[DIRSIZE]; }node; node queue[M

多看多听多总结 818

uva10085 The most distant state

基本功不够导致浪费了很多时间啊。。。 开始没想清楚,傻乎乎的用dfs结果运行

cgjiayou 445
上一篇: UVA 10422 Knights in FEN
下一篇: UVA 12097 Pie
lab103
博客等级 码龄11年 5粉丝 78原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值