Granny's Bike(哈密尔顿回路--深搜,回溯)
来源:互联网 发布:网络彩票代理怎么做的 编辑:程序博客网 时间:2024/04/30 04:45
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
50 2 50 1 32 40 3 51 440 2 3 41 31 20 10
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;
}
- Granny's Bike(哈密尔顿回路--深搜,回溯)
- SHU2011 Granny's Bike(哈密顿回路+回溯法)
- TOJ 1529. Granny's Bike--哈密顿回路
- zju1798 Granny's Bike (DFS)
- zoj 1798 Granny's Bike
- ZOJ 1798 Granny's Bike
- 初学回溯,哈密尔顿回路问题的实现
- poj2438 Children's Dining(哈密尔顿回路)
- 哈密尔顿回路
- 哈密尔顿回路
- 竞赛图找哈密尔顿回路(hdu3414)
- 哈密尔顿回路 C++
- 哈密尔顿回路问题
- HDU3414:Tour Route(竞赛图 + 哈密尔顿回路)
- AGC018:Tree and Hamilton Path(dfs & 树 哈密尔顿回路)
- [c++,algorithm] 哈密尔顿回路判断
- “欧拉回路”与“哈密尔顿回路”
- 欧拉回路和哈密尔顿回路
- thinkphp二级域名配置
- 【干货:华为的股权、研发、治理】
- Python 获取接口数据,解析JSON,写入文件
- 【hdu3473】【划分树】Mininum Sum
- unity基础开发----Unity3D的uniSWF注意事项
- Granny's Bike(哈密尔顿回路--深搜,回溯)
- myeclipse10程序下载和破解工具的使用说明
- 【shell】给变量赋默认值
- ngcore 环境配置
- java kindeditor 上传图片问题
- 【COCOS2DX-LUA 脚本开发之六】利用Lua强转函数解决使用CCNode报错或无法正常使用以及简单介绍 quick-cocos2d-x 与 OpenQuick 两款Lua免费开源框架
- Server服务器面试题
- 对称加密和分组加密中的四种模式(ECB、CBC、CFB、OFB)
- Source Insight常用快捷键及注释快捷键设置