soj 1110 15数码 IDA*
来源:互联网 发布:淘宝网顾老师太极鞋 编辑:程序博客网 时间:2024/05/24 06:19
A*估价函数是曼哈顿距离和
/**********************jibancanyang************************** *Author* :jibancanyang *Created Time* : 二 6/21 20:44:55 2016**Problem**:**Analyse**:**Get**:**Code**:***********************1599664856@qq.com**********************/#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <cmath>#include <cstdlib>#include <ctime>#include <stack>using namespace std;typedef pair<int, int> pii;typedef long long ll;typedef unsigned long long ull;typedef vector<int> vi;#define pr(x) cout << #x << ": " << x << " " #define pl(x) cout << #x << ": " << x << endl;#define pri(a) printf("%d\n",(a))#define xx first#define yy second#define sa(n) scanf("%d", &(n))#define sal(n) scanf("%lld", &(n))#define sai(n) scanf("%I64d", &(n))#define vep(c) for(vector<int>::iterator it = (c).begin(); it != (c).end(); it++) const int mod = int(1e9) + 7, INF = 0x3f3f3f3f;const int maxn = 1e5 + 13;int G[4][4];int dirx[] = {-1, 0, 1, 0};int diry[] = {0, 1, 0, -1};int aims[4][4] = { {1, 2, 3, 4} , {5, 6, 7, 8} , {9, 10, 11, 12} , {13, 14, 15, 0}};int aim;bool ans;pii mp[16];inline int abss(int y) { return y > 0 ? y : -y;}int star() { int sum = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (G[i][j]) sum += abss(mp[G[i][j]].xx - i) + abss(mp[G[i][j]].yy - j); } } return sum;}inline void swaps(int &a, int &b) { a ^= b; b ^= a; a ^= b;}void dfs(int res, int emptyx, int emptyy, pii pre, int stars) { if (ans) return; int k = stars; if (res == 0) { if (!k) ans = true; return; } if (res < k) return; for (int i = 0; i < 4; i++) { int nxtx = emptyx + dirx[i], nxty = emptyy + diry[i]; if (nxtx >= 0 && nxtx < 4 && nxty >= 0 && nxty < 4 && !(nxtx == pre.xx && nxty == pre.yy)) { int temp = stars; temp -= abss(mp[G[nxtx][nxty]].xx - nxtx) + abss(mp[G[nxtx][nxty]].yy - nxty); temp += abss(mp[G[nxtx][nxty]].xx - emptyx) + abss(mp[G[nxtx][nxty]].yy - emptyy); swaps(G[emptyx][emptyy], G[nxtx][nxty]); dfs(res - 1, nxtx, nxty, pii(emptyx, emptyy), temp); swaps(G[emptyx][emptyy], G[nxtx][nxty]); } }}int main(void){#ifdef LOCAL freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout);#endif int n; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { mp[aims[i][j]] = pii(i, j); } } sa(n); while (n--) { int x, y; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { sa(G[i][j]); if (!G[i][j]) x = i, y = j; } } ans = false; int tt = star(); for (int i = tt; ; i++) { dfs(i, x, y, pii(-1, -1), tt); if (ans) { pri(i); break; } } } return 0;}
0 0
- soj 1110 15数码 IDA*
- uva 10181 - 15-Puzzle Problem 十五数码 IDA*
- poj 1077 八数码 ida*
- 用IDA*算法求解八数码问题
- PKU-1077 Eight (八数码之IDA*)
- 八数码 poj1077 Eight(A*、IDA*)
- POJ 1077 八数码-第一道IDA*
- wikioi 1225 八数码难题 IDA*
- IDA* 解八数码问题: POJ 1077
- IDA*算法解决十五数码问题
- IDA*-洛谷P1379 八数码难题
- IDA*——Luogu1379 八数码难题
- 八数码与IDA*问题 HDU1043&&POJ1077
- 趣味编程:用IDA*算法求解八数码问题
- 八数码问题 IDA*算法+曼哈顿距离 poj 1077
- IDA*(迭代加深的A*算法) 八数码
- poj1077八数码问题——境界六(IDA*)
- 八数码 poj 1077 广搜 A* IDA*
- Android开发之EditText属性详解
- main函数执行前后--全局构造和atexit
- Masonry和FDTemplateLayoutCell搭配使用「UITableview自适应内容高度」
- Balanced Number
- android-Making Applications Accessible
- soj 1110 15数码 IDA*
- AVCodecContext结构体
- CocoaPods错误:fatal: unable to access ''SSLRead() return和error: RPC failed; result=56, HTTP code = 200
- 用nginx搭建流媒体服务器
- RxJava入门使用
- 使用DBUtils为MySQL实现连接池功能
- 常用接口介绍04——232
- js 执行上下文环境
- Activity的生命周期