Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
zhhf96
C-Plus-Plus-TheAlgorithms
提交
ea3071d5
C
C-Plus-Plus-TheAlgorithms
项目概览
zhhf96
/
C-Plus-Plus-TheAlgorithms
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
C-Plus-Plus-TheAlgorithms
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
ea3071d5
编写于
8月 27, 2020
作者:
F
Filip Hlasek
提交者:
GitHub
8月 27, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix: clang-format for hamiltons_cycle. (#1054)
上级
eee5f949
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
66 addition
and
66 deletion
+66
-66
graph/hamiltons_cycle.cpp
graph/hamiltons_cycle.cpp
+66
-66
未找到文件。
graph/hamiltons_cycle.cpp
浏览文件 @
ea3071d5
...
...
@@ -28,49 +28,49 @@
* @return `false` if there is no Hamiltonian cycle in the graph
*/
bool
hamilton_cycle
(
const
std
::
vector
<
std
::
vector
<
bool
>>
&
routes
)
{
const
size_t
n
=
routes
.
size
();
// height of dp array which is 2^n
const
size_t
height
=
1
<<
n
;
std
::
vector
<
std
::
vector
<
bool
>>
dp
(
height
,
std
::
vector
<
bool
>
(
n
,
false
));
const
size_t
n
=
routes
.
size
();
// height of dp array which is 2^n
const
size_t
height
=
1
<<
n
;
std
::
vector
<
std
::
vector
<
bool
>>
dp
(
height
,
std
::
vector
<
bool
>
(
n
,
false
));
// to fill in the [2^i, i] cells with true
for
(
size_t
i
=
0
;
i
<
n
;
++
i
)
{
dp
[
1
<<
i
][
i
]
=
true
;
}
for
(
size_t
i
=
1
;
i
<
height
;
i
++
)
{
std
::
vector
<
size_t
>
zeros
,
ones
;
// finding positions with 1s and 0s and separate them
for
(
size_t
pos
=
0
;
pos
<
n
;
++
pos
)
{
if
((
1
<<
pos
)
&
i
)
{
ones
.
push_back
(
pos
);
}
else
{
zeros
.
push_back
(
pos
);
}
// to fill in the [2^i, i] cells with true
for
(
size_t
i
=
0
;
i
<
n
;
++
i
)
{
dp
[
1
<<
i
][
i
]
=
true
;
}
for
(
size_t
i
=
1
;
i
<
height
;
i
++
)
{
std
::
vector
<
size_t
>
zeros
,
ones
;
// finding positions with 1s and 0s and separate them
for
(
size_t
pos
=
0
;
pos
<
n
;
++
pos
)
{
if
((
1
<<
pos
)
&
i
)
{
ones
.
push_back
(
pos
);
}
else
{
zeros
.
push_back
(
pos
);
}
}
for
(
auto
&
o
:
ones
)
{
if
(
!
dp
[
i
][
o
])
{
continue
;
}
for
(
auto
&
o
:
ones
)
{
if
(
!
dp
[
i
][
o
])
{
continue
;
}
for
(
auto
&
z
:
zeros
)
{
if
(
!
routes
[
o
][
z
])
{
continue
;
for
(
auto
&
z
:
zeros
)
{
if
(
!
routes
[
o
][
z
])
{
continue
;
}
dp
[
i
+
(
1
<<
z
)][
z
]
=
true
;
}
}
dp
[
i
+
(
1
<<
z
)][
z
]
=
true
;
}
}
}
bool
is_cycle
=
false
;
for
(
size_t
i
=
0
;
i
<
n
;
i
++
)
{
is_cycle
|=
dp
[
height
-
1
][
i
];
if
(
is_cycle
)
{
// if true, all subsequent loop will be true. hence
// break
break
;
bool
is_cycle
=
false
;
for
(
size_t
i
=
0
;
i
<
n
;
i
++
)
{
is_cycle
|=
dp
[
height
-
1
][
i
];
if
(
is_cycle
)
{
// if true, all subsequent loop will be true. hence
// break
break
;
}
}
}
return
is_cycle
;
return
is_cycle
;
}
/**
...
...
@@ -79,16 +79,16 @@ bool hamilton_cycle(const std::vector<std::vector<bool>> &routes) {
* @return None
*/
static
void
test1
()
{
std
::
vector
<
std
::
vector
<
bool
>>
arr
{
std
::
vector
<
bool
>
({
true
,
true
,
false
,
false
}),
std
::
vector
<
bool
>
({
false
,
true
,
true
,
false
}),
std
::
vector
<
bool
>
({
false
,
false
,
true
,
true
}),
std
::
vector
<
bool
>
({
false
,
false
,
false
,
true
})};
std
::
vector
<
std
::
vector
<
bool
>>
arr
{
std
::
vector
<
bool
>
({
true
,
true
,
false
,
false
}),
std
::
vector
<
bool
>
({
false
,
true
,
true
,
false
}),
std
::
vector
<
bool
>
({
false
,
false
,
true
,
true
}),
std
::
vector
<
bool
>
({
false
,
false
,
false
,
true
})};
bool
ans
=
hamilton_cycle
(
arr
);
std
::
cout
<<
"Test 1... "
;
assert
(
ans
);
std
::
cout
<<
"passed
\n
"
;
bool
ans
=
hamilton_cycle
(
arr
);
std
::
cout
<<
"Test 1... "
;
assert
(
ans
);
std
::
cout
<<
"passed
\n
"
;
}
/**
...
...
@@ -101,17 +101,17 @@ static void test1() {
* @return None
*/
static
void
test2
()
{
std
::
vector
<
std
::
vector
<
bool
>>
arr
{
std
::
vector
<
bool
>
({
true
,
true
,
false
,
false
}),
std
::
vector
<
bool
>
({
false
,
true
,
true
,
true
}),
std
::
vector
<
bool
>
({
false
,
false
,
true
,
false
}),
std
::
vector
<
bool
>
({
false
,
false
,
false
,
true
})};
std
::
vector
<
std
::
vector
<
bool
>>
arr
{
std
::
vector
<
bool
>
({
true
,
true
,
false
,
false
}),
std
::
vector
<
bool
>
({
false
,
true
,
true
,
true
}),
std
::
vector
<
bool
>
({
false
,
false
,
true
,
false
}),
std
::
vector
<
bool
>
({
false
,
false
,
false
,
true
})};
bool
ans
=
hamilton_cycle
(
arr
);
bool
ans
=
hamilton_cycle
(
arr
);
std
::
cout
<<
"Test 2... "
;
assert
(
!
ans
);
// not a cycle
std
::
cout
<<
"passed
\n
"
;
std
::
cout
<<
"Test 2... "
;
assert
(
!
ans
);
// not a cycle
std
::
cout
<<
"passed
\n
"
;
}
/**
...
...
@@ -120,17 +120,17 @@ static void test2() {
* @return None
*/
static
void
test3
()
{
std
::
vector
<
std
::
vector
<
bool
>>
arr
{
std
::
vector
<
bool
>
({
true
,
true
,
true
,
true
}),
std
::
vector
<
bool
>
({
true
,
true
,
true
,
true
}),
std
::
vector
<
bool
>
({
true
,
true
,
true
,
true
}),
std
::
vector
<
bool
>
({
true
,
true
,
true
,
true
})};
std
::
vector
<
std
::
vector
<
bool
>>
arr
{
std
::
vector
<
bool
>
({
true
,
true
,
true
,
true
}),
std
::
vector
<
bool
>
({
true
,
true
,
true
,
true
}),
std
::
vector
<
bool
>
({
true
,
true
,
true
,
true
}),
std
::
vector
<
bool
>
({
true
,
true
,
true
,
true
})};
bool
ans
=
hamilton_cycle
(
arr
);
bool
ans
=
hamilton_cycle
(
arr
);
std
::
cout
<<
"Test 3... "
;
assert
(
ans
);
std
::
cout
<<
"passed
\n
"
;
std
::
cout
<<
"Test 3... "
;
assert
(
ans
);
std
::
cout
<<
"passed
\n
"
;
}
/**
...
...
@@ -140,8 +140,8 @@ static void test3() {
* @param argv commandline array of arguments (ignored)
*/
int
main
(
int
argc
,
char
**
argv
)
{
test1
();
test2
();
test3
();
return
0
;
test1
();
test2
();
test3
();
return
0
;
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录