搜索例题 2019/7/3
例题1
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int n,m,x,y,map[16][16],cnt; char ch[10]; bool vis[16][16]; void dfs(int x,int y){ cnt++; vis[x][y]=1; if((map[x+1][y]==map[x][y])&&!vis[x+1][y])dfs(x+1,y); if((map[x][y+1]==map[x][y])&&!vis[x][y+1])dfs(x,y+1); if((map[x-1][y]==map[x][y])&&!vis[x-1][y])dfs(x-1,y); if((map[x][y-1]==map[x][y])&&!vis[x][y-1])dfs(x,y-1); } int main(){ scanf("%d%d%d%d",&n,&m,&x,&y); for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ scanf("%s",ch); if(ch[0]=='A')map[i][j]=1; if(ch[0]=='B')map[i][j]=2; if(ch[0]=='C')map[i][j]=3; if(ch[0]=='D')map[i][j]=4; } } if(!map[x][y]){ puts("0"); return 0; } dfs(x,y); printf("%d\n",cnt); return 0; }
例题2
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int n,k,ans,cnt[2],a[2][100010]; bool vis[100010]; int main(){ scanf("%d%d",&n,&k); if(n==k){ puts("0"); return 0; } vis[n]=1; a[0][++cnt[0]]=n; bool f=0; while(1){ ans++; f=!f; for(int i=1;i<=cnt[!f];i++){ int cur; cur=a[!f][i]+1; if(cur<=100000&&cur>=0){ if(!vis[cur]){ if(cur==k){ printf("%d\n",ans); return 0; } else { vis[cur]=1; a[f][++cnt[f]]=cur; } } } cur=a[!f][i]-1; if(cur<=100000&&cur>=0){ if(!vis[cur]){ if(cur==k){ printf("%d\n",ans); return 0; } else { vis[cur]=1; a[f][++cnt[f]]=cur; } } } cur=a[!f][i]*2; if(cur<=100000&&cur>=0){ if(!vis[cur]){ if(cur==k){ printf("%d\n",ans); return 0; } else { vis[cur]=1; a[f][++cnt[f]]=cur; } } } } cnt[!f]=0; } return 0; }
例题3
https://www.cnblogs.com/yanlifneg/p/5717160.html
例题4
#include<cstdio> #include<iostream> #include<map> #define M 45 #define ll long long using namespace std; ll a[M],s1[M],s2[M],n,key,ans; map<ll,int> hash; void dfs(int x,int sum,int flag) { if(!flag&&x>n/2) { hash[sum]++; return; } if(flag&&x>n) { ans+=hash[key-sum]; return; } dfs(x+1,sum+a[x],flag); dfs(x+1,sum,flag); } int main() { cin>>n>>key; for(int i=1; i<=n; i++) cin>>a[i]; dfs(1,0,0); dfs(n/2+1,0,1); cout<<ans; return 0; }
例题5
#include<bits/stdc++.h> using namespace std; const int maxn=20; int a[maxn]; int cnt[maxn]; int ans; int n,m; void dfs(int v,int sum) { if(sum>=ans) return; if(v==n+1) { ans=sum; return; } for(int i=1; i<=sum; i++) { if(cnt[i]+a[v]<=m) { cnt[i]+=a[v]; dfs(v+1,sum); cnt[i]-=a[v]; } } cnt[sum+1]=a[v]; dfs(v+1,sum+1); cnt[sum+1]=0; } int main() { cin>>n>>m; ans=n; for(int i=1; i<=n; i++) cin>>a[i]; sort(a+1,a+1+n,greater<int>()); dfs(1,0); cout<<ans<<endl; return 0; }
例题6
#include<iostream> using namespace std; int map[6][6],target[6][6],sx,sy,ans,mp[6][6]; int e[8][2]={{1,2},{2,1},{-1,-2},{-2,-1},{1,-2},{-2,1},{-1,2},{2,-1}}; bool flag; char ch; void standred() { target[1][1]=1;target[1][2]=1;target[1][3]=1;target[1][4]=1;target[1][5]=1; target[2][1]=0;target[2][2]=1;target[2][3]=1;target[2][4]=1;target[2][5]=1; target[3][1]=0;target[3][2]=0;target[3][3]=2;target[3][4]=1;target[3][5]=1; target[4][1]=0;target[4][2]=0;target[4][3]=0;target[4][4]=0;target[4][5]=1; target[5][1]=0;target[5][2]=0;target[5][3]=0;target[5][4]=0;target[5][5]=0; } int find() { int c=0; for(int i=1;i<=5;i++) for(int j=1;j<=5;j++) if(mp[i][j]!=target[i][j])c++; return c; } void dfs(int now,int x,int y,int k) { if(flag==1)return; int c=find(); if(now==k) { if(c==0) {ans=k;flag=1; return;} } if(now+c-1>k)return; for(int i=0;i<8;i++) { int ex=x+e[i][0],ey=y+e[i][1]; if(ex<=5&&ex>=1&&ey<=5&&ey>=1) { swap(mp[x][y],mp[ex][ey]); dfs(now+1,ex,ey,k); swap(mp[x][y],mp[ex][ey]); } } } int main() { int T; cin>>T; standred(); while(T--) { for(int i=1;i<=5;i++) for(int j=1;j<=5;j++) { cin>>ch; if(ch=='*')sx=i,sy=j,map[i][j]=2; if(ch=='0')map[i][j]=0; if(ch=='1')map[i][j]=1; } for(int i=1;i<=15;i++) { flag=0;ans=20; for(int i=1;i<=5;i++) for(int j=1;j<=5;j++) mp[i][j]=map[i][j]; dfs(0,sx,sy,i); if(ans==i)break; } if(ans<=15) cout<<ans<<endl; else cout<<-1<<endl; } }
例题7
#include<iostream> #include<cstdio> #include<algorithm> #define maxn 10010 using namespace std; int n,k,f[maxn]; struct node { int l,r,nxt; bool operator < (const node b)const { return l<b.l; } } a[maxn]; int search(int cur) { if(f[cur])return f[cur]; int res=0x7fffffff,limit=0x7fffffff; for(int i=cur+1; i<=k; i++) { if(a[i].l>limit)break; else if(a[i].l>a[cur].r) { res=min(res,search(i)); limit=min(limit,a[i].l); } } f[cur]=a[cur].r-a[cur].l+1; if(res!=0x7fffffff)return f[cur]+=res; else return f[cur]; } int main() { scanf("%d%d",&n,&k); a[0].l=1; a[0].r=0; for(int i=1; i<=k; i++)scanf("%d%d",&a[i].l,&a[i].r),a[i].r+=a[i].l-1; sort(a+1,a+k+1); search(0); printf("%d",n-f[0]); return 0; }