Granny's Bike(哈密尔顿回路--深搜,回溯)

本文介绍了一种解决Granny骑自行车路径问题的算法实现,旨在找到一条不重复经过任一地点的环形路径。通过深度优先搜索(DFS)方法,程序能够判断给定地图上是否存在这样的路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Granny's Bike
Most days Granny rides her bike around town to do errands, visit, have a cup of coffee, and so on. She enjoys riding her bike and wants to avoid passing the same place twice to add to the interest of the ride. So, each day she draws a map of the places to be visited, with lines connecting those near each other, and sees if she can visit them all and return home without passing a place more than once. Some days she finds she can do this and other days she finds she can't. For example, for the map on the left, Granny can visit every place and return home without passing any place twice, but she can't do it for the map on the right.

She turns to you to write a program to help her.


Input

There will be multiple test cases for this problem. Each test case will have input on multiple lines. The first line will contain the integer n (< 10) noting the number of places Granny wants to visit that day. These will be numbered 1 through n and Granny's house will be numbered 0. The next n lines will be a list of those places near each spot. The first line will be a list of places with a direct route from place 1. The second line will be a list of places with a direct route from place 2, and so on. You may assume that if place i has a direct route to place j, then there is a direct route the other direction also. A line containing 0 will follow the last test case.


Output

For each test case, print one line of the form:

Case m: Granny can make the circuit.

or

Case m: Granny can not make the circuit.

as appropriate. Here, m is the number of the test case, starting at 1.


Sample Input

5
0 2 5
0 1 3
2 4
0 3 5
1 4
4
0 2 3 4
1 3
1 2
0 1
0


Sample Output

Case 1: Granny can make the circuit.
Case 2: Granny can not make the circuit.

解析:题意是Granny绕城市 骑自行车,要求是除了她自己的城市外,其他的城市都要到达但仅能到达一次。这就是图中回路问题。

开始看错了题意,以为下面的每行是代表相邻数之间是连通的,一直wa。后来经同学提醒,原来下面的n行代表的是第i行就是城市i

到后面城市是连通的。

#include"stdio.h"
 #include"string.h"
 #define max 11
 int edges[max][max];
 int visited[11];
 int N,M,x,y,count,flag;
 void DFSM(int edges[][max],int i)
 {
 int j;
 
 if(count==N)
 {
 if(edges[i][0]==1)
   {
flag=1;
return;
}
 }
 for(j=1;j<=N;j++)
 if(edges[i][j]==1&&!visited[j])
 {
  visited[j]=1;
  count++;
  DFSM(edges,j);
  visited[j]=0;                   // 回溯
  count--;
 }
 
 }
 int main()
 {
 char a[1000];
 int k,i,ss=0,x;
 while(scanf("%d",&N)!=EOF&&N)
 {
  ss++;
  count=0;
  x=0;
 getchar();
 k=0;
 flag=0;
 M=N;
visited[0]=1;
 memset(edges,0,sizeof(edges));
 memset(visited,0,sizeof(visited));
 while(M--)
 {
    x++;
  gets(a);
  int len=strlen(a);
  for(i=0;i<len;i++)
  {
if(a[i]>='0'&&a[i]<='9')
{
y=a[i]-'0';
edges[x][y]=1;
        edges[y][x]=1;
       }
  }
 }
DFSM(edges,0);
 if(flag==1)
 printf("Case %d: Granny can make the circuit.\n",ss);
 else
 printf("Case %d: Granny can not make the circuit.\n",ss);
 }
 return 0;
 }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值