青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

我叫張小黑
張小黑的掙扎生活
posts - 66,  comments - 109,  trackbacks - 0
Search Techniques

Sample Problem: n Queens [Traditional]

Place n queens on an n x n chess board so that no queen is attacked by another queen.

Depth First Search (DFS)

The most obvious solution to code is to add queens recursively to the board one by one, trying all possible queen placements. It is easy to exploit the fact that there must be exactly one queen in each column: at each step in the recursion, just choose where in the current column to put the queen.

 1 search(col)
 2     if filled all columns
 3         print solution and exit 

 4   for each row
 5       if board(row, col) is not attacked
 6            place queen at (row, col)
 7            search(col+1)
 8            remove queen at (row, col)

Calling search(0) begins the search. This runs quickly, since there are relatively few choices at each step: once a few queens are on the board, the number of non-attacked squares goes down dramatically.

This is an example of depth first search, because the algorithm iterates down to the bottom of the search tree as quickly as possible: once k queens are placed on the board, boards with even more queens are examined before examining other possible boards with only k queens. This is okay but sometimes it is desirable to find the simplest solutions before trying more complex ones.

Depth first search checks each node in a search tree for some property. The search tree might look like this:

The algorithm searches the tree by going down as far as possible and then backtracking when necessary, making a sort of outline of the tree as the nodes are visited. Pictorially, the tree is traversed in the following manner:

Complexity

Suppose there are d decisions that must be made. (In this case d=n, the number of columns we must fill.) Suppose further that there are C choices for each decision. (In this case c=n also, since any of the rows could potentially be chosen.) Then the entire search will take time proportional to cd, i.e., an exponential amount of time. This scheme requires little space, though: since it only keeps track of as many decisions as there are to make, it requires only O(d) space.

Sample Problem: Knight Cover [Traditional]

Place as few knights as possible on an n x n chess board so that every square is attacked. A knight is not considered to attack the square on which it sits.

Breadth First Search (BFS)

In this case, it is desirable to try all the solutions with only k knights before moving on to those with k+1 knights. This is called breadth first search. The usual way to implement breadth first search is to use a queue of states:

 1 process(state)
 2     for each possible next state from this one
 3         enqueue next state 

 4 search()
 5     enqueue initial state
 6     while !empty(queue)
 7         state = get state from queue
 8         process(state)

This is called breadth first search because it searches an entire row (the breadth) of the search tree before moving on to the next row. For the search tree used previously, breadth first search visits the nodes in this order:

It first visits the top node, then all the nodes at level 1, then all at level 2, and so on.

Complexity

Whereas depth first search required space proportional to the number of decisions (there were n columns to fill in the n queens problem, so it took O(n) space), breadth first search requires space exponential in the number of choices.

If there are c choices at each decision and k decisions have been made, then there are c k possible boards that will be in the queue for the next round. This difference is quite significant given the space restrictions of some programming environments.

[Some details on why ck: Consider the nodes in the recursion tree. The zeroeth level has 1 nodes. The first level has c nodes. The second level has c2 nodes, etc. Thus, the total number of nodes on the k-th level is ck.]

Depth First with Iterative Deepening (ID)

An alternative to breadth first search is iterative deepening. Instead of a single breadth first search, run D depth first searches in succession, each search allowed to go one row deeper than the previous one. That is, the first search is allowed only to explore to row 1, the second to row 2, and so on. This ``simulates'' a breadth first search at a cost in time but a savings in space.

 1 truncated_dfsearch(hnextpos, depth)
 2     if board is covered
 3         print solution and exit 

 4     if depth == 0
 5         return 

 6     for i from nextpos to n*n
 7         put knight at i
 8         truncated_dfsearch(i+1, depth-1)
 9         remove knight at i 

10 dfid_search
11     for depth = 0 to max_depth
12        truncated_dfsearch(0, depth)

Complexity

