暑假训练之记录
ACM/ICPC
PKU 2286 The Rotation Game 题解
因为状态很难储存
所以款搜是不可以的
这个题从网上学了一个好的算法
用迭代深搜。每次卡一个深度然后去dfs
这样搜到解的时候就是那个卡的深度了。
知道方法后很简单
1
#include
<
string
.h
>
2
#include
<
stdio.h
>
3
const
int
N
=
25
;
4
int
a[N], ans[
100
];
5
#define
Max(a, b) ((a)>(b)?(a):(b))
6
int
f[
8
][
7
][
2
]
=
{
7
{
{
1
,
23
}
,
{
3
,
1
}
,
{
7
,
3
}
,
{
12
,
7
}
,
{
16
,
12
}
,
{
21
,
16
}
,
{
23
,
21
}
}
,
8
{
{
2
,
24
}
,
{
4
,
2
}
,
{
9
,
4
}
,
{
13
,
9
}
,
{
18
,
13
}
,
{
22
,
18
}
,
{
24
,
22
}
}
,
9
{
{
11
,
5
}
,
{
10
,
11
}
,
{
9
,
10
}
,
{
8
,
9
}
,
{
7
,
8
}
,
{
6
,
7
}
,
{
5
,
6
}
}
,
10
{
{
20
,
14
}
,
{
19
,
20
}
,
{
18
,
19
}
,
{
17
,
18
}
,
{
16
,
17
}
,
{
15
,
16
}
,
{
14
,
15
}
}
,
11
{
{
24
,
2
}
,
{
22
,
24
}
,
{
18
,
22
}
,
{
13
,
18
}
,
{
9
,
13
}
,
{
4
,
9
}
,
{
2
,
4
}
}
,
12
{
{
23
,
1
}
,
{
21
,
23
}
,
{
16
,
21
}
,
{
12
,
16
}
,
{
7
,
12
}
,
{
3
,
7
}
,
{
1
,
3
}
}
,
13
{
{
14
,
20
}
,
{
15
,
14
}
,
{
16
,
15
}
,
{
17
,
16
}
,
{
18
,
17
}
,
{
19
,
18
}
,
{
20
,
19
}
}
,
14
{
{
5
,
11
}
,
{
6
,
5
}
,
{
7
,
6
}
,
{
8
,
7
}
,
{
9
,
8
}
,
{
10
,
9
}
,
{
11
,
10
}
}
15
}
;
16
int
L, ANS;
17
int
check()
18
{
19
int
ll
=
a[
7
];
20
if
(a[
8
]
!=
ll
||
a[
9
]
!=
ll
||
a[
12
]
!=
ll
||
a[
13
]
!=
ll
||
21
a[
16
]
!=
ll
||
a[
17
]
!=
ll
||
a[
18
]
!=
ll)
22
return
0
;
23
return
ANS
=
ll;
24
}
25
int
cal()
26
{
27
int
num[
4
];
28
memset(num,
0
,
sizeof
(num));
29
for
(
int
i
=
7
;i
<=
9
;i
++
)num[a[i]]
++
;
30
for
(
int
i
=
12
;i
<=
13
;i
++
)num[a[i]]
++
;
31
for
(
int
i
=
16
;i
<=
18
;i
++
)num[a[i]]
++
;
32
return
8
-
Max(num[
3
], Max(num[
2
], num[
1
]));
33
}
34
35
36
int
di(
int
now)
37
{
38
int
i, b[N], j, t;
39
if
(now
==
L)
return
check();
40
int
c
=
cal();
41
if
(now
+
c
>
L)
return
0
;
42
for
(i
=
0
; i
<
8
;
++
i)
43
{
44
ans[now]
=
i;
45
memcpy(b, a,
sizeof
(a));
46
for
(j
=
0
; j
<
7
;
++
j) a[f[i][j][
1
]]
=
b[f[i][j][
0
]];
47
t
=
di(now
+
1
);
48
memcpy(a, b,
sizeof
(b));
49
if
(t
!=
0
)
return
t;
50
}
51
return
0
;
52
}
53
54
int
main()
{
55
int
i, t;
56
while
(scanf(
"
%d
"
,
&
a[
1
]), a[
1
])
57
{
58
for
(i
=
2
; i
<=
24
;
++
i)scanf(
"
%d
"
,
&
a[i]);
59
if
(check()
!=
0
)
60
{
61
printf(
"
No moves needed\n%d\n
"
, a[
7
]);
62
continue
;
63
}
64
L
=
1
;
65
while
( (t
=
di(
0
))
==
0
)L
++
;
66
for
(i
=
0
; i
<
L;
++
i)putchar(ans[i]
+
'
A
'
);
67
printf(
"
\n%d\n
"
, ANS);
68
}
69
return
0
;
70
}
71
72
posted on 2008-07-19 20:20
gong
阅读(1090)
评论(0)
编辑
收藏
引用
只有注册用户
登录
后才能发表评论。
网站导航:
博客园
博客园最新博文
博问
管理
Powered by:
C++博客
Copyright © gong
<
2008年7月
>
日
一
二
三
四
五
六
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
导航
C++博客
首页
新随笔
联系
聚合
管理
统计
随笔 - 50
文章 - 0
评论 - 22
引用 - 0
常用链接
我的随笔
我的评论
我参与的随笔
留言簿
(6)
给我留言
查看公开留言
查看私人留言
随笔档案
2009年7月 (11)
2008年8月 (1)
2008年7月 (38)
搜索
积分与排名
积分 - 29954
排名 - 669
最新评论
1. re: uva :: Programming Challenges :: Chapter 1-10189 - Minesweeper
那个P[8][2]是怎么想到的呢?
--陈泓旭
2. re: TOJ 2870 The K-th City 解题
哥们儿,我才识学浅,不是太理解,你这是dijkstra算法吗? 还是动态规划之类的?
--HereIcan
3. re: TOJ 2870 The K-th City 解题
aaa
--HereIcan
4. re: PKU 3367 Expressions 题解
I think we can help each other,make friends with you.(QQ:1015380720)
--ahshua
5. re: TJU 2094 Reserve Bookshelf 题解
在我们学校的oj上面提交返回错误啊
--夜雨
阅读排行榜
1. Toj Lawrence of Arabia 四边形不等式优化(1643)
2. PKU 1160 Post Office(1405)
3. uva :: Programming Challenges :: Chapter 1-10137 - The Trip(1266)
4. TOJ 2553 Japan (1215)
5. PKU 3370 Halloween treats 题解(1192)
评论排行榜
1. PKU 3337 Expression Evaluator(4)
2. Toj Lawrence of Arabia 四边形不等式优化(4)
3. TOJ 2870 The K-th City 解题(2)
4. PKU 3370 Halloween treats 题解(2)
5. TJU 2094 Reserve Bookshelf 题解(2)