hdu2614 Beat(简单dfs)

来源:互联网 发布:矩阵分解svd 视频 编辑:程序博客网 时间:2024/06/05 17:06


http://acm.hdu.edu.cn/showproblem.php?pid=2614

题意:给你一个二维数组,Map[i][j]代表做完i题做j题的难度。这个人做题不愿意做比上一道简单的,求他最多能做几道题。


思路:题中说从第0到题开始做,所以从0开始搜索,搜索过程中统计最大值即可。


#include <stdio.h>#include <algorithm>#include <stdlib.h>#include <string.h>#include <iostream>using namespace std;typedef __int64 LL;const int N = 20;const int INF = 0x3f3f3f3f;int Map[N][N], vis[N], ans, n;void dfs(int last, int num, int w){    ans = max(ans, num);    for(int i = 0; i < n; i++)    {        if(vis[i] == 1 || Map[last][i] < w) continue;        vis[i] = 1;        dfs(i, num+1, Map[last][i]);        vis[i] = 0;    }}int main(){   // freopen("in.txt", "r", stdin);    while(~scanf("%d", &n))    {        ans = 0;        for(int i = 0; i < n; i++)        {            for(int j = 0; j < n; j++)            {                scanf("%d", &Map[i][j]);            }        }        memset(vis, 0, sizeof(vis));        vis[0] = 1;        dfs(0, 1, 0);        printf("%d\n", ans);    }    return 0;}


0 0
原创粉丝点击