[acm-icpc] backtracking

Post on 12-May-2015

389 Views

Category:

Documents

4 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Backtracking

陳鵬宇 (float)<float.tw@gmail.com>

Rights to CopyAttribution-ShareAlike 3.0

You are free:

– to Share — to copy, distribute and transmit the work

– to Remix — to adapt the work

– to make commercial use of the work

● Under the following conditions:

– Attribution — You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work).

– Share Alike — If you alter, transform, or build upon this work, you may distribute the resulting work only under the same or similar license to this one.

– License text: http://creativecommons.org/licenses/by-sa/3.0/legalcode

Backtracking● 概念

– 枚舉所有可能性

● 排列、組合

Example● 1, 2, 3 的所有排列

Example● 第一格目前可以選 : 1, 2, 3

Example● 第二格目前可以選 : 2, 3

1

1

Example● 第三格目前可以選 : 3

1

1 2

1 2

Example● 找到第一個組合 :1-2-3

1

1 2

1 2 3

Example● 往回走,第三格可以選 : 3

1

1 2

1 2 3

Example● 3 已經選過,再往回,第二格可以選 :2, 3

1

1 2

1 2 3

Example● 3 已經選過,再往回,第二格可以選 :2, 3

1

1 2

1 2 3

Example● 2 已經選過,選 3

1

1 2

1 2 3

1 3

Example● 後略 ...

1

1 2

1 2 3

1 3

1 3 2

Example

1

1 2

1 2 3

1 3

1 3 2

Example● 1, 2, 3 的所有組合

1 2 3

Example

1 2 3

1 2 3

1 2 3

Example

1 2 3

1 2 3

1 2 3 1 2 3

Example

1 2 3

1 2 3

1 2 3 1 2 3

1 2 3

1 2 3 1 2 3

Practice

UVa - 441

8 Queen Problem● 問題:在 8x8 的西洋棋棋盤上擺放八隻皇后,

讓他們恰好無法互相攻擊對方。● 組合

● Uva - 750

Sudoku● 數獨:行、列、方格內不可重復● 排列

● UVa - 989

剪支● 在完成遞迴條件前檢查是否可能為答案

– Ex. 組合遞增數列

102/3/27 23

Thank You for Your Listening.

top related