poj 初期基本搜索
2017-01-19 10:37
706 查看
第四个专题了,初期基本搜索:
都是水题,两天完全可以刷完。。。
(1)、深度优先搜索
1、poj2488
题意:给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径。
分析:爆搜。。。
#include <iostream> #include <cstring> using namespace std; const int N = 35; int vis ; int p, q, flag; char b[500]; int dir[8][2] = {-1, -2, 1, -2, -2, -1, 2, -1, -2, 1, 2, 1, -1, 2, 1, 2}; void dfs(int x, int y, int cnt) { if (cnt == p*q) { cout << "A1"; for (int i = 2; i < 2*cnt; i++) cout << b[i]; cout << endl << endl; flag = 1; return; } for (int i = 0; i < 8 && !flag; i++) { int tx = x + dir[i][0], ty = y + dir[i][1]; if (tx < 1 || tx > p || ty < 1 || ty > q) continue; if (!vis[tx][ty]) { vis[tx][ty] = 1; b[2*cnt] = 'A' + ty - 1; b[2*cnt+1] = '1' + tx - 1; dfs(tx, ty, cnt+1); vis[tx][ty] = 0; } } } int main() { int t, ca = 0; cin >> t; while (t--) { cin >> p >> q; cout << "Scenario #" << ++ca << ':' << endl; memset(vis, 0, sizeof(vis)); flag = 0; vis[1][1] = 1; dfs(1, 1, 1); if (!flag) cout << "impossible" << endl << endl; } return 0; }
2、poj3083
题意:给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走先输出左转优先时,从S到E的步数,再输出右转优先时,从S到E的步数,最后输出S到E的最短步数
分析:dfs找左转和右转步数,bfs找最短路。
#include <cstdio> #include <cstring> int w, h, c, a; int Sx, Sy, Ex, Ey; int cnt, flag, tag; int dir[4][2] = {0, -1, -1, 0, 0, 1, 1, 0}; char maze[50][50]; int vis[50][50]; int que[2500][2]; void DFS(int x, int y, int cnt) { if (maze[x][y] == 'E') { printf("%d ", cnt); tag = 0; return ; } if (tag) for (int i = a+c, j = 0; j < 4; j++, i -= c) { if (i < 0) i = 3; if (i > 3) i = 0; int tx = x + dir[i][0]; int ty = y + dir[i][1]; if (tx > h || tx < 1 || ty > w || ty < 1) continue; if (tag && !vis[tx][ty] && maze[tx][ty] != '#') { a = i; DFS(tx, ty, cnt+1); } } } void BFS() { int fir = 0, sec = 0; que[sec][0] = Sx; que[sec++][1] = Sy; int step = 1; while(fir < sec && !vis[Ex][Ey]) { int tmp = sec; step++; while(fir < tmp && !vis[Ex][Ey]) { int x = que[fir][0]; int y = que[fir++][1]; for(int i = 0; i < 4; i++) { int tx = x + dir[i][0]; int ty = y + dir[i][1]; if (tx > h || tx < 1 || ty > w || ty < 1) continue; if(!vis[tx][ty] && maze[tx][ty] != '#') { que[sec][0] = tx; que[sec++][1] = ty; vis[tx][ty] = 1; } } } } printf("%d\n", step); } int main() { int t, i, j; scanf("%d", &t); while (t--) { memset(maze, 0, sizeof(maze)); memset(vis, 0, sizeof(vis)); scanf("%d %d", &w, &h); getchar(); for (i = 1; i <= h; i++) { for (j = 1; j <= w; j++) { scanf("%c", &maze[i][j]); if (maze[i][j] == 'S') Sx = i, Sy = j; else if (maze[i][j] == 'E') Ex = i, Ey = j; } getchar(); } for (i = 0; i < 4; i++) if (maze[Sx+dir[i][0]][Sy+dir[i][1]] == '.') break; vis[Sx][Sy] = 1; a = i; tag = 1; c = -1; DFS(Sx, Sy, 1); a = i; c = 1; tag = 1; DFS(Sx, Sy, 1); BFS(); } return 0; }
3、poj3009
题意:要求把一个冰壶从起点“2”用最少的步数移动到终点“3”,其中0为移动区域,1为石头区域,冰壶一旦想着某个方向运动就不会停止,也不会改变方向(想想冰壶在冰上滑动),除非冰壶撞到石头1或者到达终点3,冰壶撞到石头后,冰壶会停在石头前面,此时(静止状态)才允许改变冰壶的运动方向,而该块石头会破裂,石头所在的区域由1变为0. 也就是说,冰壶撞到石头后,并不会取代石头的位置。终点是一个摩擦力很大的区域,冰壶若到达终点3,就会停止在终点的位置不再移动。
分析:直接搜索,题目细节是沿着某一行出发会遇到石头才会停止,不停止则失败,停止时石头也会消失,步数不能超过10步。
#include <cstdio> using namespace std; int square[30][30]; int w, h, sx, sy; int minnum; int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0}; void DFS(int x, int y, int cnt) { if (cnt >= 10) return ; for (int i = 0; i < 4; i++) { int flag = 1, tag = 0; int nx = x + dir[i][0], ny = y + dir[i][1]; while (1) { if (nx < 1 || nx > h || ny < 1 || ny > w) { flag = 0; break; } if (square[nx][ny] == 1) { tag = 1; break; } if (square[nx][ny] == 3 && minnum > ++cnt) { minnum = cnt; return ; } nx += dir[i][0]; ny += dir[i][1]; } if (!flag) continue; if (tag) { square[nx][ny] = 0; nx -= dir[i][0]; ny -= dir[i][1]; if (nx == x && ny == y) { square[nx+dir[i][0]][ny+dir[i][1]] = 1; continue; } DFS(nx, ny, cnt+1); square[nx+dir[i][0]][ny+dir[i][1]] = 1; } } } int main() { while (scanf("%d %d", &w, &h), w+h) { minnum = 9999; for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++){ scanf("%d", &square[i][j]); if (square[i][j] == 2){ sx = i; sy = j; } } } DFS(sx, sy, 0); minnum <= 10 ? printf("%d\n", minnum) : puts("-1"); } return 0; }
4、poj1321
题意:中文题。。。
分析:在n*n的格子中选取k列来放棋子,每次确定列数之后在确定行数,直接搜索。
#include <cstdio> #include <cstring> using namespace std; const int N = 12; int n, k; char b ; bool visr , visc ; void init() { memset(visr, 0, sizeof(visr)); memset(visc, 0, sizeof(visc)); } int dfs(int col, int cnt) { if (cnt == k) return 1; int t = n - k + cnt + 1, res = 0; for (int j = col; j < t; j++) { for (int i = 0; i < n; i++) { if (b[i][j] == '#' && !visc[j] && !visr[i]) { visr[i] = visc[j] = 1; res += dfs(j+1, cnt+1); visr[i] = visc[j] = 0; } } } return res; } int main() { while (scanf("%d %d", &n, &k), n != -1) { init(); for (int i = 0; i < n; i++) scanf("%s", b[i]); printf("%d\n", dfs(0, 0)); } return 0; }
5、poj2251
题意:一个立体空间,有L个平面,R行,C列,.代表可以走,#代表不能走,S代表开始点,E代表结束点,问从S走到E点,最少可以经过多少分钟,若不能到达,则输出Trapped!
分析:bfs水题,三维的,没什么变化,多一种行走方式而已。
#include <cstdio> #include <cstring> #include <queue> using namespace std; const int N = 35; const int INF = 0x3f3f3f3f; char dun ; bool vis ; int l, r, c; int dx[] = {1, -1, 0, 0, 0, 0}, dy[] = {0, 0, 1, -1, 0, 0}, dz[] = {0, 0, 0, 0, 1, -1}; struct po { int x, y, z, step; po(int x, int y, int z, int s) : x(x), y(y), z(z), step(s) {} po() {} }s, e; queue<po> q; void init() { while (!q.empty()) q.pop(); q.push(s); for (int i = 0; i < l; i++) for (int j = 0; j < r; j++) memset(vis[i][j], 0, 4*(c+3)); } int bfs() { init(); while (!q.empty()) { po t = q.front(); q.pop(); if (t.x == e.x && t.y == e.y && t.z == e.z) return t.step; for (int i = 0; i < 6; i++) { int nx = t.x + dx[i], ny = t.y + dy[i], nz = t.z + dz[i]; if (nx >= 0 && nx < l && ny >= 0 && ny < r && nz >= 0 && nz < c && !vis[nx][ny][nz] && dun[nx][ny][nz] != '#') { q.push(po(nx, ny, nz, t.step+1)); vis[nx][ny][nz] = 1; } } } return INF; } int main() { while (~scanf("%d %d %d", &l, &r, &c), l+r+c) { int flags = 1, flage = 1; for (int i = 0; i < l; i++) { for (int j = 0; j < r; j++) scanf("%s", dun[i][j]); for (int j = 0; j < r && flags; j++) for (int k = 0; k < c; k++) { if (dun[i][j][k] == 'S') s = po(i, j, k, 0), flags = 0; } for (int j = 0; j < r && flage; j++) for (int k = 0; k < c; k++) { if (dun[i][j][k] == 'E') e = po(i, j, k, 0), flage = 0; } } int ans = bfs(); ans < INF ? printf("Escaped in %d minute(s).\n", ans) : puts("Trapped!"); } return 0; }
(2)、广度优先搜索
1、poj3278
题意:就是给出a和b点的横坐标,求到a,b的最小行动次数,每次可以向左或向右移动一步,即横坐标加1或者减1,也可以横坐标变成原来的两倍。
分析:bfs的最基本应用,找最小步数/最小时间等。
#include <cstdio> #include <queue> #define MAXN 100000 using namespace std; int N, K; int vis[MAXN+1]; struct po { int p, time; }; queue <po> q; void BFS() { po temp = {N, 0}; q.push(temp); vis = 1; while (!q.empty()) { int a = q.front().p, b = q.front().time; q.pop(); if (a == K) { printf("%d\n", b); return ; } if (a+1 <= MAXN && !vis[a+1]) { temp.p = a+1; temp.time = b+1; q.push(temp); vis[a+1] = 1; } if (a-1 >= 0 && !vis[a-1]) { temp.p = a-1; temp.time = b+1; q.push(temp); vis[a-1] = 1; } if (2*a <= MAXN && !vis[2*a]) { temp.p = 2*a; temp.time = b+1; q.push(temp); vis[2*a] = 1; } } } int main() { scanf("%d %d", &N, &K); BFS(); return 0; }
2、poj1426
题意:给定一个数,找到由一个01串使得是它的倍数。
分析:直接bfs每次加0加1,同时每次取模,判断是不是为0就可以了。
#include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int N = 1000000; int flag, mod, cnt, i, j; int prior , prbit ; char s[110]; int next[2] = {1, 0}; struct mul { int num, prev; }; struct Queue { mul q[N*10]; int front, rear; }; Queue Q; void BFS() { int a, b, c, d; flag = cnt = 1; mul sta = {1, 1}; Q.front = Q.rear = 0; Q.q[Q.rear] = sta; while (flag) { a = Q.q[Q.front].num; b = Q.q[Q.front++].prev; for (j = 0; j < 2 && flag; j++) { prior[++cnt] = b; prbit[cnt] = next[j]; c = (a*10 + next[j]) % mod; if (c == 0) { i = 0; d = cnt; while (d != 1) { s[i++] = prbit[d]+'0'; d = prior[d]; } s[i++] = '1'; flag = 0; break; } mul temp = {c, cnt}; Q.q[++Q.rear] = temp; } } } int main() { while (scanf("%d", &mod), mod) { memset(s, 0, sizeof(s)); BFS(); reverse(s, s+i); puts(s); } return 0; }
3、poj3126
题意:给定两个素数a b,求a变换到b需要几步,并且变换时只有一个数字不同。
分析:素数打表,直接bfs。
#include <cstdio> #include <algorithm> #include <cstring> #include <queue> #include <utility> #include <string> using namespace std; int prime[] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997,1009,1013,1019,1021,1031,1033,1039,1049,1051,1061,1063,1069,1087,1091,1093,1097,1103,1109,1117,1123,1129,1151,1153,1163,1171,1181,1187,1193,1201,1213,1217,1223,1229,1231,1237,1249,1259,1277,1279,1283,1289,1291,1297,1301,1303,1307,1319,1321,1327,1361,1367,1373,1381,1399,1409,1423,1427,1429,1433,1439,1447,1451,1453,1459,1471,1481,1483,1487,1489,1493,1499,1511,1523,1531,1543,1549,1553,1559,1567,1571,1579,1583,1597,1601,1607,1609,1613,1619,1621,1627,1637,1657,1663,1667,1669,1693,1697,1699,1709,1721,1723,1733,1741,1747,1753,1759,1777,1783,1787,1789,1801,1811,1823,1831,1847,1861,1867,1871,1873,1877,1879,1889,1901,1907,1913,1931,1933,1949,1951,1973,1979,1987,1993,1997,1999,2003,2011,2017,2027,2029,2039,2053,2063,2069,2081,2083,2087,2089,2099,2111,2113,2129,2131,2137,2141,2143,2153,2161,2179,2203,2207,2213,2221,2237,2239,2243,2251,2267,2269,2273,2281,2287,2293,2297,2309,2311,2333,2339,2341,2347,2351,2357,2371,2377,2381,2383,2389,2393,2399,2411,2417,2423,2437,2441,2447,2459,2467,2473,2477,2503,2521,2531,2539,2543,2549,2551,2557,2579,2591,2593,2609,2617,2621,2633,2647,2657,2659,2663,2671,2677,2683,2687,2689,2693,2699,2707,2711,2713,2719,2729,2731,2741,2749,2753,2767,2777,2789,2791,2797,2801,2803,2819,2833,2837,2843,2851,2857,2861,2879,2887,2897,2903,2909,2917,2927,2939,2953,2957,2963,2969,2971,2999,3001,3011,3019,3023,3037,3041,3049,3061,3067,3079,3083,3089,3109,3119,3121,3137,3163,3167,3169,3181,3187,3191,3203,3209,3217,3221,3229,3251,3253,3257,3259,3271,3299,3301,3307,3313,3319,3323,3329,3331,3343,3347,3359,3361,3371,3373,3389,3391,3407,3413,3433,3449,3457,3461,3463,3467,3469,3491,3499,3511,3517,3527,3529,3533,3539,3541,3547,3557,3559,3571,3581,3583,3593,3607,3613,3617,3623,3631,3637,3643,3659,3671,3673,3677,3691,3697,3701,3709,3719,3727,3733,3739,3761,3767,3769,3779,3793,3797,3803,3821,3823,3833,3847,3851,3853,3863,3877,3881,3889,3907,3911,3917,3919,3923,3929,3931,3943,3947,3967,3989,4001,4003,4007,4013,4019,4021,4027,4049,4051,4057,4073,4079,4091,4093,4099,4111,4127,4129,4133,4139,4153,4157,4159,4177,4201,4211,4217,4219,4229,4231,4241,4243,4253,4259,4261,4271,4273,4283,4289,4297,4327,4337,4339,4349,4357,4363,4373,4391,4397,4409,4421,4423,4441,4447,4451,4457,4463,4481,4483,4493,4507,4513,4517,4519,4523,4547,4549,4561,4567,4583,4591,4597,4603,4621,4637,4639,4643,4649,4651,4657,4663,4673,4679,4691,4703,4721,4723,4729,4733,4751,4759,4783,4787,4789,4793,4799,4801,4813,4817,4831,4861,4871,4877,4889,4903,4909,4919,4931,4933,4937,4943,4951,4957,4967,4969,4973,4987,4993,4999,5003,5009,5011,5021,5023,5039,5051,5059,5077,5081,5087,5099,5101,5107,5113,5119,5147,5153,5167,5171,5179,5189,5197,5209,5227,5231,5233,5237,5261,5273,5279,5281,5297,5303,5309,5323,5333,5347,5351,5381,5387,5393,5399,5407,5413,5417,5419,5431,5437,5441,5443,5449,5471,5477,5479,5483,5501,5503,5507,5519,5521,5527,5531,5557,5563,5569,5573,5581,5591,5623,5639,5641,5647,5651,5653,5657,5659,5669,5683,5689,5693,5701,5711,5717,5737,5741,5743,5749,5779,5783,5791,5801,5807,5813,5821,5827,5839,5843,5849,5851,5857,5861,5867,5869,5879,5881,5897,5903,5923,5927,5939,5953,5981,5987,6007,6011,6029,6037,6043,6047,6053,6067,6073,6079,6089,6091,6101,6113,6121,6131,6133,6143,6151,6163,6173,6197,6199,6203,6211,6217,6221,6229,6247,6257,6263,6269,6271,6277,6287,6299,6301,6311,6317,6323,6329,6337,6343,6353,6359,6361,6367,6373,6379,6389,6397,6421,6427,6449,6451,6469,6473,6481,6491,6521,6529,6547,6551,6553,6563,6569,6571,6577,6581,6599,6607,6619,6637,6653,6659,6661,6673,6679,6689,6691,6701,6703,6709,6719,6733,6737,6761,6763,6779,6781,6791,6793,6803,6823,6827,6829,6833,6841,6857,6863,6869,6871,6883,6899,6907,6911,6917,6947,6949,6959,6961,6967,6971,6977,6983,6991,6997,7001,7013,7019,7027,7039,7043,7057,7069,7079,7103,7109,7121,7127,7129,7151,7159,7177,7187,7193,7207,7211,7213,7219,7229,7237,7243,7247,7253,7283,7297,7307,7309,7321,7331,7333,7349,7351,7369,7393,7411,7417,7433,7451,7457,7459,7477,7481,7487,7489,7499,7507,7517,7523,7529,7537,7541,7547,7549,7559,7561,7573,7577,7583,7589,7591,7603,7607,7621,7639,7643,7649,7669,7673,7681,7687,7691,7699,7703,7717,7723,7727,7741,7753,7757,7759,7789,7793,7817,7823,7829,7841,7853,7867,7873,7877,7879,7883,7901,7907,7919,7927,7933,7937,7949,7951,7963,7993,8009,8011,8017,8039,8053,8059,8069,8081,8087,8089,8093,8101,8111,8117,8123,8147,8161,8167,8171,8179,8191,8209,8219,8221,8231,8233,8237,8243,8263,8269,8273,8287,8291,8293,8297,8311,8317,8329,8353,8363,8369,8377,8387,8389,8419,8423,8429,8431,8443,8447,8461,8467,8501,8513,8521,8527,8537,8539,8543,8563,8573,8581,8597,8599,8609,8623,8627,8629,8641,8647,8663,8669,8677,8681,8689,8693,8699,8707,8713,8719,8731,8737,8741,8747,8753,8761,8779,8783,8803,8807,8819,8821,8831,8837,8839,8849,8861,8863,8867,8887,8893,8923,8929,8933,8941,8951,8963,8969,8971,8999,9001,9007,9011,9013,9029,9041,9043,9049,9059,9067,9091,9103,9109,9127,9133,9137,9151,9157,9161,9173,9181,9187,9199,9203,9209,9221,9227,9239,9241,9257,9277,9281,9283,9293,9311,9319,9323,9337,9341,9343,9349,9371,9377,9391,9397,9403,9413,9419,9421,9431,9433,9437,9439,9461,9463,9467,9473,9479,9491,9497,9511,9521,9533,9539,9547,9551,9587,9601,9613,9619,9623,9629,9631,9643,9649,9661,9677,9679,9689,9697,9719,9721,9733,9739,9743,9749,9767,9769,9781,9787,9791,9803,9811,9817,9829,9833,9839,9851,9857,9859,9871,9883,9887,9901,9907,9923,9929,9931,9941,9949,9967,9973,0}; typedef pair<string, int> p; queue<p> q; int d[4][12] = {{9, 1, 2, 3, 4, 5, 6, 7, 8, 9}, {10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, {10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, {4, 1, 3, 7, 9}}; int vis[10000], isprime[10000]; int solve(string f, string s) { while (!q.empty()) q.pop(); memset(vis, 0, sizeof(vis)); q.push(p(f, 0)); while (!q.empty()) { p t = q.front(); q.pop(); string x = t.first; if (x == s) return t.second+1; for (int i = 0; i< 4; i++) { for (int j = 1; j <= d[i][0]; j++) { string tmp = x; tmp[i] = d[i][j] + '0'; int k = atoi(tmp.c_str()); if (!vis[k] && isprime[k]) vis[k] = 1, q.push(p(tmp, t.second+1)); } } } return 0; } int main() { for (int i = 0; prime[i]; i++) isprime[prime[i]] = 1; int t; scanf("%d", &t); while (t--) { char f[10], s[10]; scanf("%s %s", &f, &s); int ans = solve(f, s); vis[atoi(f)] = 1; ans > 0 ? printf("%d\n", ans-1) : puts("Impossible"); } return 0; }
4、poj3087
题意:已知两堆牌s1和s2的初始状态, 其牌数均为c,按给定规则能将他们相互交叉组合成一堆牌s12,再将s12的最底下的c块牌归为s1,最顶的c块牌归为s2,依此循环下去。
现在输入s1和s2的初始状态以及 预想的最终状态s12。问s1 s2经过多少次洗牌之后,最终能达到状态s12,若永远不可能相同,则输出"-1"。
分析:直接模拟,set判重。
#include <cstdio> #include <algorithm> #include <cstring> #include <string> #include <set> using namespace std; const int N = 210; int len; char f , s , last ; string res, tt; set<string> a; void init() { a.clear(); res = last; tt = ""; for (int i = 0; i < len; i++) tt += s[i], tt += f[i]; a.insert(tt); } int bfs() { init(); int ans = 1; while (1) { if (tt == res) return ans; string tmp = ""; for (int i = len; i < 2*len; i++) tmp += tt[i], tmp += tt[i-len]; if (a.count(tmp)) return -1; else tt = tmp, a.insert(tt), ans++; } } int main() { int t, ca = 0; scanf("%d", &t); while (t--) { scanf("%d", &len); scanf("%s %s %s", f, s, last); printf("%d %d\n", ++ca, bfs()); } return 0; }
5、Poj3414
题意:给出了两个瓶子的容量A,B, 以及一个目标水量C,
对A、B可以有如下操作:装满一个瓶子、倒空一个瓶子、一个瓶子倒水到另一个瓶子。
问经过哪几个操作后能使得任意一个瓶子的残余水量为C,且输出操作过程。若不可能得到则输出impossible。
分析:设几个状态区别操作,直接bfs,并且记录路径。
#include <cstdio> #include <algorithm> #include <cstring> #include <queue> using namespace std; const int N = 110; bool vis ; int a, b, c; struct node { int a, b, cur, step; node(int a = 0, int b = 0, int c = 0, int s = 0) : a(a), b(b), cur(c), step(s) {} }; queue<node> q; int prior[N*N], ans[N*N]; void solve(int cur) { if (prior[cur] == -1) return ; solve(prior[cur]); if (ans[cur] == 1) puts("DROP(1)"); else if (ans[cur] == 2) puts("FILL(1)"); else if (ans[cur] == 3) puts("POUR(1,2)"); else if (ans[cur] == 4) puts("DROP(2)"); else if (ans[cur] == 5) puts("FILL(2)"); else if (ans[cur] == 6) puts("POUR(2,1)"); } bool bfs() { int cnt = 0; prior[0] = -1; q.push(node()); while (!q.empty()) { node t = q.front(); q.pop(); if (t.a == c || t.b == c) { printf("%d\n", t.step); solve(t.cur); return 1; } int sum = t.a + t.b; if (t.a) { if (!vis[0][t.b]) { q.push(node(0, t.b, ++cnt, t.step+1)); prior[cnt] = t.cur, vis[0][t.b] = 1; ans[cnt] = 1; } int db = b - t.b; if (t.a >= db && !vis[t.a-db]) { q.push(node(t.a-db, b, ++cnt, t.step+1)); prior[cnt] = t.cur, vis[t.a-db][b] = 1; ans[cnt] = 3; } if (t.a < db && !vis[0][sum]) { q.push(node(0, sum, ++cnt, t.step+1)); prior[cnt] = t.cur; vis[0][sum] = 1; ans[cnt] = 3; } } else { if (t.a != a && !vis[a][t.b]) { q.push(node(a, t.b, ++cnt, t.step+1)); prior[cnt] = t.cur, vis[a][t.b] = 1; ans[cnt] = 2; } } if (t.b) { if (!vis[t.a][0]) { q.push(node(t.a, 0, ++cnt, t.step+1)); prior[cnt] = t.cur, vis[t.a][0] = 1; ans[cnt] = 4; } int da = a - t.a; if (t.b >= da && !vis[a][t.b-da]) { q.push(node(a, t.b-da, ++cnt, t.step+1)); prior[cnt] = t.cur, vis[a][t.b-da] = 1; ans[cnt] = 6; } if (t.b < da && !vis[sum][0]) { q.push(node(sum, 0, ++cnt, t.step+1)); prior[cnt] = t.cur; vis[sum][0] = 1; ans[cnt] = 6; } } else { if (b != t.b && !vis[t.a][b]) { q.push(node(t.a, b, ++cnt, t.step+1)); prior[cnt] = t.cur, vis[t.a][b] = 1; ans[cnt] = 5; } } } return 0; } int main() { scanf("%d %d %d", &a, &b, &c); if (!bfs()) puts("impossible"); return 0; }
[b](3)、简单搜索技巧和剪枝
这里的几个题都可以直接搜,不需要剪枝,而且我感觉剪枝没多大用,因为本来效率就很高,不过可以学习最基本的一些剪枝技巧。
1、poj2531
题意:是把点分成A、B两组,节点之间的间距已给出,要求解分组的方法,使得∑Cij (i∈A,j∈B)最大
分析:直接暴力枚举A组,优化了下计算过程。不过这题归类为剪枝,当然也有剪枝的方式来搜索。方式同样是枚举A组的点,每次在A组中加入一个点,计算新的权值,如果答案更大,则把该点加入A集合中,继续进行枚举,否则就没必要继续进行枚举了。枚举新加入的其他点即可。
#include <cstdio> #include <cstring> #include <vector> #include <algorithm> #include <numeric> using namespace std; const int N = 25; int c , sum , tmp , val[1<<21], a ; int ans = 0, n, up; void dfs(int i, int* a, int k, int t) { if (i == n) { if (val[t] == -1) { int tot = 0; for (int i = 0; i < k; i++) { tot += sum[a[i]]; for (int j = 0; j < k; j++) tot -= c[a[i]][a[j]]; } val[t] = val[up^t] = tot; ans = max(ans, tot); } return ; } dfs(i+1, a, k, t); a[k++] = i; dfs(i+1, a, k, t | tmp[i]); } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) tmp[i] = 1 << i; up = (1 << n) - 1; memset(val, -1, 4*(up+10)); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) scanf("%d", &c[i][j]); for (int i = 0; i < n; i++) sum[i] = accumulate(c[i], c[i]+n, 0); dfs(0, a, 0, 0); printf("%d\n", ans); return 0; }
2、poj1416
题意:将一个数字m(不超过六位)剪成几段,要求得到的新的几个数之和不超过另外一个给定的数n,求可以得到的最接近n的数。
分析:简单的搜索题,随便暴力都能过,剪枝的方式就是如果当前枚举出的切割方式使得和已经比目标大了,则没必要继续搜索。这是可行性剪枝。
#include <cstdio> #include <cstring> #include <vector> #include <algorithm> #include <cmath> using namespace std; const int N = 10; int dig , tmp , ans ; int tar, a, len, res, cnt, l; void dfs(int i, int sum, int* a, int k) { if (sum > tar) return ; if (i > len) { if (sum == res) cnt++; else if (sum > res) { res = sum; l = k; memcpy(ans, a, 4*k), cnt = 1; } } for (int j = i; j <= len; j++) { int val = 0; for (int x = i; x <= j; x++) val = val * 10 + dig[x]; a[k] = val; dfs(j+1, sum + val, a, k+1); } } int main() { while (scanf("%d %d", &tar, &a), tar+a) { if (tar == a) { printf("%d %d\n", tar, a); continue; } int t = a, sum = 0; len = log10(a) + 1; for (int i = len; i >= 1; i--, t /= 10) dig[i] = t % 10, sum += dig[i]; if (sum > tar) { puts("error"); continue; } cnt = res = 0; dfs(1, 0, tmp, 0); if (cnt > 1) puts("rejected"); else { printf("%d", res); for (int j = 0; j < l; j++) printf(" %d", ans[j]); puts(""); } } return 0; }
3、poj2676
题意:把一个9行9列的网格,再细分为9个3*3的子网格,要求每行、每列、每个子网格内都只能使用一次1~9中的一个数字,即每行、每列、每个子网格内都不允许出现相同的数字。求满足题意的数独。
分析:直接搜索,标记行、列、块,值得一提的是倒着搜比正着搜效率高出许多,这也算是一个技巧。
#include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int N = 25; int a ; char s[10]; int k, flag = 1; struct po{int x, y;}p[90]; int visro , visco , visbl[10] ; po pos[] = {{0, 0}, {1, 1}, {1, 4}, {1, 7}, {4, 1}, {4, 4}, {4, 7}, {7, 1}, {7, 4}, {7, 7}}; int num[10][10]; void DFS(int cnt) { if (cnt < 0) { for (int i = 1; i <= 9; i++){ for (int j = 1; j <= 9; j++) printf("%d", a[i][j]); putchar('\n'); } flag = 0; return ; } if (flag){ int x = p[cnt].x, y = p[cnt].y; for (int i = 1; i <= 9 && flag; i++){ if (!visco[y][i] && !visro[x][i] && !visbl[num[x][y]][i]) { visco[y][i] = visro[x][i] = visbl[num[x][y]][i] = 1; a[x][y] = i; DFS(cnt-1); if (!flag) break; visco[y][i] = visro[x][i] = visbl[num[x][y]][i] = 0; } } } } int main() { for (int i = 1; i <= 9; i++) { int x = pos[i].x, y = pos[i].y; for (int j = 0; j < 3; j++) for (int l = 0; l < 3; l++) num[x+j][y+l] = i; } int t; scanf("%d", &t); while (t--) { flag = 1; k = 0; for (int i = 1; i <= 9; i++) { scanf("%s", s+1); for (int j = 1; j <= 9; j++) { a[i][j] = s[j]-'0'; if (!a[i][j]) p[k++] = (po){i, j}; } } memset(visro, 0, sizeof(visro)); memset(visco, 0, sizeof(visco)); memset(visbl, 0, sizeof(visbl)); for (int i = 1; i <= 9; i++) { for (int j = 1; j <= 9; j++) visro[i][a[i][j]] = visco[i][a[j][i]] = 1; int x = pos[i].x, y = pos[i].y; for (int j = 0; j < 3; j++) for (int l = 0; l < 3; l++) visbl[i][a[x+j][y+l]] = 1; } DFS(k-1); } return 0; }
4、poj1129
题意:给一个图,相邻的结点关系已给出,求给图着色要多少种颜色。
分析:直接暴力枚举,颜色数从小到大枚举即可,不过这题简直神坑,刚开始我读错题了,以为是枚举全排列,存在相邻关系的点在排列中相邻会多一种颜色,然后在那各种剪枝,但是14以上的数据基本上不可能在时限内解出来了。。。交上去居然没超时,折腾了一阵发现读错题了,重写了一遍,这题无语主要是数据过弱,网上很多错误代码都过了,我跑数据测试过。难怪我枚举全排列都没超时。。。。
#include <cstdio> #include <cstring> using namespace std; const int N = 30; bool g ; char s[N<<1]; int val , n; bool cal(int x, int v) { for (int i = 0; i < n; i++) if (g[x][i] && val[i] == v) return 0; return 1; } bool dfs(int x, int num) { if (x == n) return 1; for (int i = 1; i <= num; i++) { if (cal(x, i)) { val[x] = i; if (dfs(x+1, num)) return 1; } } return 0; } int main() { while (scanf("%d", &n), n) { for (int i = 0; i < n; i++) { memset(g[i], 0, 4*n); val[i] = 0; scanf("%s", s); for (int j = 2; s[j]; j++) g[i][s[j]-'A'] = 1; } int ans = 0; for (int i = 1; !ans; i++) if (dfs(0, i)) ans = i; ans == 1 ? puts("1 channel needed.") : printf("%d channels needed.\n", ans); } return 0; }
总结:第四个专题是最基本的搜索题目训练,题目都很简单,可以帮助巩固基础。至于后面的剪枝部分的四个题目,也算是体现了一些基本的剪枝策略。
大概看来可以得出以下几点:
1、 搜索顺序很重要,比如说数独那题,这题倒着来效率比正着来效率更高,正是体现了搜索的顺序,有时我们还需要对搜索对象做些处理,原则是使得搜索更高效,更易剪枝,比如说处理搜索对象使得更易得到最优解,进而进行最优性剪枝,把不灵活的搜索对象放在前面可以进行可行性剪枝等。
2、 可行性剪枝,比如说切纸中如果和已经比目标大了,直接返回。
3、 最优性剪枝,第一个题的搜索方式倒是体现了一点,如果当前加入的点不可以得到更大的最优解直接退出。
我们需要的是根据以上三点得出更高效的搜索方式。
以上只是个人对剪枝的一点点看法,也不是什么经验之谈,望大牛不吝赐教。
相关文章推荐
- poj初期基本算法
- 奇怪的电梯(基本的搜索)
- [转帖]基本搜索技巧十条
- Windows搜索DLL文件的基本规则
- POJ 3126-Prime Path 简单搜索 BFS
- POJ 搜索题 慢慢做也算是有个标准了
- POJ 3414-Pots 简单搜索 BFS
- 《对弈程序基本技术》专题:Alpha-Beta搜索
- POJ_2503(基本Hash)
- POJ 3278-Catch That Cow 广度优先搜索BFS
- 瞬间模糊搜索1000万基本句型的语言算法
- Linux下的搜索工具find基本用法
- 使用 Apache Solr 实现更加灵巧的搜索,第 1 部分: 基本特性和 Solr 模式
- POJ-1011-Sticks(经典搜索)
- POJ 2112:二分搜索 + 二分图多重匹配 + 最短路径
- 基本算法连载(1)-顺序搜索与二分搜索
- .net lucene 实战搜索(三)----- 基本之搜索
- [转载]搜索算法(含基本搜索算法与深度搜索与广度搜索算法等思想)
- 搜索文件的基本知识
- POJ 1321-棋盘问题 简单搜索DFS