最近我寫搜索老是出現不少亂七八糟的的錯誤node
我個zzios
把基礎的代碼貼一下吧spa
例一:奇怪的電梯code
(廣搜blog
#include<iostream> #include<cstdio> using namespace std; long long int a,b[10001],c,d[201],e[10001],head,tail=1,f[201],x,y; int main() { cin>>a>>e[0]>>c; for(int i=1;i<=a;i++) cin>>d[i]; while(head<tail){ if(e[head]==c){ cout<<b[head]<<endl; return 0; } x=e[head]+d[e[head]]; if(x<=a&&f[x]==0){ e[tail]=x; f[x]=1; b[tail]=b[head]+1; tail++; } y=e[head]-d[e[head]]; if(y>=1&&f[y]==0){ e[tail]=y; f[y]=1; b[tail]=b[head]+1; tail++; } head++; } cout<<"-1"<<endl; return 0; }
例二:八皇后ci
(深搜get
//八皇后 #include<iostream> #include<cstring> #include<cstdio> using namespace std; int search(int ); void print(); bool d[10000]={0},b[10000]={0},c[10000]={0}; int n,sum=0,a[10000]; int main() { cin>>n; search(1); cout<<sum<<endl; return 0; } int search(int i){ int j; for(j=1;j<=n;j++){ if(!b[j]&&!c[i+j]&&!d[i-j+n]){ a[i]=j; b[j]=1; c[i+j]=1; d[i-j+n]=1; if(i==n) print(); else search(i+1); b[j]=0; c[i+j]=0; d[i-j+n]=0; } } } void print(){ int i; sum++; if(sum>=4) return; else { for(i=1;i<=n;i++) cout<<a[i]<<" "; cout<<endl; } }
以前的碼風還能夠, 果真符合我強迫症的風格。string
關於新碼風it
例三:奶酪io
(深搜
#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int N = 1010; int t, n, vis[N]; bool flag; double h, r; struct node { double x, y, z; }e[N]; bool cmp(node x, node y) { return y.z < x.z; } double way(double x1, double y1, double z1, double x2, double y2, double z2) { return sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) + (z2 - z1) * (z2 - z1)); } void dfs(node x, int num) { if(x.z + r >= h) {flag = 1;return;} vis[num] = 1; for(int i = 1; i <= n; i++) { if(flag) return; if(!vis[i] && way(x.x, x.y, x.z, e[i].x, e[i].y, e[i].z) <= r * 2) dfs(e[i], i); } } int main() { scanf("%d", &t); while(t--) { memset(vis, 0, sizeof(vis)); memset(e, 0, sizeof(e)); flag = 0; scanf("%d%lf%lf", &n, &h, &r); for(int i = 1; i <= n; i++) scanf("%lf%lf%lf", &e[i].x, &e[i].y, &e[i].z); sort(e + 1, e + 1 + n, cmp); for(int i = 1; i <= n; i++) if(e[i].z - r <= 0) dfs(e[i], i); if(flag) printf("Yes\n"); else printf("No\n"); } return 0; }//ljbcjhwqc
也沒怎麼變嘛
謝謝收看, 祝身體健康!