当前位置: 首页 > news >正文

U536193 八数码

第一个代码是本题做法

#include <bits/stdc++.h>
using namespace std;
const int N = 6, M = 110, INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
typedef long long int LL;
#define x first
#define y second
typedef pair<int, string> PIS;
int f(string start)
{int res = 0;for (int i = 0; i < start.size(); i++){if (start[i] != 'x'){auto t = start[i] - '1';res += abs(i / 3 - t / 3) + abs(i % 3 - t % 3);}}return res;
}
int bfs(string start)
{char op[] = "urdl";int dx[4] = {-1, 0, 1, 0};//代表上右下左四个方向。int dy[4] = {0, 1, 0, -1}; // 代表上右下左四个方向。string end = "12345678x";priority_queue<PIS, vector<PIS>, greater<PIS>> heap;unordered_map<string, pair<char, string>> pre;unordered_map<string, int> dist;dist[start] = 0;heap.push({f(start), start});while (heap.size()){auto t = heap.top();heap.pop();string state = t.y;if (state == end)break;int x, y;for (int i = 0; i < 9; i++){if (state[i] == 'x'){x = i / 3, y = i % 3;break;}}string source = state;for (int i = 0; i < 4; i++){int a = x + dx[i], b = y + dy[i];if (a < 0 || a >= 3 || b < 0 || b >= 3)continue;state = source;swap(state[a * 3 + b], state[x * 3 + y]);if (dist.count(state) == 0 || dist[state] > dist[source] + 1){dist[state] = dist[source] + 1;pre[state] = {op[i], source};heap.push({f(state) + dist[state], state});}}}return dist[end];
}
int main()
{ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);string start, end;int seq[8];int idx = 0;for (int i = 0; i < 9; i++){char x;cin >> x;start += x;if (x != 'x')seq[idx++] = x - '0';}int res = 0;for (int i = 0; i < 8; i++){for (int j = i + 1; j < 8; j++){if (seq[i] > seq[j])res++;}}if (res & 1)//如果逆序对为奇数的话,则无法到达目标状态。cout << -1 << endl;elsecout << bfs(start) << endl;return 0;
}

第二个代码是y总的记录方案做法

#include <bits/stdc++.h>
using namespace std;
const int N = 6, M = 110, INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
typedef long long int LL;
#define x first
#define y second
typedef pair<int, string> PIS;
int f(string start)
{int res = 0;for (int i = 0; i < start.size(); i++){if (start[i] != 'x'){auto t = start[i] - '1';res += abs(i / 3 - t / 3) + abs(i % 3 - t % 3);}}return res;
}
string bfs(string start)
{char op[] = "urdl";int dx[4] = {-1, 0, 1, 0};//代表上右下左四个方向。int dy[4] = {0, 1, 0, -1}; // 代表上右下左四个方向。string end = "12345678x";priority_queue<PIS, vector<PIS>, greater<PIS>> heap;unordered_map<string, pair<char, string>> pre;unordered_map<string, int> dist;dist[start] = 0;heap.push({f(start), start});while (heap.size()){auto t = heap.top();heap.pop();string state = t.y;if (state == end)break;int x, y;for (int i = 0; i < 9; i++){if (state[i] == 'x'){x = i / 3, y = i % 3;break;}}string source = state;for (int i = 0; i < 4; i++){int a = x + dx[i], b = y + dy[i];if (a < 0 || a >= 3 || b < 0 || b >= 3)continue;state = source;swap(state[a * 3 + b], state[x * 3 + y]);if (dist.count(state) == 0 || dist[state] > dist[source] + 1){dist[state] = dist[source] + 1;pre[state] = {op[i], source};heap.push({f(state) + dist[state], state});}}}string res;while (1){if (end == start)break;res += pre[end].x;end = pre[end].y;}reverse(res.begin(),res.end());return res;
}
int main()
{ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);string start, end;int seq[8];int idx = 0;for (int i = 0; i < 9; i++){char x;cin >> x;start += x;if (x != 'x')seq[idx++] = x - '0';}int res = 0;for (int i = 0; i < 8; i++){for (int j = i + 1; j < 8; j++){if (seq[i] > seq[j])res++;}}if (res & 1)//如果逆序对为奇数的话,则无法到达目标状态。cout << -1 << endl;elsecout << bfs(start) << endl;return 0;
}

