你最愿意做的哪件事,才是你的天赋所在

0%

Treasure Cave

题目链接

Treasure Cave

题目描述

实际上就是求二叉树的最短遍历,输出路径

思路

采用递归的方式,先不停的深入下去,找到点后就输出,在递归的过程中输出即可。

代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<iostream>
using namespace std;
int pre[5010];
int solve(int x,int m)
{
if(m==1)
{
cout<<x<<endl;
return 1;
}
if(solve(x+1,pre[m]))cout<<pre[m]<<endl;
}
int main()
{
int n,s,t;
while(cin>>n>>s>>t)
{
int mm,u,p;
for(int i=1;i<=n;i++)pre[i]=i;
for(int i=1;i<=s;i++)
{
cin>>mm>>u>>p;
pre[u]=pre[p]=mm;
}
if(solve(1,t))cout<<t<<endl;
}
}
-------------你最愿意做的哪件事才是你的天赋所在-------------