hdu2614 Beat–DFS
最后更新于:2022-04-01 19:58:38
原题链接:[http://acm.hdu.edu.cn/showproblem.php?pid=2614](http://acm.hdu.edu.cn/showproblem.php?pid=2614)
**一:原题内容**
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道题,但是他有一个习惯,每做一题,该题必须比上一次做的题的难度大,难度大小根据做题所需的时间判断。
**三:AC代码**
~~~
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
#include
#include
#define max(a, b) (((a) > (b)) ? (a) : (b))
using namespace std;
bool visit[16]; //从0到n-1标记题号是否做过
int time[16][16]; //
int n; //
int sum; //记录做出题的最大数
void DFS(int, int, int);
int main()
{
while (cin >> n)
{
memset(visit, 0, sizeof(visit));
sum = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cin >> time[i][j];
DFS(0, 0, 1);//第二个参数只要不大于0就行,也就是小于等于0皆可
visit[0] = 1;//解决第一题
cout << sum << endl;
}
return 0;
}
void DFS(int cur,int lastTime,int cnt)//cur代表现在正要解决的题号(从0到n-1),也就是time数组的横坐标;lastTime是上一次做题的时间;cnt是已经解决了多少题
{
sum = max(sum, cnt);//这里要随时更新,因为题意是找到所能做出的题目的最大值
if (sum == n)//如果能把所有题目做完,那还做什么呢?停止!
return;
for (int i = 1; i < n; i++)//第一题已被解决,所以从1开始
{
if (!visit[i] && time[cur][i] >= lastTime)
{
visit[i] = 1;
DFS(i, time[cur][i], cnt+1);
visit[i] = 0;
}
}
}
~~~
';