POJ 1020谈几种重要的思维方式

来源:互联网 发布:哪里有淘宝买家数据 编辑:程序博客网 时间:2024/06/06 03:58

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">题目 [</span><a target=_blank href="http://poj.org/problem?id=1020" style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">http://poj.org/problem?id=1020</a><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">]:</span>

Nahid Khaleh decides to invite the kids of the "Shahr-e Ghashang" to her wedding anniversary. She wants to prepare a square-shaped chocolate cake with known size. She asks each invited person to determine the size of the piece of cake that he/she wants (which should also be square-shaped). She knows that Mr. Kavoosi would not bear any wasting of the cake. She wants to know whether she can make a square cake with that size that serves everybody exactly with the requested size, and without any waste.
Input

The first line of the input file contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by input data for each test case. Each test case consist of a single line containing an integer s, the side of the cake, followed by an integer n (1 ≤ n ≤ 16), the number of cake pieces, followed by n integers (in the range 1..10) specifying the side of each piece.
Output

There should be one output line per test case containing one of the words KHOOOOB! or HUTUTU! depending on whether the cake can be cut into pieces of specified size without any waste or not.

DFS还是BFS
  POJ 1020是一道穷举搜索题,在深度优先搜索还是广度优先搜索上,一开始选择了广度优先搜索,因为在印象中广度优先搜索就是用来快速找到到达某个状态的最短路径,而这道题要求判断一个大的正方形蛋糕能否恰好被给定的小正方形蛋糕完全填满而没有空余,如果能够快速找到一条这样的路径,那不就能说明答案为真么,相反如果穷举了所有状态还没有找到,则答案为假。对比POJ 1753,也是要搜索各种路径,直到找到一种可行的方案。因此我认为BFS是合适的,当然,一开始写算法也是按BFS去写的,直到发现碰到的问题并不是POJ 1753能够完成的:
  1) POJ 1753只考虑4×4的棋盘,因此状态最多只有2^16 = 65535种,任何时刻的状态用一个short型变量足够保存了,但POJ 1020却是面对最大40×40的棋盘,要存储任何时刻的状态,除了选择STL bitset之外别无选择;
  2) 不但任一时刻的状态存储需要占用的空间大,还要考虑到BFS的特点:在同层次的状态搜索完毕之前,下一层次的状态都仍然存在于内存中,最终,在倒数第二层次的最后一个状态获得处理之前,最后一个层次的所有状态都呆在内存中。数目有多少呢?假设每次改变一个棋盘格的状态,那么这个数目将是2^1600,大到不可能在内存中放得下。

  其实不管是BFS还是DFS,最终都能将节点遍历一遍,但实际应用时还是要分辨清楚两种方式的特点,根据实际情况选用。比较如下:
BFS:

  1. 用于明确找到最短路径的情形
  2. 用队列(Queue)作为数据结构
  3. 需要为每一个遍历节点存储当前状态。以棋盘为例,要保存当前棋盘格局(各个棋盘格上有无棋子,黑棋还是白棋)、当前走的步数(用于记录和输出最小步数)、引发状态跃迁的那枚棋子(为了记录和输出最小路径),等等。这些状态信息不能用全局变量代替,因为任一时刻队列头元素和全局变量所存储的状态并非对应的
  4. 空间复杂度为O(2^n),考虑空间需求能否满足
  5. 适合广度大的图(在任一状态下,分支情况众多)

DFS:

  1. 可用递归实现,或栈数据结构实现非递归(本质上都是栈结构)
  2. 可用全局变量记录存储状态:进行下一次递归之前,修改全局状态;递归完成之后,恢复原先状态。对比BFS中棋盘的例子,在DFS中状态变量只需1份即可,大大降低空间需求
  3. 空间复杂度O(n)
  4. 运行时间上,不管是递归调用还是栈数据结构实现非递归,都要维护状态变量,因此运行起来要慢(感觉如果维护成本不大,则相差并不会太多)
  5. 适合深度大的图(任一状态下,分支情况不多,但深度相对大)

