HDU 2614 Beat(dfs)

来源:互联网 发布:网络策划 编辑:程序博客网 时间:2024/06/05 05:20

Beat

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1647    Accepted Submission(s): 972


Problem Description
Zty is a man that always full of enthusiasm. He wants to solve every kind of difficulty ACM problem in the world. And he has a habit that he does not like to solve
a problem that is easy than problem he had solved. Now yifenfei give him n difficulty problems, and tell him their relative time to solve it after solving the other one.
You should help zty to find a order of solving problems to solve more difficulty problem.
You may sure zty first solve the problem 0 by costing 0 minute. Zty always choose cost more or equal time’s problem to solve.
 

Input
The input contains multiple test cases.
Each test case include, first one integer n ( 2< n < 15).express the number of problem.
Than n lines, each line include n integer Tij ( 0<=Tij<10), the i’s row and j’s col integer Tij express after solving the problem i, will cost Tij minute to solve the problem j.
 

Output
For each test case output the maximum number of problem zty can solved.


 

Sample Input
30 0 01 0 11 0 030 2 21 0 11 1 050 1 2 3 10 0 2 3 10 0 0 3 10 0 0 0 20 0 0 0 0
 

Sample Output
324

题意:T(i,j)表示的是当做完i题然后做j题,j题需要花费的时间,从0题开始做,花费时间0,要求找能解出的最多个数(花费时间是不递减的)

思路:dfs搜索即可

AC代码:

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=16;int g[maxn][maxn];int n;int vis[maxn];int dfs(int i,int cur){ //i表示之前前做的题,cur表示 i对应的时间 int ans=1;for(int j=1;j<n;j++){if(!vis[j] && cur<=g[i][j]){vis[j]=1;ans=max(ans,dfs(j,g[i][j])+1);vis[j]=0;}}return ans;}int main(){while(scanf("%d",&n)==1){memset(g,-1,sizeof(g));memset(vis,0,sizeof(vis));for(int i=0;i<n;i++) for(int j=0;j<n;j++) scanf("%d",&g[i][j]);         int ans=dfs(0,0);  printf("%d\n",ans);}return 0;}







1 0
原创粉丝点击