I - Beat HDU - 2614 DFS

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 }

原文地址:https://www.cnblogs.com/by-DSL/p/8654660.html

时间: 2024-12-10 05:59:13

I - Beat HDU - 2614 DFS的相关文章

hdu 4109 dfs+剪枝优化

求最久时间即在无环有向图里求最远路径 dfs+剪枝优化 从0节点(自己增加的)出发,0到1~n个节点之间的距离为1,mt[i]表示从0点到第i个节点目前所得的最长路径 #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<vector> using namespace std; const

HDU 1015 dfs回溯

题目真长.....看了好长时间才看懂.. 就是给你一个32位数字和一个最多15个字符的字符串,从字符串中选出5个字符,若满足题中给的那个式子,输出字典序最大的那5个字符,若不满足,输出no solution. 为了解决字典序问题,在输入字符串后,把字符串按从大到小排一下序,搜索一下若满足条件输出即可. 贴代码. 1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 #include <

hdu 1312 DFS算法简单题

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 此题与油田那题很像是练习深搜的好题,题意是从"@"开始,遍历整个图,找到连接的 "."有多少个 但要考虑变化,简单处理下就行了,主要是斜角的不要了,而且判断结束方式也不一样 具体看代码吧 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

HDU 5143 DFS

分别给出1,2,3,4   a, b, c,d个 问能否组成数个长度不小于3的等差数列. 首先数量存在大于3的可以直接拿掉,那么可以先判是否都是0或大于3的 然后直接DFS就行了,但是还是要注意先判合法能否进入下层递归来减少内存消耗. /** @Date : 2017-09-27 15:08:23 * @FileName: HDU 5143 DFS.cpp * @Platform: Windows * @Author : Lweleth ([email protected]) * @Link :

HDU 1045 DFS暴搜

Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6229    Accepted Submission(s): 3506 Problem Description Suppose that we have a square city with straight streets. A map of a city is a s

hdu 2212 DFS(水题)

DFS Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4923    Accepted Submission(s): 3029 Problem Description A DFS(digital factorial sum) number is found by summing the factorial of every digit

HDU 2614 Beat(DFS)

题目链接 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 solvea problem that is easy than problem he had solved. Now yifenfe

HDU 2614 Beat 深搜DFS

这道题目还是比较水的,但是题意理解确实费了半天劲,没办法 谁让自己是英渣呢! 题目大意: 猪脚要解决问题, 他有个习惯,每次只解决比之前解决过的问题的难度要大. 他给我们一个矩阵  矩阵的 i 行 j 列表示 解决完第 i 个问题后再解决第 j 个问题 花费时间为 T[i][j] 也就是 题目的难度. 并且他是从第0个问题开始解决的,第0个问题花费的时间为 0 下面是代码 : 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include&l

DFS HDU 2614

很水的.但是wa了很多遍,因为写num[1][1]... 改成0之后就过了...虽然不知道为什么... Beat Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 659    Accepted Submission(s): 415 Problem Description Zty is a man that always full of