UVA1533-Moving Pegs(BFS+状态压缩)

本文详细解析了ProblemUVA1533-MovingPegs问题,通过二进制存储状态的方法,使用BFS算法在三角形地图上寻找最短的跳棋移动路径。代码中包含了一个预处理的临接表,用于记录每个点能够到达的点,以及具体的实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem UVA1533-Moving Pegs

Accept:106  Submit:375

Time Limit: 3000 mSec

 Problem Description

 

 

 Input

The input consists of T test cases. The number of test cases (T) is given in the first line of the input file. Each test case is a single integer which means an empty hole number.

 

 Output

For each test case, the first line of the output file contains an integer which is the number of jumps in a shortest sequence of moving pegs. In the second line of the output file, print a sequence of peg movements. Apegmovementconsistsofapairofintegersseparatedbyaspace. Thefirstintegerofthe pair denotes the hole number of the peg that is moving, and the second integer denotes a destination (empty) hole number.

 

 Sample Input

1
5
 
 

 Sample Ouput

10

12 5 3 8 15 12 6 13 7 9 1 7 10 8 7 9 11 14 14 5

 

题解:15个洞,二进制存储状态是比较正的思路。接下来就是水题了,只不过是把矩形地图换成了三角形地图,预处理一个临接表,存一下对于每个点能到哪些点。因为要字典序,因此顺序很重要,稍加分析就知道周围6个位置的大小关系,注意对于15个记录相邻点的数组,一定要统一顺序,除了字典序,还因为有可能要顺着一个方向走几格,这时顺序一致就很方便。

 

  1 #include <bits/stdc++.h>
  2 
  3 using namespace std;
  4 
  5 const int maxn = 15, maxm = 6;
  6 const int dir[maxn][maxm] = 
  7 {    
  8     {-1,-1,-1,-1, 1, 2},  {-1, 0,-1, 2, 3, 4},  { 0,-1, 1,-1, 4, 5},  {-1, 1,-1, 4, 6, 7},
  9     { 1, 2, 3, 5, 7, 8},  { 2,-1, 4,-1, 8, 9},  {-1, 3,-1, 7,10,11},  { 3, 4, 6, 8,11,12},
 10     { 4, 5, 7, 9,12,13},  { 5,-1, 8,-1,13,14},  {-1, 6,-1,11,-1,-1},  { 6, 7,10,12,-1,-1},
 11     { 7, 8,11,13,-1,-1},  { 8, 9,12,14,-1,-1},  { 9,-1,13,-1,-1,-1} 
 12 };
 13 
 14 
 15 int s;
 16 bool vis[1 << maxn];
 17 pair<int, int> path[1 << maxn];
 18 int pre[1 << maxn];
 19 
 20 struct Node {
 21     int sit, time;
 22     int pos;
 23     Node(int sit = 0, int time = 0, int pos = 0) :
 24         sit(sit), time(time), pos(pos) {}
 25 };
 26 
 27 int bfs(int &p) {
 28     int cnt = 0;
 29     int ori = (1 << maxn) - 1;
 30     ori ^= (1 << s);
 31     queue<Node> que;
 32     que.push(Node(ori, 0, 0));
 33     vis[ori] = true;
 34     while (!que.empty()) {
 35         Node first = que.front();
 36         que.pop();
 37         if (first.sit == (1 << s)) {
 38             p = first.pos;
 39             return first.time;
 40         }
 41 
 42         int ssit = first.sit;
 43         for (int i = 0; i < maxn; i++) {
 44             if (!(ssit&(1 << i))) continue;
 45 
 46             for (int j = 0; j < maxm; j++) {
 47                 int Next = dir[i][j];
 48                 if (Next == -1 || !(ssit&(1 << Next))) continue;
 49 
 50                 int tmp = ssit ^ (1 << i);
 51                 while (Next != -1) {
 52                     if (!(ssit&(1 << Next))) {
 53                         //printf("%d %d\n",i, Next);
 54                         tmp ^= (1 << Next);
 55                         if (!vis[tmp]) {
 56                             Node temp(tmp, first.time + 1, ++cnt);
 57                             pre[cnt] = first.pos;
 58                             path[cnt] = make_pair(i, Next);
 59                             que.push(temp);
 60                             vis[tmp] = true;
 61                         }
 62                         break;
 63                     }
 64                     tmp ^= (1 << Next);
 65                     Next = dir[Next][j];
 66                 }
 67             }
 68         }
 69     }
 70     return -1;
 71 }
 72 
 73 void output(int pos) {
 74     if (!pre[pos]) {
 75         printf("%d %d", path[pos].first + 1, path[pos].second + 1);
 76         return;
 77     }
 78     output(pre[pos]);
 79     printf(" %d %d", path[pos].first + 1, path[pos].second + 1);
 80 }
 81 
 82 int main()
 83 {
 84     int iCase;
 85     scanf("%d", &iCase);
 86     while (iCase--) {
 87         scanf("%d", &s);
 88         s--;
 89         memset(vis, false, sizeof(vis));
 90         memset(pre, -1, sizeof(pre));
 91         int pos;
 92         int ans = bfs(pos);
 93         if (ans == -1) {
 94             printf("IMPOSSIBLE\n");
 95         }
 96         else {
 97             printf("%d\n", ans);
 98             output(pos);
 99             printf("\n");
100         }
101     }
102     return 0;
103 }

 

转载于:https://www.cnblogs.com/npugen/p/9600814.html

内容概要:本文从关键概念、核心技巧、应用场景、代码案例分析及未来发展趋势五个维度探讨了Python编程语言的进阶之路。关键概念涵盖装饰器、生成器、上下文管理器、元类和异步编程,这些概念有助于开发者突破基础认知的核心壁垒。核心技巧方面,介绍了内存优化、性能加速、代码复用和异步处理的方法,例如使用生成器处理大数据流、numba库加速计算密集型任务等。应用场景展示了Python在大数据处理、Web开发、人工智能和自动化运维等多个领域的广泛运用,特别是在FastAPI框架中构建异步API服务的实战案例,详细分析了装饰器日志记录、异步数据库查询和性能优化技巧。最后展望了Python的未来发展趋势,包括异步编程的普及、类型提示的强化、AI框架的深度整合以及多语言协同。 适合人群:已经掌握Python基础语法,希望进一步提升编程技能的开发者,特别是有意向从事数据科学、Web开发或AI相关工作的技术人员。 使用场景及目标:①掌握Python进阶概念和技术,如装饰器、生成器、异步编程等,提升代码质量和效率;②学习如何在实际项目中应用这些技术,如通过FastAPI构建高效的异步API服务;③了解Python在未来编程领域的潜在发展方向,为职业规划提供参考。 阅读建议:本文不仅提供了理论知识,还包含了丰富的实战案例,建议读者在学习过程中结合实际项目进行练习,特别是尝试构建自己的异步API服务,并通过调试代码加深理解。同时关注Python社区的发展动态,及时掌握最新的技术和工具。
内容概要:本文档《Rust系统编程实战》详细介绍了Rust在系统编程领域的应用,强调了其内存安全、零成本抽象和高性能的特点。文档分为三个主要部分:核心实战方向、典型项目案例和技术关键点。在核心实战方向中,重点讲解了unsafe编程、FFI(外部函数接口)和底层API调用,涉及操作系统组件开发、网络编程、设备驱动开发、系统工具开发和嵌入式开发等多个领域,并列出了每个方向所需的技术栈和前置知识。典型项目案例部分以Linux字符设备驱动为例,详细描述了从环境搭建到核心代码实现的具体步骤,包括使用bindgen生成Linux内核API的Rust绑定,定义设备结构体,以及实现驱动核心函数。 适合人群:对系统编程有兴趣并有一定编程基础的开发者,尤其是那些希望深入了解操作系统底层机制、网络协议栈或嵌入式系统的工程师。 使用场景及目标:①掌握Rust在不同系统编程场景下的应用,如操作系统组件开发、网络编程、设备驱动开发等;②通过实际项目(如Linux字符设备驱动)的学习,理解Rust与操作系统内核的交互逻辑;③提高对unsafe编程、FFI和底层API调用的理解和运用能力。 阅读建议:由于文档内容较为深入且涉及多个复杂概念,建议读者在学习过程中结合实际操作进行练习,特别是在尝试实现Linux字符设备驱动时,务必按照文档提供的步骤逐步进行,并多加调试和测试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值