暑假集训日记--7.31--搜索

本文通过具体实例介绍了深度优先搜索(DFS)和广度优先搜索(BFS)的基本原理及应用技巧,包括如何确定搜索条件、使用递归实现DFS、利用队列进行BFS等,并提供了马走日字典序路径输出等代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

看了半天的搜索的课件以及资料,做了半天的搜索的练习题。逐步复习了搜索的有关知识。

又重新开始学搜索,这一天也只写了几个代码。有hdoj上的,有poj上的,还有就是曾经老师给的资料。都是比较基础的,但是也通过这些练习逐步理解搜索的知识。

1、DFS(深度优先搜索)

需要找准搜索的条件,一步一步的往下搜索,因为有回溯的过程,所以很多地方需要用到递归的方法。

有的时候DFS需要用到栈的先进后出的性质。

通过递归不断重复得到目标解。

2、BFS(广度优先搜索)

BFS常用到队列的先进先出的规则。因为广度优先搜索是每一层每一层的搜索。今天做的练习中,BFS的题也都用的队列进行求解。

每次取出队列的首元素,进行拓展。

将拓展得到的可行状态放到队列内。

将初始状态删除。

重复直到队列为空或者得到目标解。

同时在今天的练习中,我也体会到了在DFS搜索的过程中,记录走的路径。例如下面这个题:

Background 
The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey 
around the world. Whenever a knight moves, it is two squares in one direction and one square perpendicular to this. The world of a knight is the chessboard he is living on. Our knight lives on a chessboard that has a smaller area than a regular 8 * 8 board, but it is still rectangular. Can you help this adventurous knight to make travel plans? 

Problem 
Find a path such that the knight visits every square once. The knight can start and end on any square of the board.
 

Input
The input begins with a positive integer n in the first line. The following lines contain n test cases. Each test case consists of a single line with two positive integers p and q, such that 1 <= p * q <= 26. This represents a p * q chessboard, where p describes how many different square numbers 1, . . . , p exist, q describes how many different square letters exist. These are the first q letters of the Latin alphabet: A, . . .
 

Output
The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the lexicographically first path that visits all squares of the chessboard with knight moves followed by an empty line. The path should be given on a single line by concatenating the names of the visited squares. Each square name consists of a capital letter followed by a number. <br>If no such path exist, you should output impossible on a single line.
 

Sample Input
3 1 1 2 3 4 3
Sample Output
Scenario #1: A1 Scenario #2: impossible Scenario #3: A1B3C1A2B4C2A3B1C3A4B2C4


题目大意就是象棋里的马走“日”,若可以走完整个棋盘,则按照字典序输出其路径。否则输出impossible。

这个题主要是字典序输出的问题。同时通过这个题也学会了在搜索的过程中记录路径。

附源代码:

/*

 棋盘plus plus,注意字典序问题。即搜索的顺序。

 */


#include <iostream>

using namespacestd;

int p,q;

bool flag[100][100];

 int dx[8] = {-1,1,-2,2,-2,2,-1,1};//确定搜索的顺序

 int dy[8] = {-2,-2,-1,-1,1,1,2,2};

int flag1;

struct Point//这个结构体就是记录搜索的路径

{

    int x,y;

}point[26];

void DFS(int x,int y,int n)

{

    int i;

    point[n].x = x;

    point[n].y = y;

    if (n ==p * q)

    {

        flag1 = 1;

        return ;

    }

    else

    {

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

        {

            if (x +dx[i] >= 1 && x + dx[i] <= p && y +dy[i] >= 1 && y + dy[i] <= q &&flag[x + dx[i]][y +dy[i]] != 1 && flag1 == 0)

            {

                flag[x +dx[i]][y + dy[i]] = 1;

                DFS(x+dx[i],y+dy[i],n+1);

                flag[x +dx[i]][y + dy[i]] = 0;

            }

        }

    }

}

int main()

{

    int n;

    while (cin >> n)

    {

    int j;

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

    {

        scanf("%d %d",&p,&q);

        memset(flag,0,sizeof(flag));

        flag[1][1] = 1;

        flag1 = 0;

        DFS(1,1,1);

        cout <<"Scenario #" << j << ":" << endl;

        if (flag1 == 0)

        {

            cout <<"impossible";

            //memset(point,0,sizeof(point));

        }

        else

        {

        int t;

        for (t = 1; t <=p*q; t++)

            cout <<char(point[t].y+64) <<point[t].x;

        }

        cout <<endl;

        if (j != n)

            cout <<endl;

        //memset(point,0,sizeof(point));

    }

    }

    return 0;

}



这个题目的字典序输出困扰了我好久。主要还是确定顺序,即dx,dy。在开始时将横坐标按照大写字母输出,也是个不该错的地方。太粗心……


BFS与DFS的题,要找准搜索的条件,确定好函数的参数。这一点在以后的练习中还要多加注意。


DFS的题,今天做的基本上都是字符的问题。下面附一个比较简单的代码:

(题目大意:求有多少个@块)

样例输入:

1 1

*

3 5

* @ * @ *

* * @ * *

* @ * @ *

1 8

@ @ * * * * @ *

0 0//结束

样例输出:

0

1

2

源代码:

/*地图 @,*

 DFS

 在不超过边界,没有访问过,并且符号为@的情况下,对符号为@该点进行深搜,结果+1

 */


#include <iostream>

#define MAX 110

using namespacestd;

char num[MAX][MAX];

int flag[MAX][MAX] = {0};

int dx[8] = {0,1,1,1,0,-1,-1,-1};

int dy[8] = {1,1,0,-1,-1,-1,0,1};

int m,n;

void DFS(int x,int y)

{

    int i;

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

    {

        if (x +dx[i] < 1 || x + dx[i] > m || y +dy[i] < 1 || y + dy[i] > n)

            continue;

        if (flag[x +dx[i]][y + dy[i]] == 1)

            continue;

        if (num[x +dx[i]][y + dy[i]] == '*')

            continue;

        flag[x +dx[i]][y + dy[i]] = 1;

        DFS(x +dx[i],y + dy[i]);

    }

}

int main()

{

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

    {

        int i,j;

        if (m == 0 &&n == 0)

            break;

        memset(flag,0,sizeof(flag));

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

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

                cin >>num[i][j];

        int sum = 0;

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

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

            {

                if (num[i][j] == '*')

                    continue;

                else

                {

                    if (flag[i][j] != 1)

                    {

                        flag[i][j] = 1;

                        DFS(i,j);

                        sum++;

                    }

                }

            }

        cout << sum <<endl;

    }

    return 0;

}


通过今天的练习,能解决一些比较简单的搜索的题目。需要看的搜索的资料还有很多,这个专题里还有图论的题目。在接下来的时间里,计划是边复习资料,边做题巩固。

虽然累了点,但是一天过得很充实……

不理解为什么第一次发博客的时候内容少了一半……总感觉和第一次比漏了些东西……

想起来再做补充吧。

今天还写了几个代码,都比较简单,有一些过后补博客吧。

一个暑假,总要努力做点什么。

加油吧。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值