C指针编程之道 ---第十次笔记

//指针在搜索算法中实例

//迷宫算法

//搜索最长用到的就是深度优先搜索和广度优先搜索

//深度优先搜索就像名字一样,对每一个道路一直搜索到底,

//为了防止思路,特别的设置了栈这种数据结构

//使得每次找到思路的时候还可以退出到出发点。

//

//

//广度优先搜索

//广度优先搜索就是利用队列性质先进先出的性质,把每次的搜索结果放入队列,

//排除思路等条件

//

//回溯法

//就是枚举每个可能的判断,如果可以就执行,不可以就返回开始的地方

//八皇后的实现:回溯法

#include <iostream>

#include <cstdio>

#include <cmath>

using namespace std;

int a[9] = {100};

//用来存放解的个数

int count = 0;

int Place(int i, int value)

{

int j;

if(i == 1)

return 1;

for(j = 1; j < i; ++j)

{

if(a[j] == value)

return 0;

if(abs(i - j) == abs(value - a[j]))

return 0;

}

return 1;

}

void ShowResult()

{

int i, j;

for(j = 1; j <= 8; ++j)

{

for(i = 1; i < a[j]; ++i)

{

printf("* ");

}

printf("Q");

for(i = i + 1; i <= 8; ++i)

{

printf("* ");

}

printf("\n");

}

}

void Backtrack(int t)

{

int i;

if(t > 8)

{

printf("*********************");

ShowResult();

count++;

return;

}

else

{

for(i = 1; i <= 8; ++i)

{

if(Place(t, i))

{

a[t] = i;

Backtrack(t + 1);

}

}

}

}

void ShowCount()

{

printf("\n八皇后的问题共有%d个解:\n", count);

}

int main()

{

Backtrack(1);

ShowCount();

return 0;

}

时间: 2024-08-22 05:55:12

C指针编程之道 ---第十次笔记的相关文章

C指针编程之道 ---第九次笔记

//这里说的是指针在算法中的应用 //直接选择排序 //每个排序的算法都是指针的方便性的特点来指向每个元素进行交换等 //这里的基本思想是对待排序的记录进行n - 1次选择. //第i次操作选择i大(小)的记录放在第i个(或者n - i - 1 个)位置上. //即每次都将一个记录放在它最终的位置上, //这就是所谓的"各回各家" #include <iostream> #include <cstdio> using namespace std; void Se

C指针编程之道 ---第一次笔记

//==================C指针编程之道===========================// /////////////第一次笔记//////////////// * 表示该变量为指针变量,这也是指针变量的特性. int *pStu; //定义指针变量pStu, 并且pStu指向int类型变量 static int *pStu; //定义指针变量pStu, 并且pStu指向静态整形变量 char *pStu;   //pStu是一个指针变量 取地址符& 和 取值运算符 * 通

C指针编程之道 ---第八次笔记

这次是算法中最常用的快速排序,常备成为快排,这里是头尾两个指针的算法. //快速排序 #include <iostream> #include <cstdio> using namespace std; int partition(int *Array, int i, int j) { int t = *(Array + 1); while(i < j) { while(i < j && *(Array + j) >= t) { j--; } if

C指针编程之道 ---第五次笔记

//数据结构中指针的应用 //内存中的堆栈和数据结构中的堆栈室友区别的 //在数据结构中,常常把堆栈放在一起表示的一种数据结构, //但是在内存中堆是存储动态的内存,栈是存放静态的以及调用的函数. //在数据结构中涉及了堆栈,队列,链表等在这里主要实现的是队列. //循环队列的指针应用 #include <iostream> #include <cstdio> #define QueueSize_UarLen 8 using namespace std; typedef struc

C指针编程之道 ---第四次笔记

//多为数组的指针学习 //定义二位数组 //int date[4][5]; //说明这个数组的所有成员都是int类型 //int date[4][5] = { // {1, 2, 3, 4, 5}, // {1, 2, 3, 4, 5}, // {1, 2, 3, 4, 5}, // {1, 2, 3, 4, 5} //}; //或者int date[4][5] = {1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5}; //例子访问二位数组 #include <

C指针编程之道 ---第三次笔记

这次整理的是函数指针和指针函数 这是指针的调用: 代码: #include <iostream> #include <cstdio> using namespace std; typedef unsigned char unit8_t; extern void swapdata(unit8_t dat_x, unit8_t dat_y); int main() { unit8_t x, y; scanf("%d %d", &x, &y); pri

C指针编程之道 ---第十一次笔记

这次来说交换函数的实现: 1. #include <stdio.h> #include <stdlib.h> void swap(int x, int y) { int temp; temp = x; x = y; y = temp; } int main() { int a = 10, b = 20; printf("交换前:\n a = %d, b = %d\n", a, b); swap(a, b); printf("交换后:\n a = %d,

C指针编程之道 ---第六次笔记

//指向文件类型的指针 //在C语言的文件你的读写一般使用系统的库函数来对数据进行读写 //文件的类型的指针 //文件的结构 #include <iostream> #include <cstdio> using namespace std; typedef struct { short level; //缓冲区满或者是空的程度 unsigned flags; //文件状态标志 char fd; //文件描述符 unsigned hold; //如无缓冲区不读取字符 short b

C指针编程之道 ---第七次笔记

//指针在C语言算法中的应用 //首先说的是排序 //排序基本上分为5种 //插入排序, 选择排序, 交换排序, 归并排序, 分配排序 //先说7上8下的冒泡排序 #include <iostream> #include <cstdio> using namespace std; void BubbleSort(int *Array, int n) { int a; for(int i = n; i > 0; --i) { for(int j = 0; j < i; +