Complete search
The Idea
Solving a problem using complete search is based on the ``Keep It Simple, Stupid’’ principle. The goal of solving contest problems is to write programs that work in the time allowed, whether or not there is a faster algorithm.
Complete search exploits the brute force, straight-forward, try-them-all method of finding the answer. This method should almost always be the first algorithm/solution you consider. If this works within time and space constraints, then do it: it’s easy to code and usually easy to debug. This means you’ll have more time to work on all the hard problems, where brute force doesn’t work quickly enough.
In the case of a problem with only fewer than a couple million possibilities, iterate through each one of them, and see if the answer works.
Problem: Party Lamps [IOI 98]
You are given N lamps and four switches. The first switch toggles all lamps, the second the even lamps, the third the odd lamps, and last switch toggles lamps 1, 4, 7, 10, … .
Given the number of lamps, N, the number of button presses made (up to 10,000), and the state of some of the lamps (e.g., lamp 7 is off), output all the possible states the lamps could be in.
Naively, for each button press, you have to try 4 possibilities, for a total of 410000 (about 106020 ), which means there’s no way you could do complete search (this particular algorithm would exploit recursion).
Noticing that the order of the button presses does not matter gets this number down to about 100004 (about 1016 ), still too big to completely search (but certainly closer by a factor of over 106000 ).
However, pressing a button twice is the same as pressing the button no times, so all you really have to check is pressing each button either 0 or 1 times. That’s only 24 = 16 possibilities, surely a number of iterations solvable within the time limit.
本文探讨了使用完全搜索算法解决编程竞赛问题的策略,强调其简单直接的特点。通过实例分析,如解决Party Lamps问题,展示了如何通过减少可能性空间来优化搜索效率。
330

被折叠的 条评论
为什么被折叠?



