Simple search
1.DFS
UVA 548 树
1. You can use an array of ways to achieve binary tree node is still in the application of "dynamic static" idea, write newnode function
2. given binary tree preorder and postorder traversals, binary tree can be constructed, in accordance with post-order traversal method to find the root, and then find the roots in order traversal, in order to identify left and right subtrees of the list of nodes then recursively the left and right sub-tree structure
3. Note the template you enter here, will facilitate the use stringstream
#include<iostream> #include<string> #include<cmath> #include<cstring> #include<vector> #include<map> #include<set> #include<algorithm> #include<queue> #include<stack> #include<sstream> #include<cstdio> #define INF 0x3f3f3f3f const int maxn = 1e6 + 5; const double PI = acos(-1.0); typedef long long ll; using namespace std; const int maxv = 10000 + 10; int in_order[maxv], post_order[maxv], lch[maxv], rch[maxv]; int n; bool read_list(int* a) { string line; if (!getline(cin, line)) return false; stringstream ss(line); n = 0; int x; while (ss >> x) a[n++] = x; return n > 0; } int build(int L1, int R1, int L2, int R2) { if (L1 > R1) return 0; int root = post_order[R2]; int p = L1; while (in_order[p] != root) p++; int cnt = p - L1; lch[root] = build(L1, p - 1, L2, L2 + cnt - 1); rch[root] = build(p + 1, R1, L2 + cnt, R2 - 1); return root; } int best, best_sum; void dfs(int u, int sum) { sum += u; if (!lch[u] && !rch[u]) { if (sum < best_sum || (sum == best_sum && u < best)) { best = u; best_sum = sum; } } if (lch[u]) dfs(lch[u], sum); if (rch[u]) dfs(rch[u], sum); } int main() { while (read_list(in_order)) { read_list(post_order); build(0, n - 1, 0, n - 1); best_sum = 10000000; dfs(post_order[n - 1], 0); cout << best << "\n"; } return 0; }
2000 to improve the group -C words Solitaire
Connected with determines whether substr
#include<iostream> #include<string> #include<cmath> #include<cstring> #include<vector> #include<map> #include<set> #include<algorithm> #include<queue> #include<stack> #include<sstream> #include<cstdio> #define INF 0x3f3f3f3f const int maxn = 1e6 + 5; const double PI = acos(-1.0); typedef long long ll; using namespace std; string s[25]; int vis[25]; int Max=-1; int n; void dfs(string x, int len) { Max = max(Max, len); for (int i = 1; i <= n; i++) { if (vis[i] == 2) continue; int l1 = x.length(); int l2 = s[i].length(); int p = 1; while (p < min(l1, l2)) if (x.substr(l1 - p) == s[i].substr(0, p)) break; else p++; if (p < min(l1, l2)) { view [i] ++ ; dfs(x.substr(0,l1-p)+s[i], len + s[i].length()-p); view [i] - ; } } } int main () { std::ios::sync_with_stdio(0); cin.tie ( 0 ); cin >> n; for (int i = 1; i <= n; i++) cin >> s[i]; cin >> s[0]; s[0] = "0" + s[0]; dfs(s[0], s[0].length()); cout << Max-1; return 0; }
N Queens backtracking http://acm.hdu.edu.cn/showproblem.php?pid=2553
#include<iostream> #include<string> #include<cmath> #include<cstring> #include<vector> #include<map> #include<set> #include<algorithm> #include<queue> #include<stack> #include<sstream> #include<cstdio> #define INF 0x3f3f3f3f const int maxn = 1e6 + 5; const double PI = acos(-1.0); typedef long long ll; using namespace std; int X [ 15 ], Y [ 15 ]; // X [i] is the i-th row where columns int N1, ANS; bool place(int k) { for (int i = 1; i < k; i++) { if (abs(x[i] - x[k]) == abs(i - k) || x[i] == x[k]) return false; } return true; } // determine the position of the feasibility void DFS ( int a) { // iterate to a line, a total of n1 rows IF (a n1>) ANS ++; // been put n1 queens the else { for ( int I = . 1 ; I <= N1; I ++ ) { x[a] = i; if (place(a)) dfs(a + 1); } } } int main() { int n; for (int i = 1; i <= 10; i++) { years = 0 ; n1 = i; dfs(1); y [i] = years; } while (scanf("%d", &n), n) { printf("%d\n", y[n]); } return 0; }
BFS
BFS is usually implemented with queue, whether to open a visited flag array vis
Catch That Cow http://poj.org/problem?id=3278 as if DFS will TLE, consider DFS
#include<iostream> #include<string> #include<cmath> #include<cstring> #include<vector> #include<map> #include<set> #include<algorithm> #include<queue> #include<stack> #include<sstream> #include<cstdio> #define INF 0x3f3f3f3f const int maxn = 1e6 + 5; const double PI = acos(-1.0); 200 100N =intconst ; typedef long long ll; using namespace std; int n, k; struct Node { int x; int step; }; queue<Node> q; int vis[N]; void bfs() { int X, Step; while (!q.empty()) { Node Q = q.front(); q.pop(); X = Q.x; Step = Q.step; if (X == k) { printf("%d", Step); return; } if (X >= 1 && (!vis[X - 1])) { Node p; vis[X - 1] = true; p.x = X - 1; p.step = Step + 1; q.push(p); } if (X <= k && (!vis[X + 1])) { Node p; vis[X + 1] = true; p.x = X + 1; p.step = Step + 1; q.push(p); } if (X <= k && (!vis[X * 2])) { Node p; vis[X * 2] = true; p.x = X * 2; p.step = Step + 1; q.push(p); } } } int main () { scanf("%d%d", &n, &k); Node NODE; NODE.x = n; NODE.step = 0; q.push(NODE); bfs(); return 0; }
Backtracking is to find a general (or all) solution (or optimal solution in some sense) to meet the constraints, and general state space search is to find the path to a final state from the initial state to
Path-finding problem can be attributed to traverse the implicit graph, its task is to find an optimal path from initial state to a final state, rather than backtracking to find a solution that meets certain conditions.
Eight digital
#include<iostream> #include<map> #include<queue> using namespace std; queue<int>Q; Map < int , int > VIS; // tag array Map < int , int > STEP; // record the number of steps in this state int the dir [ . 4 ] [ 2 ] = {- . 1 , 0 , 0 , . 1 , . 1 , 0 , 0 , - . 1 }; // top, right, bottom, left int MAT [ . 3 ] [ . 3 ]; int State; // the initial state of the input int R & lt, C; void INPUT () // input data into a matrix and nine integer { int tmp = 0 ; for ( int I = 0 ; I < . 3 ; I ++ ) for ( int J = 0 ; J < . 3 ; J ++ ) { cin>>mat[i][j]; tmp=tmp*10+mat[i][j]; } state=tmp; } BOOL can_move ( int U, int D) // determines whether you can go { for ( int I = 2 ; I> = 0 ; i--) // integer matrix go back Analyzing { for ( int J = 2 ; J> = 0 ; J, ) { mat [i] [j] = u% 10 ; u/=10; if(mat[i][j]==0) { r=i; c = j; } } } // determines whether the four directions can go IF ((D == 0 && R & lt == 0 ) || (D == . 1 && C == 2 ) || (D == 2 && R & lt == 2 ) || (D = = . 3 && C == 0 )) return 0 ; return . 1 ; } int move_to ( int u, int D) // returned from a state of go u { int tmp = 0 ; int NR R & lt + the dir = [D] [ 0 ]; int NC = C + the dir [D] [ . 1 ] ; food [r] [c] = food [NR] [NC]; mat[nr][nc]=0; for(int i=0;i<3;i++) { for(int j=0;j<3;j++) tmp=tmp*10+mat[i][j]; } return tmp; } int bfs(int s) { Q.push(s); view [p] = 1 ; step[s]=0; while(Q.size()) { you and, v; u=Q.front(); Q.pop(); if(u==123456780) return step[u]; for(int i=0;i<4;i++) { if(can_move(u,i)) { v=move_to(u,i); if(!vis[v]) { to [v] = 1 ; step[v]=step[u]+1; Q.push(v); } } } } return -1; } int main () { input(); int ans=bfs(state); cout<<ans<<endl; return 0; }