ACM-DFS之Beat——hdu2614

Beat
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.


依旧是DFS练习题,这道题讲述的是一个孩子要做n个题目,每个题目难度不同,这个孩子有一个脾气,就是做的题目不会做比之前做的简单的。
题目中的Tij就是第i行第j列代表着,做完i号题目后做j题目所花费的时间,如果做i题目花费了2分钟,做完i后做j花费时间小于2分钟则,这个有脾气的小屁孩不会去鸟这道题,他只做花费时间大于等于2分钟的题目。
很有原则的小孩儿啊。
这个我主要想说的就是dfs函数中的flag,这个的设置比较巧妙。
代表每次最大值判断(maxx和sl的比较)都是在解决了这道题后的,
防止循环到一道题,发现做完这道题后面的题目都不能做    或者   做完所有题目后的判断。


代码:
#include <iostream>
#include <string.h>
using namespace std;
int problem[16][16],vis[16];
int n,maxx;

void dfs(int line,int pre,int sl)
{
    int i,flag=0;
    for(i=0;i<n;++i)
    {
        // 当前i是否访问过,或者i是否就是当前站点
        if(i==line || vis[i]==1)    continue;

        if(problem[line][i]>=pre)
        {
            vis[i]=1;
            dfs(i,problem[line][i],sl+1);
            vis[i]=0;
            flag=1;
        }
    }
    if(!flag)  maxx=sl>maxx?sl:maxx;
}

int main()
{
    int i,j;
    while(cin>>n)
    {
        memset(vis,0,sizeof(vis));
        for(i=0;i<n;++i)
            for(j=0;j<n;++j)
                cin>>problem[i][j];

        // 初始化maxx,开始dfs
        vis[0]=1;
        maxx=-1;
        dfs(0,0,1);
        cout<<maxx<<endl;
    }
    return 0;
}


你可能感兴趣的:(ACM,DFS,Beat,hdu2614)