编程总结
每每刷完一道题后,其思想和精妙之处没有地方记录,本篇博客用以记录刷题过程中的遇到的算法和技巧
BFS 是广度优先遍历,常借助队列来实现
队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(head)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。
BFS 具体说明参考《编程之美》专栏里
剑指 Offer 32 - I. 从上到下打印二叉树
思路:
#define MAXLINE 2000
int *levelOrder(struct TreeNode *root, int *returnSize) {
returnSize[0] = 0;
// 特殊情况
if (root == NULL) return NULL;
int front = 0; // 相当于数组左边为尾,右边为头
int tail = 0;
struct TreeNode *queue[MAXLINE];
int *res = (int*)malloc(sizeof(int) * MAXLINE);
queue[tail++] = root; // 根节点入队列,队尾插入
while(front < tail) {
struct TreeNode* tmp = (struct TreeNode*)malloc(sizeof(struct TreeNode) * MAXLINE);
tmp = queue[front++]; // 出队列,队头删除
res[(*returnSize)++] = tmp->val;
if(tmp->left != NULL){
queue[tail++] = tmp->left;
}
if(tmp->right != NULL){
queue[tail++] = tmp->right;
}
}
return res;
}
Open Lock
解开一个密码锁。这个密码包括四位,每一位都是1 ~ 9的数字。每次操作,你可以对任一位加上或者减去1,一个数字位可能会变成1,而当它从1再被减“1”时,它会变成“9”;此外,你也可以把相邻位的数字调换。每一个操作是一步。
你的目标是,使用最少的步数解开密码。
注意:最左边一位数字不能算是最右边一位数字的相邻位。
输入样例 1 复制
1234
2144
输出样例 1
2
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#define MAX 10001
typedef struct {
char *passWord;
int patlLoss;
} Node;
Node *gQueue[MAX];
int gFlag[MAX] = {0};
int head = 0;
int tail = 0;
// 交换函数,i为位置
void Swap(char *lock, int pos)
{
char buffer;
buffer = lock[pos];
lock[pos] = lock[pos + 1];
lock[pos + 1] = buffer;
}
// 非交换式的密码BFS搜索
void NoExchangeSearch(int i, Node *node, int dirFlag)
{
char buffer = node->passWord[i];
char changeBuffer;
if (dirFlag) {
changeBuffer = buffer == '1' ? '9' : buffer - 1;
} else {
changeBuffer = buffer == '9' ? '1' : buffer + 1;
}
char *p = (char *)malloc(5 * sizeof(char));
strcpy(p, node->passWord);
p[i] = changeBuffer;
int num = atoi(p);
if (gFlag[num]) {
free(p);
return;
}
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->passWord = p;
newNode->patlLoss = node->patlLoss + 1;
gFlag[num] = 1;
tail++;
gQueue[tail] = newNode;
}
// 交换式的密码BFS搜索
void ExchangeSearch(int pos, Node *node)
{
char *p = (char *)malloc(5 * sizeof(char));
strcpy(p, node->passWord);
Swap(p, pos); // 交换位置
int num = atoi(p); // ATOI Str2Int
if (gFlag[num]) {
free(p);
return;
}
Node *newNodeEx = (Node *)malloc(sizeof(Node));
newNodeEx->passWord = p;
newNodeEx->patlLoss = node->patlLoss + 1;
gFlag[num] = 1;
tail++;
gQueue[tail] = newNodeEx;
}
int main()
{
char initPassword[5];
char rightPassword[5];
scanf("%s", initPassword);
scanf("%s", rightPassword);
// 特殊场景补充判断
if (strcmp(initPassword, rightPassword) == 0) {
printf("0");
return 0;
}
Node *initNode = (Node *)malloc(sizeof(Node));
initNode->passWord = initPassword;
initNode->patlLoss = 0;
gQueue[tail] = initNode;
int tmpHead = 0;
int endFlag = 0;
while (head <= tail) {
Node *node = gQueue[head++]; // 出队
Node *tmpNode = (Node *)malloc(sizeof(Node));
while ((tmpNode != NULL) && (tmpHead <= tail)) {
tmpNode = gQueue[tmpHead];
if (strcmp(rightPassword, tmpNode->passWord) == 0) {
printf("%d", tmpNode->patlLoss);
endFlag = 1;
break;
}
tmpHead++;
}
if (endFlag == 1) {
break;
}
for (int i = 0; i < 4; i++) {
NoExchangeSearch(i, node, 1); // 进行递减方向
NoExchangeSearch(i, node, 0); // 进行递增方向
}
for (int i = 0; i < 3; i++) { // 进行位置交换
ExchangeSearch(i, node);
}
}
return 0;
}
Dota
输入样例 1 复制
4
0000
0000
0000
0000
输出样例 1
3
//主要考点:宽度优先搜索 注意:走过的点可以重复走,如果重新经过该点需要的步骤更少
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 100001
#define MAX_STEP 999999
#define ROW 51
#define COL 51
typedef struct Node {
int x;
int y;
int step;
} Node;
int main()
{
int N;
scanf("%d", &N);
char map[ROW][COL];
int visited[ROW][COL] = {0};
int minstep = MAX_STEP;
int step;
const int stepX[8] = {0, 0, 2, -2, 1, -1, -2, 2};
const int stepY[8] = {1, -1, 2, -2, 0, 0, 2, -2}; // 可以移动8个方向
Node queue[MAX_SIZE];
int head = 0, tail = 0;
int nextX, nextY;
for (int i = 0; i < N; i++) {
scanf("%s", &map[i]);
}
memset(visited, 0, sizeof(int)*ROW*COL);
queue[tail].x = 0;
queue[tail].y = 0;
queue[tail++].step = 0; // 首元素入队
visited[0][0] = 1;
while (tail != head) { // 当队列不为空循环
for (int k = 0; k < 8; k++) {
nextX = queue[head].x + stepX[k];
nextY = queue[head].y + stepY[k];
// 越界处理
if (nextX < 0 || nextX >= N || nextY < 0 || nextY >= N) {
continue;
}
// 障碍处理
if (map[nextX][nextY] == '1') { // ‘1’表示障碍,遇到障碍返回
continue;
}
step = queue[head].step + 1; // 计算消耗的步数
if (map[nextX][nextY] == '2') { // ‘2’表示陷阱,减速step++
step++;
}
// 入队条件:对于已经来过的地方,时间更短的入队;如果没有来过,直接入队
if (step < visited[nextX][nextY] || visited[nextX][nextY] == 0) {
queue[tail].x = nextX;
queue[tail].y = nextY;
queue[tail++].step = step;
visited[nextX][nextY] = step;
}
// 如果已经到达终点 仍需继续搜索 记录此时需要的步骤
if ((nextX == N - 1) && (nextY == N - 1)) {
minstep = (minstep > step ? step : minstep);
break;
}
}
head++; // 出队,继续向8个方向遍历.
}
if (MAX_STEP == minstep) {
printf("Impossible");
} else {
printf("%d",minstep);
}
return 0;
}