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代码有何区别?为何一个AC一个TLE?个人感觉刚种写法等价啊bool dfs(int num,int start,int leftstick,int lefttotal) //num 小棍数目, start 当前起点 ,leftstick 当前小棍剩余长度,lefttotal 剩余总长度 { int i,pre=-1; if (leftstick==0) { leftstick=len; num--; start=0; } if (num==0) { return true; } if (leftstick>lefttotal) { return false; } for (i=start;i<n;++i) { if ((st[i])&&(pre!=stick[i])&&(leftstick>=stick[i])) { pre=st[i]; st[i]=false; if (dfs(num,i+1,leftstick-stick[i],lefttotal-stick[i])) { return true; } else { st[i]=true; if ((leftstick-stick[i])==0) { return false; } } } } return false; } bool dfs(int num,int start,int leftstick,int lefttotal) { int i,pre=-1; if (leftstick==0) { leftstick=len; num--; } if (num==0) { return true; } if (leftstick>lefttotal) { return false; } if (leftstick==len) { start=0; while (!st[start]) { start++; } st[start]=false; if (dfs(num,start+1,leftstick-stick[start],lefttotal-stick[start])) { return true; } num++; leftstick=0; st[start]=true; return false; } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator