1204 Word Puzzles

初学ac自动机,拿别人的代码作别模板修改的。

之前用query()查询的时候,查询到一个合适解就返回了,wa了n次,自己竟然犯这么低级错误,太不应该了 

这次看了别人的代码后受益良多,以此记载一下

 

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.c++代码
最新发布
12-10
八数码问题是一个经典的搜索问题,可通过广度优先搜索(BFS)算法来解决。下面是一个使用C++实现的八数码问题求解程序: ```cpp #include <iostream> #include <queue> #include <unordered_map> #include <string> #include <vector> #include <algorithm> using namespace std; // 定义方向数组,分别表示右、左、上、下 const int dx[4] = {0, 0, -1, 1}; const int dy[4] = {1, -1, 0, 0}; const char dir[4] = {'r', 'l', 'u', 'd'}; // 目标状态 const string target = "12345678x"; // 交换字符串中两个位置的字符 string swapChars(string s, int i, int j) { swap(s[i], s[j]); return s; } // BFS求解八数码问题 string bfs(string start) { queue<pair<string, string>> q; unordered_map<string, bool> visited; q.push({start, ""}); visited[start] = true; while (!q.empty()) { auto [state, path] = q.front(); q.pop(); if (state == target) { return path; } // 找到空格的位置 int x = state.find('x'); int r = x / 3, c = x % 3; for (int i = 0; i < 4; ++i) { int nr = r + dx[i], nc = c + dy[i]; if (nr >= 0 && nr < 3 && nc >= 0 && nc < 3) { int nx = nr * 3 + nc; string nextState = swapChars(state, x, nx); if (!visited[nextState]) { visited[nextState] = true; q.push({nextState, path + dir[i]}); } } } } return "unsolvable"; } int main() { string start; char c; for (int i = 0; i < 9; ++i) { cin >> c; start += c; } string result = bfs(start); cout << result << endl; return 0; } ``` ### 代码解释 1. **输入处理**:程序从标准输入读取初始状态,将其存储为一个字符串。 2. **广度优先搜索(BFS)**:使用队列来实现BFS,存储待探索的状态和对应的移动路径。 3. **状态表示**:使用字符串来表示八数码的状态,空格用`x`表示。 4. **方向移动**:通过方向数组`dx`和`dy`来表示右、左、上、下四个方向的移动。 5. **结果输出**:如果找到目标状态,输出移动路径;否则输出`unsolvable`。 ### 复杂度分析 - **时间复杂度**:$O(9!)$,因为八数码问题的状态总数为$9!$。 - **空间复杂度**:$O(9!)$,主要用于存储已访问的状态。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值