ZOJ1395(Door Man)

本文介绍了一个通过输入房间间连接关系判断是否能形成欧拉路径或欧拉回路的算法实现。该算法首先读取起始房间及房间总数,然后通过一系列字符串输入解析出各个房间之间的连接关系,并计算每个房间的度数。最后根据欧拉路径与回路的条件输出判断结果。
//2009-05-14 10:26:41        Accepted      1395      C++      0      184
#include <iostream>
#include 
<cstdlib>
#include 
<cctype>
#include 
<cstring>
using namespace std;

const int N = 25
;

int
 main()
ExpandedBlockStart.gifContractedBlock.gif
{
    
char ch[100
];
    
int m, n, cnt= 0
;
    
int
 i, j, len, a, odd_cnt;
    
int
 degree[N];

    scanf(
"%s"
, ch);

    
while(strcmp(ch, "ENDOFINPUT"!= 0
)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
//
M indicates the butler's starting room, and N indicates 
        
//the number of rooms in the house (1 <= N <= 20). 

        scanf("%d%d",&m, &n);
        cnt 
= 0
;
        memset(degree, 
0sizeof
(degree));
        getchar();
        
        
for(i = 0; i < n; i++
)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            gets(ch);
//getchar();

            len = strlen(ch);
            
for(j = 0;j < len; j++
)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if
(isdigit(ch[j]))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    a 
= atoi(ch +
 j);
                    
while(isdigit(ch[j]) && j <
 len)
                        j
++
;
                    degree[i]
++
;
                    degree[a]
++
;
                    cnt
++
;
                }

            }

        }

        gets(ch);

        
for(odd_cnt = 0, i = 0; i < n; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if(odd_cnt > 2
)
                
break
;
            
if(degree[i] % 2 != 0
)
                odd_cnt
++
;
        }


        
if(odd_cnt > 2 || odd_cnt == 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{//不能形成欧拉通路,也不能形成欧拉回路的情况
            printf("NO\n");
        }

        
else if(odd_cnt == 2)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{//对形成欧拉通路的情况分析
            if(m != 0 && degree[m] %2 != 0 && degree[0% 2 != 0)
                printf(
"YES %d\n"
, cnt);
            
else

                printf(
"NO\n");
        }

        
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{//对行程欧拉回路的情况分析
            if(m == 0)
                printf(
"YES %d\n"
, cnt);
            
else

                printf(
"NO\n");

        }


        scanf(
"%s", ch);

    }

    
return 0;
}

转载于:https://www.cnblogs.com/Xredman/archive/2009/05/14/1456574.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值