第三个是P1379 八数码难题做法

#include <bits/stdc++.h>
using namespace std;
const int N = 6, M = 110, INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
typedef long long int LL;
#define x first
#define y second
typedef pair<int, string> PIS;
string start;
int f(string x)//这个代表估计函数。
{int res = 0;for (int i = 0; i < x.size(); i++){if (x[i] != '0'){int t = x[i] - '1';//t表示实际在九宫格中的位置。res += abs(i / 3 - t / 3) + abs(i % 3 - t % 3);}}return res;
}
int bfs()
{string end = "123804765";priority_queue<PIS, vector<PIS>, greater<PIS>> heap;unordered_map<string, int> dist;int dx[4] = {-1, 0, 1, 0};int dy[4] = {0, 1, 0, -1};heap.push({f(start), start});dist[start] = 0;while (heap.size()){auto t = heap.top();heap.pop();string state = t.y;if (state == end)break;int x = 0, y = 0;for (int i = 0; i < 9; i++){if (state[i] == '0'){x = i / 3;y = i % 3;break;}}string source = state;for (int i = 0; i < 4; i++){int a = x + dx[i], b = y + dy[i];if (a < 0 || a >= 3 || b < 0 || b >= 3)continue;state = source;swap(state[a * 3 + b], state[x * 3 + y]);if (dist.count(state) == 0 || dist[state] > dist[source] + 1){dist[state] = dist[source] + 1;heap.push({dist[state] + f(state), state});}}}return dist[end];
}
int main()
{ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);cin >> start;cout << bfs() << endl;return 0;
}

http://www.dtcms.com/a/451262.html

相关文章:

  • 常规FA的工业镜头有哪些?能做什么?
  • 自己做网站 需要会什么6vs做网站创建项目时选哪个
  • 济南seo网站优化古典网站建设欣赏
  • 网站标题如何书写财经网站建设方案
  • 建设一个国外服务器的网站新开传奇网站999新服网
  • 建设银行网站官网网址wordpress内容折叠
  • wordpress 网站 上传蚂蚁币是什么网站建设
  • 音乐网站开发开发好看的页面
  • 【PyTorch】基于YOLO的多目标检测(二)
  • 没有网站怎么做排名优化麻将app软件开发
  • 网站 后台 安装网站地图 设计
  • 网站开发做前端还是后端网站建设百科
  • 电子商务网站建设开题报告盗用网站模板
  • 音乐网站建设流程班级设计网站建设
  • 网站建设做的快营销型网站图片
  • 网站开发公司 网站空间能打开那种网站的手机浏览器
  • 玩具网站 下载设计师装修网
  • 建设银行网站背景wordpress点击文章不能进入
  • 四川煤矿基本建设工程公司网站外贸seo建站
  • 上海创意网站建设汽车零部件公司网站建设方案
  • 网站开发需要用到哪些资料一起做网站吧
  • wordpress做视频网站吗网站后台如何修改新闻发布时间
  • UNIX下C语言编程与实践40-UNIX 全局跳转:setjmp 与 longjmp 函数的使用与注意事项
  • 请网站建设的人多少钱网页特效梦工厂
  • 合肥网站忧化发帖网站百度收率高的
  • ORB_SLAM2原理及代码解析:Tracking::TrackWithMotionModel() 函数
  • 基于51单片机超声波测液位测距仪水位监测报警设计
  • 制作网站 优帮云德清县新巿镇城市建设网站
  • 武义县建设局网站首页子网站怎么建设
  • 湖州北京网站建设开发工具包