The space complexity of iterative deepening is just the space complexity of depth first search: O(n). The time complexity, on the other hand, is more complex. Each truncated depth first search stopped at depth k takes ck time. Then if d is the maximum number of decisions, depth first iterative deepening takes c0 + c1 + c2 + ... + cd time.

If c = 2, then this sum is cd+1 - 1, about twice the time that breadth first search would have taken. When c is more than two (i.e., when there are many choices for each decision), the sum is even less: iterative deepening cannot take more than twice the time that breadth first search would have taken, assuming there are always at least two choices for each decision.

Which to Use?

Once you've identified a problem as a search problem, it's important to choose the right type of search. Here are some things to think about.

In a Nutshell
Search Time

Space When to use
DFS O(c k)

O(k) Must search tree anyway, know the level the answers are on, or you aren't looking for the shallowest number.
BFS O(c d )

O(c d ) Know answers are very near top of tree, or want shallowest answer.
DFS+ID O(c d)

O(d) Want to do BFS, don't have enough space, and can spare the time.
d is the depth of the answer
k is the depth searched
d <= k

Remember the ordering properties of each search. If the program needs to produce a list sorted shortest solution first (in terms of distance from the root node), use breadth first search or iterative deepening. For other orders, depth first search is the right strategy.

If there isn't enough time to search the entire tree, use the algorithm that is more likely to find the answer. If the answer is expected to be in one of the rows of nodes closest to the root, use breadth first search or iterative deepening. Conversely, if the answer is expected to be in the leaves, use the simpler depth first search.

Be sure to keep space constraints in mind. If memory is insufficient to maintain the queue for breadth first search but time is available, use iterative deepening.

Sample Problems

Superprime Rib [USACO 1994 Final Round, adapted]

A number is called superprime if it is prime and every number obtained by chopping some number of digits from the right side of the decimal expansion is prime. For example, 233 is a superprime, because 233, 23, and 2 are all prime. Print a list of all the superprime numbers of length n, for n <= 9. The number 1 is not a prime.

For this problem, use depth first search, since all the answers are going to be at the nth level (the bottom level) of the search.

Betsy's Tour [USACO 1995 Qualifying Round]

A square township has been partitioned into n 2 square plots. The Farm is located in the upper left plot and the Market is located in the lower left plot. Betsy takes a tour of the township going from Farm to Market by walking through every plot exactly once. Write a program that will count how many unique tours Betsy can take in going from Farm to Market for any value of n <= 6.

Since the number of solutions is required, the entire tree must be searched, even if one solution is found quickly. So it doesn't matter from a time perspective whether DFS or BFS is used. Since DFS takes less space, it is the search of choice for this problem.

Udder Travel [USACO 1995 Final Round; Piele]

The Udder Travel cow transport company is based at farm A and owns one cow truck which it uses to pick up and deliver cows between seven farms A, B, C, D, E, F, and G. The (commutative) distances between farms are given by an array. Every morning, Udder Travel has to decide, given a set of cow moving orders, the order in which to pick up and deliver cows to minimize the total distance traveled. Here are the rules:

  • The truck always starts from the headquarters at farm A and must return there when the day's deliveries are done.
  • The truck can only carry one cow at a time.
  • The orders are given as pairs of letters denoting where a cow is to be picked up followed by where the cow is to be delivered.

Your job is to write a program that, given any set of orders, determines the shortest route that takes care of all the deliveries, while starting and ending at farm A.

Since all possibilities must be tried in order to ensure the best one is found, the entire tree must be searched, which takes the same amount of time whether using DFS or BFS. Since DFS uses much less space and is conceptually easier to implement, use that.

Desert Crossing [1992 IOI, adapted]

A group of desert nomads is working together to try to get one of their group across the desert. Each nomad can carry a certain number of quarts of water, and each nomad drinks a certain amount of water per day, but the nomads can carry differing amounts of water, and require different amounts of water. Given the carrying capacity and drinking requirements of each nomad, find the minimum number of nomads required to get at least one nomad across the desert.

