1.分治法
分治法的基本思想就是将一个规模为n的问题分解成k个规模较小的子问题,而、这些子问题相互独立且与原问题相同;子问题的解合并后可以得到原问题的解。它的一般的算法设计模式如下:
divide-and-conquer(P)
{
if (|P| <= n0) adhoc(P)
divide P into smaller subinstances P1, p2, ..., Pk;
for (i = 1; i <= k;i++)
yi = divide-and-conquer(Pi);
return merge(y1, y2, ..,yk);
}
|P|表示问题的规模,n0表示阈值,表示当问题P的规模不超过n0时,问题已经容易解决。
adhoc(P)则是直接求解小规模P。
merge则用于合并子问题的解,来得到原问题的解。
分治法基本步骤为:
- 分解:将原问题分解为子问题
- 解决:求解子问题
- 合并:将子问题的解合并为原问题的解
在分治法中,以二分搜索算是简单。
2.二分搜索
二分搜索只适用于能够随机读取的结构,比如数组。另外,二分搜索需要保证数据本身是排好序的。
#include <iostream>
using namespace std;
template<typename Type>
int binarySearch(Type a[], const Type& x, int n) {
int left = 0;
int right = n - 1;
while (left <= right) {
int middle = (left + right) / 2;
if (x == a[middle])
return middle;
//往右边搜索
if (x > a[middle])
left = middle + 1;
else
right = middle - 1;
}
//未找到则返回-1
return -1;
}
int main() {
int a[] = { 1, 2, 3, 5, 9, 10 };
int length = sizeof(a) / sizeof(a[0]);
cout << binarySearch(a, 5, length)<<endl;
return 0;
}
在二分搜索中,每次比较都会把问题的规模缩小为原来的½,所以二分搜索的时间复杂度为O(logn)。
在main函数中,通过
sizeof(a)/sizeof(a[0])
可以获取到数组的长度,这里必须要保证这个a是Type a[]类型的,指针则不行。
以本例为例,第一次搜索left和right指针如下:
经判断得到a[mid] = 3 < 5,然后让left = mid + 1,再次比较如下:
此时比较a[mid]=9 > 5,之后会有right = mid - 1;接下来再次判断则可以得到5所在的索引,搜索结束。
3.棋盘覆盖
3.1 问题描述
在一个*
个方格的棋盘中, 若恰有一个方格与其他方格不同,则成该方格为一个特殊的方格,且该棋盘为一特殊棋盘。显然,特殊的方格出现在棋盘上的位置为
种情形。
如上图,就是在k=2时的一个例子。
使用如下图的4种L型骨牌进行填充,且骨牌相互之间不可覆盖。
可以知道,在任何一个 *
个方格的棋盘中,用到的L型骨牌的个数恰为(
-1)/3。
以上图为例,使用分治策略的步骤大致如下:
注:3~6步骤不分先后步骤。
- 如果当前棋盘的尺寸为1,则直接退出
- 把棋盘当前的尺寸除以二来得到四个小棋盘
- 判断左上角的小棋盘是否存在特殊方格,如果存在,则重复步骤2;否则在该子棋盘的右下角构建一个特殊方块,然后重复步骤2
- 判断右上角的小棋盘是否存在特殊方格,如果存在,则重复步骤2;否则在该子棋盘的左下角构建一个特殊方块,然后重复步骤2
- 判断左下角的小棋盘是否存在特殊方格,如果存在,则重复步骤2;否则在该子棋盘的右上角构建一个特殊方块,然后重复步骤2
- 判断右下角的小棋盘是否存在特殊方格,如果存在,则重复步骤2;否则在该子棋盘的左上角构建一个特殊方块,然后重复步骤2
为了让分治策略可以解决棋盘覆盖的问题,当该子棋盘不存在特殊方块的时候,程序会先构造一个特殊方块,这样就使得原问题和子问题的解法相同;除此之外,因为只能使用到L型的骨牌,所以新增的方块都以中间为中心,如上图所示。
代码示例:
#include <iostream>
using namespace std;
#define CHESS_SIZE 4
//全局变量
//棋盘
int board[CHESS_SIZE][CHESS_SIZE];
//L型牌的编号
int tile = 0;
/**
* @param topRow: 左上角方格的行号
* @param topCol: 左上角方格的列号
* @param specialRow: 特殊方格方格的行号
* @param specialCol: 特殊方格方格的列号
* @param size: pow(2,k)
*/
void chessBoard(int topRow, int topCol, int specialRow, int specialCol, int size) {
//划分到1*1时,则不再进行处理
if (size == 1)
return;
//L型骨牌号
int t = tile++;
//分割棋盘
int newSize = size / 2;
//覆盖左上角子棋盘
if (specialRow < topRow + newSize && specialCol < topCol + newSize)
//特殊方格在此棋盘中
chessBoard(topRow, topCol, specialRow, specialCol, newSize);
else {
//用t号L型骨牌覆盖左下角
board[topRow + newSize - 1][topCol + newSize - 1] = t;
chessBoard(topRow, topCol, topRow + newSize - 1, topCol + newSize - 1, newSize);
}
//覆盖右上角子棋盘
if (specialRow < topRow + newSize && specialCol >= topCol + newSize)
chessBoard(topRow, topCol + newSize, specialRow, specialCol, newSize);
else {
board[topRow + newSize - 1][topCol + newSize] = t;
chessBoard(topRow, topCol + newSize, topRow + newSize - 1, topCol + newSize, newSize);
}
//覆盖左下角
if (specialRow >= topRow + newSize && specialCol < topCol + newSize)
chessBoard(topRow + newSize, topCol, specialRow, specialCol, newSize);
else {
board[topRow + newSize][topCol + newSize - 1] = t;
chessBoard(topRow + newSize, topCol, topRow + newSize, topCol + newSize - 1, newSize);
}
//覆盖右下角
if (specialRow >= topRow + newSize && specialCol >= topCol + newSize)
chessBoard(topRow + newSize, topCol + newSize, specialRow, specialCol, newSize);
else {
board[topRow + newSize][topCol + newSize] = t;
chessBoard(topRow + newSize, topCol + newSize, topRow + newSize, topCol + newSize, newSize);
}
}
int main() {
//特殊方格
board[0][1] = -1;
//测试
chessBoard(0, 0, 0, 1, CHESS_SIZE);
for (int i = 0; i < CHESS_SIZE; i++) {
for (int j = 0; j < CHESS_SIZE; j++)
cout << board[i][j]<<" ";
cout << endl;
}
return 0;
}
输出如下:
参考
- 《计算机算法设计与分析》王晓东