大一集训 搜索技术科普
A - Red and Black HDU - 1312 dfs
题目大意:给你起点,障碍物不可越过,最多能去到几个点。
1 #include <iostream> 2 #include <map> 3 #include<cstdio> 4 using namespace std; 5 int sx, sy,w,h,ans=0; 6 char mp[21][21]; 7 int vis[21][21]; 8 void dfs(int x, int y) { 9 if (x < 0 || y < 0 || x >= h || y >= w || mp[x][y] == '#'|| vis[x][y] == 1)return; 10 ans++; vis[x][y] = 1; 11 dfs(x-1, y);dfs(x+1, y);dfs(x, y-1);dfs(x, y+1); 12 } 13 int main() 14 { 15 while (scanf("%d %d", &w, &h), w + h) { 16 for (int i = 0;i < h;i++) { 17 scanf("%s", mp[i]); 18 for (int j = 0;j < w;j++) { 19 if (mp[i][j] == '@') { sx = i, sy = j; } 20 } 21 } 22 dfs(sx, sy); 23 printf("%d\n", ans); 24 ans = 0; 25 memset(vis, 0, sizeof vis); 26 } 27 }
C - N皇后问题 HDU - 2553
1 #include <iostream> 2 #include <map> 3 #include <string> 4 #include<algorithm> 5 #include<vector> 6 #include<cmath> 7 #include<queue> 8 #define max 20 9 using namespace std; 10 int n, ans[max], mp[max][max], res; 11 bool ok(int x, int y) { 12 for (int i = 1;i <= x - 1;i++) { 13 for (int j = 1;j <= n;j++) { 14 if (!(x == i && y == j)) {//除去需要判断的点 15 if (mp[i][j] == 1) {//该点放有皇后 16 if (j == y || i + j == x + y || i - j == x - y)return false;//判断列,正负对角线是否满足 17 } 18 } 19 } 20 } 21 return true; 22 } 23 void dfs(int queen) { 24 if (queen == n + 1) { 25 res++; 26 return; 27 } 28 for (int i = 1;i <= n;i++) { 29 if (ok(queen, i)) { 30 mp[queen][i] = 1; 31 dfs(queen + 1); 32 mp[queen][i] = 0; 33 } 34 } 35 } 36 int main() { 37 memset(ans, -1, sizeof(ans)); 38 while (scanf("%d", &n), n) { 39 if (ans[n] != -1) {//没有ans会超时,相当于预处理 40 printf("%d\n", ans[n]); 41 continue; 42 } 43 memset(mp, 0, sizeof(mp)); 44 res = 0; 45 dfs(1); 46 ans[n] = res; 47 printf("%d\n", ans[n]); 48 } 49 return 0; 50 }
G - Catch That Cow POJ - 3278 bfs
题目大意:农民抓牛,农民可以选择前进或后退一步,或者达到当前距离的两倍地方。
1 #include <iostream> 2 #include <map> 3 #include <string> 4 #include<algorithm> 5 #include<vector> 6 #include<cmath> 7 #include<queue> 8 #define max 100010 9 int vis[max] = { 0 }; 10 struct Node { 11 int x; 12 int step; 13 }temp, nxt; 14 using namespace std; 15 int n, k; 16 int a[max]; 17 int bfs(int a) { 18 temp.x = a; 19 temp.step = 0; 20 queue<Node>q; 21 q.push(temp); 22 while (!q.empty()) { 23 temp = q.front(); 24 q.pop(); 25 if (temp.x == k)return temp.step; 26 for (int i = 0;i < 3;i++) { 27 if (i == 0) { nxt.x = temp.x * 2; } 28 else if (i == 1) { nxt.x = temp.x + 1; } 29 else if (i == 2) { nxt.x = temp.x - 1; } 30 if (nxt.x < 0 || nxt.x>max || vis[nxt.x] == 1)continue;//如果把vis[nxt.x]放前面,nxt.x可能会超过vis数组开的范围,例如(100000*2),或者直接改变vis数组的大小 31 nxt.step = temp.step + 1; 32 vis[nxt.x] = 1; 33 q.push(nxt); 34 } 35 } 36 } 37 int main() { 38 while (scanf("%d %d", &n, &k) != EOF) { 39 memset(vis, 0, sizeof(vis)); 40 printf("%d\n", bfs(n)); 41 } 42 }
H - Dungeon Master POJ - 2251
三维立体图,从S到E,不能经过‘#’
1 #include <iostream> 2 #include <map> 3 #include <string> 4 #include<algorithm> 5 #include<vector> 6 #include<cmath> 7 #include<queue> 8 #define max 10002 9 using namespace std; 10 int vis[35][35][35] = { 0 }; 11 int sx, sy, sz; 12 int ex, ey, ez; 13 char mp[35][35][35]; 14 int dx[6] = { 1,0,-1,0,0,0 }, dy[6] = { 0,1,0,-1 }, dz[6] = {0,0,0,0,1,-1}; 15 int l, r, c; 16 typedef struct Node { 17 int x, y, z; 18 int step; 19 }node; 20 int bfs(int a, int b, int cc) { 21 node temp, next; 22 temp.x = a; 23 temp.y = b; 24 temp.z = cc; 25 temp.step = 0; 26 queue<node>q; 27 q.push(temp); 28 while (!q.empty()) { 29 temp = q.front(); 30 q.pop(); 31 if (temp.x == ex && temp.y == ey && temp.z == ez)return temp.step; 32 for (int i = 0;i < 6;i++) { 33 if ((vis[temp.x + dx[i]][temp.y + dy[i]][temp.z + dz[i]] == 1))continue; 34 if((mp[temp.x + dx[i]][temp.y + dy[i]][temp.z + dz[i]] == '#'))continue; 35 if ((temp.x + dx[i]) < 0)continue; 36 if((temp.x + dx[i])>=r)continue; 37 if ((temp.y + dy[i])<0)continue; 38 if ((temp.y + dy[i])>=c)continue; 39 if ((temp.z + dz[i])<0)continue; 40 if ((temp.z + dz[i])>=l)continue; 41 next.x=temp.x + dx[i]; 42 next.y=temp.y + dy[i]; 43 next.z=temp.z + dz[i]; 44 next.step = temp.step + 1; 45 vis[next.x][next.y][next.z] = 1; 46 q.push(next); 47 } 48 } 49 return -1; 50 } 51 int main() { 52 while (scanf("%d %d %d", &l, &r, &c) ,l+r+c) { 53 memset(vis, 0, sizeof(vis)); 54 memset(mp, '0', sizeof(mp)); 55 getchar(); 56 for (int i = 0;i < l;i++) { 57 for (int j = 0;j < r;j++) { 58 for (int k = 0;k < c;k++) { 59 scanf("%c", &mp[j][k][i]); 60 if (mp[j][k][i] == 'S') { sx = j;sy = k;sz = i; } 61 if (mp[j][k][i] == 'E') { ex = j;ey = k;ez = i; } 62 } 63 getchar(); 64 } 65 if(i!=l-1)getchar(); 66 } 67 int res = bfs(sx, sy, sz); 68 if (res== -1)printf("Trapped!\n"); 69 else printf("Escaped in %d minute(s).\n", res); 70 } 71 }
I - Prime Path HDU - 1973
题意:给你两个四位数,都是素数,每次改变可以改变其中的任何一个数字,但要求改变后的四位数(没有前导零)依然是素数,问最少改变几次可以使得第一个数改为第二个数
1 #include <iostream> 2 #include <map> 3 #include <string> 4 #include<algorithm> 5 #include<vector> 6 #include<cmath> 7 #include<queue> 8 #define max 10002 9 using namespace std; 10 int n, m; 11 int vis[max] = { 0 }; 12 int num[max] = { 0 }; 13 typedef struct Node { 14 int x; 15 int step; 16 }node; 17 void getprime() { 18 for (int i = 1001;i <= 9999;i += 2) { 19 int flag = 1; 20 for (int j = 2;j <= sqrt(i);j++) { 21 if (i % j == 0) {flag = 0;break;} 22 } 23 if (flag == 1)num[i] = 1; 24 } 25 } 26 int bfs(int a) { 27 node temp, next; 28 temp.x = a; 29 temp.step = 0; 30 queue<node>q; 31 q.push(temp); 32 while (!q.empty()) { 33 temp = q.front(); 34 q.pop(); 35 if (temp.x == m)return temp.step; 36 for (int i = temp.x / 1000;i < 9;i++) { 37 if (vis[temp.x + 1000 * (i - temp.x / 1000 + 1)] ==1 )continue; 38 if (num[temp.x + 1000 * (i - temp.x / 1000 + 1)] == 1) {next.x = temp.x + 1000 * (i-temp.x/1000+1);next.step = temp.step + 1;vis[next.x] = 1;q.push(next);} 39 } 40 for (int i = temp.x / 1000;i >1;i--) { 41 if (vis[temp.x - 1000 * (temp.x / 1000 - i + 1)] == 1)continue; 42 if (num[temp.x - 1000 * (temp.x / 1000 - i + 1)] == 1) { next.x = temp.x - 1000 * (temp.x/1000-i+1);next.step = temp.step + 1;vis[next.x] = 1; q.push(next);} 43 } 44 for (int i = temp.x%1000/100;i < 9;i++) { 45 if (vis[temp.x + 100 * (i - temp.x % 1000 / 100 + 1)] == 1)continue; 46 if (num[temp.x + 100 * (i - temp.x % 1000 / 100 + 1)] == 1) { next.x = temp.x + 100 * (i- temp.x % 1000 / 100+1);next.step = temp.step + 1;vis[next.x] = 1;q.push(next); } 47 } 48 for (int i = temp.x%1000/100;i > 0;i--) { 49 if (vis[temp.x - 100 * (temp.x % 1000 / 100 - i + 1)] == 1)continue; 50 if (num[temp.x - 100 * (temp.x % 1000 / 100 - i + 1)] == 1) { next.x = temp.x - 100 * (temp.x % 1000 / 100-i+1);next.step = temp.step + 1;vis[next.x] = 1; q.push(next); } 51 } 52 for (int i = temp.x % 100 / 10;i < 9;i++) { 53 if (vis[temp.x + 10 * (i - temp.x % 100 / 10 + 1)] == 1)continue; 54 if (num[temp.x + 10 * (i - temp.x % 100 / 10 + 1)] == 1) { next.x = temp.x + 10 * (i- temp.x % 100 / 10+1);next.step = temp.step + 1;vis[next.x] = 1;q.push(next); } 55 } 56 for (int i = temp.x % 100 / 10;i > 0;i--) { 57 if (vis[temp.x - 10 * (temp.x % 100 / 10 - i + 1)] == 1)continue; 58 if (num[temp.x - 10 * (temp.x % 100 / 10 - i + 1)] == 1) { next.x = temp.x - 10 * (temp.x % 100 / 10-i+1);next.step = temp.step + 1;vis[next.x] = 1; q.push(next); } 59 } 60 for (int i = temp.x % 10;i < 9;i++) { 61 if (vis[temp.x + 1 * (i - temp.x % 10 + 1)] == 1)continue; 62 if (num[temp.x + 1 * (i - temp.x % 10 + 1)] == 1) { next.x = temp.x + 1 * (i- temp.x % 10+1);next.step = temp.step + 1;vis[next.x] = 1;q.push(next); } 63 } 64 for (int i = temp.x % 10;i > 0;i--) { 65 if (vis[temp.x - 1 * (temp.x % 10 - i + 1)] == 1)continue; 66 if (num[temp.x - 1 * (temp.x % 10 - i + 1)] == 1) { next.x = temp.x - 1 * (temp.x % 10-i+1);next.step = temp.step + 1;vis[next.x] = 1; q.push(next); } 67 } 68 } 69 } 70 int main() { 71 int cn; 72 scanf("%d", &cn); 73 getprime(); 74 while (cn--) { 75 memset(vis, 0, sizeof(vis)); 76 scanf("%d %d", &n, &m); 77 printf("%d\n", bfs(n)); 78 } 79 return 0; 80 }

浙公网安备 33010602011771号