All the nomads must survive, so every nomad that starts out must either turn back at some point, carrying enough water to get back to the start or must reach the other side of the desert. However, if a nomad has surplus water when it is time to turn back, the water can be given to their friends, if their friends can carry it.

Analysis: This problem actually is two recursive problems: one recursing on the set of nomads to use, the other on when the nomads turn back. Depth-first search with iterative deepening works well here to determine the nomads required, trying first if any one can make it across by themselves, then seeing if two work together to get across, etc.

Addition Chains

An addition chain is a sequence of integers such that the first number is 1, and every subsequent number is the sum of some two (not necessarily unique) numbers that appear in the list before it. For example, 1 2 3 5 is such a chain, as 2 is 1+1, 3 is 2+1, and 5 is 2+3. Find the minimum length chain that ends with a given number.

Analysis: Depth-first search with iterative deepening works well here, as DFS has a tendency to first try 1 2 3 4 5 ... n, which is really bad and the queue grows too large very quickly for BFS.

posted on 2009-01-21 19:17 zoyi 閱讀(429) 評論(0)  編輯 收藏 引用

只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


歡迎光臨 我的白菜菜園

<2008年4月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

常用鏈接

留言簿(8)

隨筆分類

隨筆檔案

文章檔案

相冊

acmer

online judge

隊友

技術

朋友

搜索

  •  

最新評論

閱讀排行榜

