Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:dfs一次过In Reply To:dfs一次过 Posted by:2698022795 at 2019-09-27 21:32:42 #include<iostream> using namespace std; const int maxn=9; char map[maxn][maxn]; int ans; int n,k; int visit[maxn]; void dfs(int x,int sum) { if(sum==k){ //cout<<"sum="<<sum<<" k="<<k<<endl; ans++; return; } if(x==n+1){ //cout<<"sum="<<sum<<" k="<<k<<endl; if(sum==k) ans++; return ; } for(int i=1;i<=n;i++){ if(map[x][i]=='#'&&!visit[i]){ // cout<<"map["<<x<<"]["<<i<<"]"<<endl; visit[i]=1; dfs(x+1,sum+1); visit[i]=0; } } dfs(x+1,sum); } int main() { while(scanf("%d%d",&n,&k)!=EOF&&(n!=-1||k!=-1)) { getchar(); for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) scanf("%c",&map[i][j]); getchar(); } for(int i=1;i<=maxn;i++) visit[i]=0; ans=0; dfs(1,0); printf("%d\n",ans); } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator