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 |
新手DFS求32ms优化到16ms方法(附代码)#include<stdio.h> #include<string.h> int n,sum,k,x[9],y[9]; char a[9][9]; void dfs(int f,int t) { if(t==k) { ++sum; return; } for(int i=f;i<n;++i) for(int j=0;j<n;++j) { if(y[j]==1)continue; if(a[i][j]=='.')continue; y[j]=1; dfs(i+1,t+1); y[j]=0; } } int main() { char s[9]; while(~scanf("%d%d",&n,&k)&&(n!=-1||k!=-1)) { sum=0; memset(y,0,sizeof(y)); for(int i=0;i<n;++i) { scanf("%s",s); for(int j=0;j<n;++j) a[i][j]=s[j]; } dfs(0,0); printf("%d\n",sum); } } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator