I - Beat HDU - 2614 DFS
程序员文章站
2022-05-10 10:35:05
Beat Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2184 Accepted Submission(s): 1256 Problem De ......
Beat
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2184 Accepted Submission(s):
1256
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.
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.
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
3
0 0 0
1 0 1
1 0 0
3
0 2 2
1 0 1
1 1 0
5
0 1 2 3 1
0 0 2 3 1
0 0 0 3 1
0 0 0 0 2
0 0 0 0 0
Sample Output
3
2
4
Hint
Hint: sample one, as we know zty always solve problem 0 by costing 0 minute.
So after solving problem 0, he can choose problem 1 and problem 2, because T01 >=0 and T02>=0.
But if zty chooses to solve problem 1, he can not solve problem 2, because T12 < T01.
So zty can choose solve the problem 2 second, than solve the problem 1. 题意:
一个小孩要做n道题,且这n道题难度不同。mp[i][j]代表做完第i道题后,再做第j道题的难度系数,每次做题难度系数不断上升,问最多做题数。
思路:
简单DFS,具体看代码:
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #define INF 0x3f3f3f3f 5 #define N 20 6 using namespace std; 7 int vis[N];//标记 8 int mp[N][N]; 9 int n; 10 int ans; 11 void DFS(int s,int len,int num){//s--下一道题,len--做题的数量,num--记录上一题的难度系数 12 int flag=0; 13 for(int j=0;j<n;j++){ 14 if(!vis[j]&&s!=j&&mp[s][j]>=num){//如果没有标记并且难度系数上升 15 vis[j]=1; 16 DFS(j,len+1,mp[s][j]); 17 vis[j]=0; 18 flag=1; 19 } 20 } 21 if(!flag)ans=max(ans,len); 22 } 23 24 int main(){ 25 while(~scanf("%d",&n)){ 26 ans=-1; 27 memset(vis,0,sizeof(vis)); 28 for(int i=0;i<n;i++){ 29 for(int j=0;j<n;j++) 30 scanf("%d",&mp[i][j]); 31 } 32 vis[0]=1; 33 DFS(0,1,0); 34 printf("%d\n",ans); 35 } 36 return 0; 37 }
下一篇: 笑弯了腰的校园糗事