解题思路
利用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
初步了解了方法的定义与使用。