链接:戳这里
思路:
( 由于要应付某个比赛所以最近在练一些基础的爆搜
枚举每一行去爆搜,当前行的j列为'#' 标记这一列,终止条件即为 num==k || r>n
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<string> #include<vector> #include <ctime> #include<queue> #include<set> #include<map> #include<stack> #include<iomanip> #include<cmath> #define mst(ss,b) memset((ss),(b),sizeof(ss)) #define maxn 0x3f3f3f3f #define MAX 1000100 ///#pragma comment(linker, "/STACK:102400000,102400000") typedef long long ll; typedef unsigned long long ull; #define INF (1ll<<60)-1 using namespace std; int n,k; int a[110][110],vis[110],ans; void DFS(int r,int num){ if(num==k) { ans++; return ; } if(r>n) return ; for(int i=1;i<=n;i++){ if(a[r][i]==1 && !vis[i]){ vis[i]=1; DFS(r+1,num+1); vis[i]=0; } } DFS(r+1,num); } int main(){ while(scanf("%d%d",&n,&k)!=EOF){ if(n==-1 && k==-1) break; mst(a,0); mst(vis,0); ans=0; for(int i=1;i<=n;i++){ getchar(); for(int j=1;j<=n;j++){ char c; scanf("%c",&c); if(c=='#') a[i][j]=1; } } DFS(1,0); cout<<ans<<endl; } return 0; }
链接:戳这里
题意:在一个三维的空间里 要从S->E 就算逃出,问最少需要多少步 逃不出就Trapped!
思路:
典型的优先队列+BFS
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<string> #include<vector> #include <ctime> #include<queue> #include<set> #include<map> #include<stack> #include<iomanip> #include<cmath> #define mst(ss,b) memset((ss),(b),sizeof(ss)) #define maxn 0x3f3f3f3f #define MAX 1000100 ///#pragma comment(linker, "/STACK:102400000,102400000") typedef long long ll; typedef unsigned long long ull; #define INF (1ll<<60)-1 using namespace std; char mp[31][31][31]; int l,r,c; int vis[31][31][31]; struct node{ int z,x,y,step; node(int z=0,int x=0,int y=0,int step=0):z(z),x(x),y(y),step(step){} bool operator < (const node &a) const{ return step>a.step; } }; int xx[6]={0,0,1,-1,0,0}; int yy[6]={1,-1,0,0,0,0}; int zz[6]={0,0,0,0,1,-1}; bool pd(node t){ if(t.z<1 || t.z>l || t.x<1 || t.x>r || t.y<1 || t.y>c) return false; if(vis[t.z][t.x][t.y]==1 || mp[t.z][t.x][t.y]=='#') return false; return true; } priority_queue<node> qu; void BFS(node st){ while(!qu.empty()) qu.pop(); mst(vis,0); qu.push(st); vis[st.z][st.x][st.y]=1; while(!qu.empty()){ node now=qu.top(),next; qu.pop(); for(int i=0;i<6;i++){ next.z=now.z+zz[i]; next.x=now.x+xx[i]; next.y=now.y+yy[i]; if(pd(next)){ next.step=now.step+1; if(mp[next.z][next.x][next.y]=='E'){ printf("Escaped in %d minute(s).\n",next.step); return ; } vis[next.z][next.x][next.y]=1; qu.push(next); } } } printf("Trapped!\n"); } char s[33]; int main(){ while(scanf("%d%d%d",&l,&r,&c)!=EOF){ if(l==0 && r==0 && c==0) break; node st; for(int i=1;i<=l;i++){ for(int j=1;j<=r;j++){ scanf("%s",s); for(int k=0;k<strlen(s);k++){ mp[i][j][k+1]=s[k]; if(mp[i][j][k+1]=='S') st=node(i,j,k+1,0); } } } /*for(int i=1;i<=l;i++){ for(int j=1;j<=r;j++){ for(int k=1;k<=c;k++){ printf("%c ",mp[i][j][k]); } cout<<endl; } cout<<endl; }*/ BFS(st); } return 0; } /* 3 4 5 S.... .###. .##.. ###.# #.### ##### ##.## ##... E.### ##### #.### ####E */