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 |
没特别搞什么优化 dfs297ms第一次理解成选一些列使每行均有且只有一个一 想着正好用状压 果断悲剧。。。 后来又写错一个循环上界 wa了数次后 猛然发现 感觉我的代码挺短的 来和大家分享一下 #include<cstdio> #include<iostream> #include<cstring> using namespace std; int state[20][320] , m , n , num[20]; bool flag; void dfs(int l , int*p , int left) { int tem[320] , i , j; if(left==0) { flag = true; return; } for (i = l; i <=m ; i++) { if(num[i]==0) continue; if(num[i]>left) continue; for (j = 1 ; j <= n ; j++) { if((state[i][j]&p[j])!=0) break; tem[j] = (state[i][j]|p[j]); } if (j>n) dfs(i+1,tem,left-num[i]); if (flag) return; } return; } int main() { int i , j , p[320]; while(cin>>m>>n) { flag = false; memset(p,0,sizeof(p)); memset(num,0,sizeof(num)); for (i = 1 ; i <= m ; i++) { for (j = 1 ; j <= n ; j++) { scanf("%d",&state[i][j]); if(state[i][j]==1) num[i]++; } } dfs(1,p,n); if(flag) printf("Yes, I found it\n"); else printf("It is impossible\n"); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator