首页 > 代码库 > POJ 1321

POJ 1321

dfs 简单题

 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 using namespace std; 5 int vis[9][9]; 6 int mat[9][9],n,k,ans; 7  8 void dfs(int x,int y,int t) 9 {10     //if(k-t > n - y + 1 + (n-x+1)*n) return;11     if(t == k){12        // cout<<"test:"<<t<<" "<<x<<" "<<y<<endl;13         ans++;14         return;15     }16     if(y>n){17         dfs(x+1,1,t);18         return;19     }20     if(x>n) return;21     if(!vis[x][y] && mat[x][y]){22         //cout<<"in : "<<vis[x][y]<<" "<<x<<" "<<y<<endl;23         for(int i=1;i<=n;i++)24             vis[x][i]++ , vis[i][y]++;25         dfs(x,y+1,t+1);26         for(int i=1;i<=n;i++)27             vis[x][i]-- , vis[i][y]--;28     }29     dfs(x,y+1,t);30 }31 32 int main()33 {34     freopen("test.in","rb",stdin);35     char a;36     while(scanf("%d%d",&n,&k)!=EOF){37         if(n == -1 && k==-1)38             break;39         for(int i=1;i<=n;i++)40             for(int j=1;j<=n;j++){41                 cin>>a;42                 if(a == #) mat[i][j] = 1;43                 else mat[i][j] = 0;44             }45 46         memset(vis,0,sizeof(vis));47         ans = 0;48         dfs(1,1,0);49         printf("%d\n",ans);50     }51     return 0;52 }

 

POJ 1321