| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1042 人关注过本帖
标题:Granny's Bike(哈密顿回路问题,不知道怎么下手)
只看楼主 加入收藏
心剑菩提
Rank: 1
等 级:新手上路
帖 子:249
专家分:0
注 册:2007-5-17
收藏
 问题点数:0 回复次数:1 
Granny's Bike(哈密顿回路问题,不知道怎么下手)
Description

Most days Granny rides her bike around town to do errands, visit, have a cup of co®ee, 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 place1. 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.




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.


Hint

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

Source

[[it] 本帖最后由 心剑菩提 于 2008-8-8 16:00 编辑 [/it]]

1010.jpg (368.71 KB)
图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: Granny Bike she days bike 
2008-08-08 15:59
心剑菩提
Rank: 1
等 级:新手上路
帖 子:249
专家分:0
注 册:2007-5-17
收藏
得分:0 
刚作的正确代码
#include"stdio.h"
#include"string.h"

int a[12][12],n;
int visited[12];

int DFS(int x,int y)
{
    int i;
    for(i=1;i<=n;i++)
    {
        if(y==n&&a[x][0])    return 1;
        if(!visited[i]&&a[x][i])
        {
            visited[x]=1;
            if(DFS(i,y+1))  return 1;
            visited[x]=0;
        }
    }
    return 0;
}
int main()
{
    int i,j,t=1;
    char c;
    while(scanf("%d",&n)&&n!=0)
    {
        memset(a,0,sizeof(a));
        memset(visited,0,sizeof(visited));
        for(i=1;i<=n;i++)
        {
            while(1)
            {
                scanf("%d",&j);
                a[i][j]=a[j][i]=1;
                c=getchar();
                if(c=='\n')    break;
            }
        }
        if(DFS(0,0))
            printf("Case %d: Granny can make the circuit.\n",t++);
        else
            printf("Case %d: Granny can not make the circuit.\n",t++);
    }
    return 0;
}

前世五百次的回眸 才换来今生的擦肩而过
2008-08-17 16:05
快速回复:Granny's Bike(哈密顿回路问题,不知道怎么下手)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016171 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved