万能的搜索

71人阅读 评论(0)

搜索算法

深度优先搜索

//
//  main.c
//  Algorithm_04_01
//
//  Created by vito7zhang on 16/1/25.
//

#include <stdio.h>
int a[10],book[10];
int n;

void dfs(int step){
if (step == n+1) {
for (int i = 1; i <= n; i++) {
printf("%d",a[i]);
}
printf("\n");
return;
}

for (int i = 1; i <= n; i++) {
if (book[i] == 0) {
a[step] = i;
book[i] = 1;
dfs(step+1);
book[i] = 0;
}
}
}

int main(int argc, const char * argv[]) {
printf("请输入全排列的最大数字\n");
scanf("%d",&n);
dfs(1);

return 0;
}

//
//  main.c
//  Algorithm_04_01_02
//
//  Created by vito7zhang on 16/1/25.
//

#include <stdio.h>
int a[10],book[10],total=0;
int n;

void dfs(int step){
if (step == 10) {
if (a[1]*100+a[2]*10+a[3]+a[4]*100+a[5]*10+a[6]==a[7]*100+a[8]*10+a[9]) {
total++;
printf("%d%d%d+%d%d%d=%d%d%d\n",a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
}
return;
}
for (int i = 1; i <= 9; i++) {
if (book[i] == 0) {
a[step] = i;
book[i] = 1;
dfs(step+1);
book[i] = 0;
}
}
}

int main(int argc, const char * argv[]) {
dfs(1);
printf("totol = %d\n",total/2);
return 0;
}


使用深度优先搜索解决最短路径问题

0 0 1 0
0 0 0 0
0 0 1 0
0 1 0 0
0 0 0 1

0表示可通路径，1表示障碍物

//
//  main.c
//  Algorithm_04_02
//
//  Created by vito7zhang on 16/1/25.
//

#include <stdio.h>
int n,m,p,q,min=999999;
int a[51][51],book[51][51];

void dfs(int x1,int y1,int step){
int next[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};
int tx,ty;

if (x1 == p && y1 == q) {
if (min > step) {
min = step;
}
return;
}

for (int k = 0; k <= 3; k++) {
tx = x1+next[k][0];
ty = y1+next[k][1];
if (tx<1||tx>n||ty<1||ty>n) {
continue;
}
if (a[tx][ty] == 0 && book[tx][ty] == 0) {
book[tx][ty] = 1;
dfs(tx, ty, step+1);
book[tx][ty] = 0;
}
}

return;
}

int main(int argc, const char * argv[]) {
int startX,startY;
printf("请输入行列数\n");
scanf("%d%d",&n,&m);
printf("请输入迷宫\n");
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
scanf("%d",&a[i][j]);
}
}
printf("请输入起始坐标和终止坐标\n");
scanf("%d%d%d%d",&startX,&startY,&p,&q);

book[startX][startY] = 1;
dfs(startX, startY, 0);

printf("%d\n",min);
return 0;
}


广度优先搜索

//
//  main.c
//  Algorithm_04_03
//
//  Created by vito7zhang on 16/1/27.
//

#include <stdio.h>
struct note{
int x;
int y;
int f;
int s;
};

int main(int argc, const char * argv[]) {

struct note que[2501];
int a[51][51]={0},book[51][51]={0};

int next[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};

int i,j,k,n,m,startx,starty,p,q,tx,ty,flag;

scanf("%d%d",&n,&m);

for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
scanf("%d",&a[i][j]);
}
}
scanf("%d%d%d%d",&startx,&starty,&p,&q);

//队列初始化
tail = 1;

//往队列中插入迷宫入口
que[tail].x = startx;
que[tail].y = starty;
que[tail].f = 0;
que[tail].s = 0;
tail++;
book[startx][starty]=1;

flag = 0;//标记是否到达终点
for (k = 0; k < 4; k++) {

if (tx<1||tx>n||ty<1||ty>m) {
continue;
}
if (a[tx][ty]==0&&book[tx][ty]==0) {
book[tx][ty] = 1;
que[tail].x = tx;
que[tail].y = ty;
tail++;
}
if (tx==p&&ty==q) {
flag = 1;
break;
}
}
if (flag == 1) {
break;
}
}
printf("%d\n",que[tail-1].s);
return 0;
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：8230次
• 积分：462
• 等级：
• 排名：千里之外
• 原创：29篇
• 转载：10篇
• 译文：2篇
• 评论：0条