频道栏目
首页 > 程序开发 > 软件开发 > C++ > 正文
ACM-DFS之Beat——hdu2614
2014-04-03 11:20:16         来源: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 
#include 
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=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>problem[i][j];

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

点击复制链接 与好友分享!回本站首页
相关TAG标签
上一篇:POJ 3243 大步小步算法
下一篇:hdu 2108 Shape of HDU
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站