評論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            在线播放国产一区中文字幕剧情欧美 | 香蕉尹人综合在线观看| 久久国产精品第一页| 欧美日韩精品不卡| 亚洲高清不卡在线| 另类酷文…触手系列精品集v1小说| 亚洲国产美女精品久久久久∴| 亚洲精品亚洲人成人网| 蜜臀久久久99精品久久久久久| 国内精品美女av在线播放| 欧美专区第一页| 亚洲一区国产视频| 国产精品免费视频观看| 亚洲主播在线播放| av不卡在线观看| 欧美午夜精品理论片a级大开眼界| 亚洲精品日产精品乱码不卡| 欧美国产精品久久| 老司机精品久久| 在线不卡免费欧美| 免费一区二区三区| 欧美电影电视剧在线观看| 亚洲精品国产精品乱码不99 | 日韩亚洲欧美高清| 欧美高清视频| 欧美国产精品人人做人人爱| 亚洲精品久久久久久久久久久久久| 欧美成人网在线| 免费观看成人鲁鲁鲁鲁鲁视频| 亚洲欧洲偷拍精品| 久久精品99无色码中文字幕| 一区二区冒白浆视频| 国产精品亚洲综合一区在线观看| 欧美一级理论片| 欧美一区二粉嫩精品国产一线天| 国内自拍视频一区二区三区| 久久久久久**毛片大全| 可以免费看不卡的av网站| 日韩视频永久免费| 一区二区三区日韩精品| 国产精品视频一区二区三区| 久久久久免费| 欧美成人免费大片| 亚洲性视频h| 久久精品一本| 一区二区三区日韩| 欧美影视一区| 99精品欧美一区| 亚洲综合成人在线| 亚洲国产一区二区a毛片| 亚洲美女91| 极品av少妇一区二区| 亚洲精品美女91| 国产欧美精品va在线观看| 嫩模写真一区二区三区三州| 欧美精品偷拍| 久久亚洲国产精品日日av夜夜| 欧美多人爱爱视频网站| 欧美影院午夜播放| 欧美喷水视频| 欧美va亚洲va香蕉在线| 欧美日韩在线播放三区| 久久中文字幕一区二区三区| 欧美精品精品一区| 久久久www成人免费无遮挡大片 | 亚洲国产精品久久久久婷婷老年| 一本久久a久久精品亚洲| 含羞草久久爱69一区| 亚洲欧洲日本专区| 精品999久久久| 亚洲国产精品激情在线观看| 欧美区在线播放| 久久gogo国模啪啪人体图| 欧美日韩成人| 午夜精品久久久久久久男人的天堂 | 亚洲男人av电影| 美国十次了思思久久精品导航| 午夜精品福利在线| 欧美日韩精品三区| 欧美激情精品| 亚洲视频在线观看免费| 亚洲精品视频在线观看免费| 亚欧成人精品| 亚洲欧美激情一区二区| 欧美日韩mp4| 最新日韩欧美| 亚洲精品人人| 欧美激情中文字幕一区二区| 欧美激情麻豆| 亚洲国产电影| 久久久国产精品一区| 久久久999| 国产欧美日韩一区二区三区在线观看| 欧美日韩国产精品一区二区亚洲| 欧美一区久久| 夜夜嗨网站十八久久| 久久久久久久久久久久久久一区| 久久久欧美精品| 麻豆久久精品| 99这里有精品| 欧美日韩三级视频| 欧美在线一二三区| 亚洲欧美日韩国产一区二区三区 | 欧美日本国产精品| 欧美成人精品1314www| 欧美日本韩国| 日韩一区二区精品葵司在线| 欧美久久久久久久久| 久久精品成人| 一区二区高清在线观看| 欧美一区二区三区免费看| 国产精品毛片a∨一区二区三区| 久久电影一区| 欧美一区二区黄| 国产女主播一区| 欧美中文字幕久久| 亚洲伊人一本大道中文字幕| 亚洲一区日韩| 亚洲欧洲一区二区三区在线观看 | 一区二区日韩精品| 亚洲欧洲一区二区在线观看| 日韩午夜在线视频| 亚洲欧美国产va在线影院| 国产日韩欧美视频| 久久免费视频这里只有精品| 亚洲国产成人久久| 亚洲综合电影| 在线播放国产一区中文字幕剧情欧美| 老巨人导航500精品| 亚洲精选视频免费看| 欧美一区2区视频在线观看 | 亚洲一区二区伦理| 国产精品综合| 久热re这里精品视频在线6| 日韩亚洲视频在线| 久久婷婷国产综合国色天香| 亚洲精品视频在线观看网站| 国产精品久久久久av免费| 欧美中文字幕第一页| 亚洲精品日韩久久| 欧美在线播放| 99精品视频免费观看| 国产婷婷色一区二区三区四区| 欧美va日韩va| 欧美中文字幕不卡| 亚洲婷婷综合久久一本伊一区| 免费视频亚洲| 久久久久久久久久久成人| 亚洲一级黄色| 亚洲精品国精品久久99热一| 国产欧美视频在线观看| 欧美日韩精品一区二区在线播放| 久久成人在线| 亚洲一区二区成人| 亚洲人成网站777色婷婷| 另类欧美日韩国产在线| 欧美一级视频免费在线观看| 日韩亚洲欧美中文三级| 激情婷婷亚洲| 国产日韩欧美91| 欧美精品一区二区三区很污很色的| 欧美一级视频免费在线观看| 一本色道久久综合一区| 亚洲欧洲精品天堂一级| 久久资源在线| 玖玖视频精品| 亚洲免费中文字幕| a4yy欧美一区二区三区| 亚洲大片av| 国产一区二区三区免费观看| 国产精品久久久久9999| 欧美日韩中文字幕在线| 欧美精品亚洲二区| 免费91麻豆精品国产自产在线观看 | 久久夜色精品国产欧美乱| 亚洲欧美影音先锋| 亚洲在线视频一区| 中日韩高清电影网| 亚洲特级片在线| 亚洲一区二区在线看| 亚洲一区二区在线免费观看视频| 亚洲色图自拍| 亚洲一区二区三区四区五区黄| 亚洲视频1区| 亚洲伊人观看| 亚洲欧美日韩国产综合在线 | 久久国产黑丝| 国产伦理一区| 国产一区二区无遮挡| 国内精品免费午夜毛片| 国模精品娜娜一二三区| 韩国免费一区| 曰韩精品一区二区| 亚洲理伦在线| 亚洲一区二区三区中文字幕在线| 亚洲私人影吧| 欧美一级电影久久| 欧美一区二区精美| 嫩草国产精品入口| 亚洲精品视频啊美女在线直播|