1. 程式人生 > >I - Beat HDU - 2614 DFS

I - Beat HDU - 2614 DFS

圖片 span font cond lin tput first view string

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. 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 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 }
View Code

I - Beat HDU - 2614 DFS