HDU-P2614:Beat[dfs搜索]

HDU-P2614: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 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

思路

从题目可以容易看出是搜索题目,但是要进行搜索的图和之前直观的二维数组表示的图有一些差别,更加类似于邻接表的储存方式。
如果能很好的理解邻接表那么这个题目的代码完全没有难度。

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int mp[16][16], n;
int vis[16];
int ans;
void dfs(int cur, int t, int num) //分别是当前要做的题目,上一个题目的难度和当前总共解决问题的数目
{
    for(int i = 0; i < n; ++i) {
        if(i == cur || vis[i]) //不能和当前题目相同,不能做已经做过的题目
            continue;
        if(mp[cur][i] >= t) {
            vis[i] = 1;
            dfs(i, mp[cur][i], num+1);
            vis[i] = 0;
        }
    }
    ans = max(ans, num);
}
int main(void)
{
    while(~scanf("%d", &n)) {
        memset(vis, 0, sizeof(vis));
        ans = 1;
        vis[0] = 1;
        for(int i = 0; i < n; ++i)
            for(int j = 0; j < n; ++j)
                scanf("%d", &mp[i][j]);
        dfs(0, 0, 1);
        printf("%d\n", ans);
    }
    return 0;
}
posted @   JACK121385  阅读(8)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示