几道启发式搜索问题 soj2831
2012-08-31 21:43
127 查看
先mark一下。
先附个A*的代码
注意一下 判重的 问题
先附个A*的代码
注意一下 判重的 问题
#include <string.h> #include <stdio.h> #include <ctype.h> #include <algorithm> #include <queue> #include <string.h> #include <set> using namespace std; const int maxn = 32 ; int n ; struct node { int depth ; int h ; char array[20] ; int operator < (const node& obj) const { return depth+h > obj.depth+obj.h ; } }start; inline bool get(int &t) { bool flag = 0 ; char c; while(!isdigit(c = getchar())&&c!='-') if( c == -1 ) break ; if( c == -1 ) return 0 ; if(c=='-') flag = 1 , t = 0 ; else t = c ^ 48; while(isdigit(c = getchar())) t = (t << 1) + (t << 3) + (c ^ 48) ; if(flag) t = -t ; return 1 ; } inline int abs(int x) { return x > 0 ? x : -x; } int h_(node& a) { int i , ans = a.array[0] != 1 ; for( i = 1 ; i < n ; i++) ans += abs(a.array[i]-a.array[i-1]) != 1 ; a.h = ans ; return ans ; } struct cmp { bool operator()(const node &a,const node &b) const { int i ; for( i = 0 ; i < n ; i++) if( a.array[i] != b.array[i] ) break; return i < n ; } }; void solve() { priority_queue<node> q ; set<node,cmp> st ; h_(start); start.depth = 0 ; q.push(start); while (!q.empty()) { node sth = q.top() ; q.pop(); if( st.find(sth) != st.end() ) continue; st.insert(sth); if(sth.h==0) { printf("%d\n",sth.depth); return; } int i , j ; j = sth.depth+1; for( i = 0 ; i < n - 1 ; i++) { /*reverse[i,n-1]*/ node ss = sth ; reverse(ss.array+i,ss.array+n); ss.depth = j ; h_(ss); q.push(ss); } } } int main() { int i , j ; while (get(n)&&n) { for( i = 0 ; i < n ; i++) { get(j); start.array[i]=j; } solve(); } }
相关文章推荐
- 贪心算法::启发式搜索解决马跳棋盘问题
- 优化算法 | N皇后问题启发式算法局部搜索
- UOJ#9 浅谈在线仙人球嵌套动态网络路径剖分优化的分支定界贪心剪枝启发式迭代加深人工智能搜索决策算法解决问题
- Sicily1153-马的周游问题:启发式搜索
- 启发式搜索程序设计-八数码问题
- 使用ActionScript3基于Flex实现八数码问题启发式搜索
- 「8-Queens Problem」皇后问题局部极值启发式搜索方法
- hdu 1151 - > 双向路径搜索解决覆盖问题
- 机试算法讲解: 第41题 广度优先搜索之迷宫问题
- 2812 恼人的青蛙(暴力搜索时根据问题优化判断条件以加快搜索速度)
- [原创]2天的搜索经验对IIS的问题的全面解决方案
- P11: 背包问题的搜索解法
- 关于实现搜索、排序问题
- nyoj 部分和问题(深度优先搜索DFS)
- android ICS2 google帐户的联系人无法搜索出来问题
- POJ 1321棋盘问题(简单搜索)
- C语言通过深度优先搜索来解电梯问题和N皇后问题的示例
- 迷宫问题---深度搜索(dfs)
- 二分搜索问题(未理解)
- 今天分公司的人遇到监听方面的问题,从网上搜索方法解决了备注一下