降维
  选用适当的数据结构和算法,可以将降低维度,也降低算法的时间复杂度。以POJ 1020为例:
问题:在4×4蛋糕A[1...4, 1...4]中,判断是否能切下正方形蛋糕A[2...4, 2...4]。假设用数组A[4][4]表示蛋糕,A[i][j] = 'o'表示该单元格表示的小蛋糕存在,A[i][j] = 'x'表示已经被切下
x x x o
o o x o
o o x o
o o o o

图 1

解法一:检查每一块小蛋糕A[i][j]的状态

/* Check if subarea A[2...4, 2...4] is available */bool check_subarea(A[2...4, 2...4]){    bool bAvailable = true;    for (i = 2; i <= 4; i++) {        for (j = 2; j <= 4; j++) {            if (A[i][j] == 'x') {                bEmpty = false;                break;            }        }    }        return bAvailable;}

解法二:用一个一维数组row[4]记录每一列的蛋糕开始行。按上图所示,则row[4] = {2, 2, 1, 3}

/* Check if subarea A[2...4, 2...4] is empty */bool check_subarea(A[2...4, 2...4]){    bool bAvailable = true;    for (i = 2; i <= 4; i++) {        if (row[i] > 2) {            bAvailable = false;            break;        }    }        return bAvailable;}

剪枝
  选用适当的数据结构和算法,可以巧妙地进行剪枝,减少搜索。还是以POJ 1020为例:
问题:输入16个整数,各个整数均不大于10,他们代表了小方形蛋糕的边长。给定蛋糕形状,要求在给定位置处切下一块边长最大的蛋糕。
假如蛋糕形状如下图所示。

x x x x x x x x o x
x x x x x x x x o x
x x x o o x o o o x
x x x o o o o o o x
o o o o o o o o o x
o o o o o o o o o x
o o o o o o o o o x
o o o o o o o o o o
o o o o o o o o o o
o o o o o o o o o o

图 2

输入的各个整数为9, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 6, 6, 7, 8
解法一:对各个小方形蛋糕进行排序,然后从大到小试探
以下算法参数:
A[1...10, 1...10]: 蛋糕形状,A[i][j] = 'o' 表示该格蛋糕存在,A[i][j] = 'x'表示已经被切下
row              : 开始行
col              : 开始列
cake_side[16]    : 各个小方形蛋糕的边长。从中选取可以切下的最大边长蛋糕

/* * Cut a piece of cake with maximum side, from given position */int cut_cake(A[1...10, 1...10], row, col, cake_side[n]){    sort(cake_side, side + 16);        for (i = 16; i >= 1; i--) {        side = cake_side[i];        /*         * Check if possible to cut a cake from position A[row][col]         * If so, cut it        */    }}

解法二:已知边长不会大于10,可以用一个数组n_side[10]保存各个边长的个数,n_side[i]记录边长i的个数。在本例中,
n_side = {
    2,  /* number of cake sides with length 1 */
    2,  /* number of cake sides with length 2 */
    2,  /* number of cake sides with length 3 */
    2,  /* number of cake sides with length 4 */
    2,  /* number of cake sides with length 5 */
    3,  /* number of cake sides with length 6 */
    1,  /* number of cake sides with length 7 */
    1,  /* number of cake sides with length 8 */
    1,  /* number of cake sides with length 9 */
    0,  /* number of cake sides with length 10 */
};

/* * Cut a piece of cake with maximum side, from given position */int cut_cake(A[1...10, 1...10], row, col, cake_side[n]){    for (i = 10; i >= 1; i--) {        if (n_side[i] > 0) {            /*             * Check if possible to cut a cake from position A[row][col]             * If so, cut it            */        }    }}

在本例中,解法二明显消除了对相等长度边长的重复检查。



0 0