学习总结3

本文介绍了一种使用深度优先搜索(DFS)的方法来解决一个涉及整数矩阵的问题,目标是找到从左上角到右下角的最大路径和,通过递归遍历并更新最大值。
摘要由CSDN通过智能技术生成

解题思路

利用dfs进行遍历,直到无法遍历就把此次遍历解出的题目与最大值进行比较,遍历完后输出最大值。

代码

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
using namespace std;
int g[20][20];
int j[20];
int n,max1;
void dfs(int i,int k,int sum)
{
    for(int x=0;x<n;x++)
    {
        if(g[i][x]>=sum&&j[x]==0)
        {
            j[x]=1;
            dfs(x,k+1,g[i][x]);
            j[x]=0;
        }
    }
    if(k>max1)
    {
        max1=k;
    }
    return ;
}
int main()
{
    while(~scanf("%d",&n))
    {
        for(int x=0;x<n;x++)
        {
            for(int y=0;y<n;y++)
            scanf("%d",&g[x][y]);
        }
        max1=0;
        j[0]=1;
        dfs(0,1,0);
        j[0]=0;
        printf("%d\n",max1);
    }
    return 0;
}

java

初步了解了方法的定义与使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值