HDU-4255 BFS 最短路
题意:蛇形填数,然后素数处是障碍,给你起点终点,求步数;
思路:其实就是bfs,关键是将数字转换成位置比较难;
bfs其实比较简单,就是固定的思路,固定的步骤;
模板:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 const int dir[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; 2 int vis[maxn], d[maxn]; 3 bool is_ok(int x, int y)///坐标是否合格,按照题意来进行 4 { 5 if(x<0 || y<0||x>n||y>n) 6 return false; 7 return true; 8 } 9 int dfs(Node st,Node ed)///起点终点 10 { 11 queue<Node> q; 12 q.push(st);///压进起点 13 memset(vis,0,sisteof(vis)); 14 memset(d,0,sizeof(d)); 15 int ss = a[st.x][st.y]; 16 d[ss] = 0; 17 int edd = a[ed.x][ed.y]; 18 while(!q.empty()) 19 { 20 Node c = q.front(),v; 21 q.pop(); 22 int stt = a[c.x][c.y]; 23 if(stt == edd)///先判断是否到达终点 24 return d[stt]; 25 repu(i,0,4) 26 { 27 v.x = c.x + dir[i][0]; 28 v.y = c.y + dir[i][1]; 29 if(is_ok(v.x,v.y))///坐标是否合格 30 { 31 int sd = a[v.x][v.y]; 32 if(!vis[sd])///符合所有条件后 33 { 34 q.push(v);///压进 35 vis[sd] = 1;///V过 36 d[sd] = d[stt] + 1;///步数为之前的+1 37 } 38 } 39 else 40 continue; 41 } 42 } 43 return -1; 44 }
该题代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <cstring> 5 #include <cstdlib> 6 #include <string> 7 #include <vector> 8 #include <algorithm> 9 #include<queue> 10 #include <set> 11 #define repu(i,a,b) for(int i=a;i<b;i++) 12 using namespace std; 13 #define N 1000010 14 #define ll long long 15 #define _cle(m, a) memset(m, a, sizeof(m)) 16 const int maxn = 1000010; 17 const int mn = 500; 18 int tot; 19 int a[1100][1100]; 20 struct Node 21 { 22 int x, y; 23 } nodes[maxn]; 24 void init() 25 { 26 memset(a, 0, sizeof(a)); 27 a[mn][mn] = 1; 28 tot = 1; 29 nodes[1].x = mn; 30 nodes[1].y = mn; 31 int cur = 1; 32 int i = mn, j = mn+1; 33 while(tot <= maxn) 34 { 35 int t; 36 t = 0; 37 i++; 38 while(t < cur*2) 39 { 40 a[--i][j] = ++tot; 41 nodes[tot].x = i; 42 nodes[tot].y = j; 43 t++; 44 } 45 t = 0; 46 while(t < cur*2) 47 { 48 a[i][--j] = ++tot; 49 nodes[tot].x = i; 50 nodes[tot].y = j; 51 t++; 52 } 53 t = 0; 54 while(t < cur*2) 55 { 56 a[++i][j] = ++tot; 57 nodes[tot].x = i; 58 nodes[tot].y = j; 59 t++; 60 } 61 t = 0; 62 while(t < cur*2) 63 { 64 a[i][++j] = ++tot; 65 nodes[tot].x = i; 66 nodes[tot].y = j; 67 t++; 68 } 69 ++j; 70 cur++; 71 } 72 } 73 int prime[maxn]; 74 void is_prime() 75 { 76 memset(prime, 0, sizeof(prime)); 77 int m = sqrt(maxn+0.5); 78 prime[0] = prime[1] = 1; 79 for(int i = 2; i <= m; i++) 80 { 81 if(!prime[i]) 82 { 83 for(int j = i*i; j <= maxn; j+=i) 84 { 85 prime[j] = 1; 86 } 87 } 88 } 89 } 90 91 const int dir[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; 92 int vis[maxn], d[maxn]; 93 bool is_ok(int x, int y) 94 { 95 return x>=0 && y >=0; 96 } 97 int bfs(Node z,Node b) 98 { 99 queue<Node> q; 100 q.push(z); 101 memset(vis,0,sizeof(vis)); 102 memset(d,0,sizeof(d)); 103 int ss = a[z.x][z.y]; 104 d[ss] = 0; 105 int ed = a[b.x][b.y]; 106 while(!q.empty()) 107 { 108 Node c = q.front(),v; 109 q.pop(); 110 int st = a[c.x][c.y]; 111 if(st == ed) 112 return d[st]; 113 repu(i,0,4) 114 { 115 v.x = c.x + dir[i][0]; 116 v.y = c.y + dir[i][1]; 117 if(is_ok(v.x,v.y)) 118 { 119 int sd = a[v.x][v.y]; 120 if(!vis[sd]&&prime[sd]) 121 { 122 q.push(v); 123 vis[sd] = 1; 124 d[sd] = d[st] + 1; 125 } 126 } 127 else 128 continue; 129 } 130 } 131 return -1; 132 } 133 int main() 134 { 135 init(); 136 is_prime(); 137 int x, y, kase = 0; 138 while(~scanf("%d%d", &x, &y)) 139 { 140 if(!prime[x] || !prime[y]) 141 { 142 printf("Case %d: impossible\n", ++kase); 143 continue; 144 } 145 int ans = bfs(nodes[x], nodes[y]); 146 if(ans == -1) printf("Case %d: impossible\n", ++kase); 147 else printf("Case %d: %d\n", ++kase, ans); 148 } 149 150 return 0; 151 }
人生就像心电图,想要一帆风顺,除非game-over
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步