HOJ1797 Red and Black 基础搜索

本文解析了HOJ1797题目——RedandBlack,介绍了使用深度优先搜索(DFS)和宽度优先搜索(BFS)两种方法解决该问题的具体实现。通过实例输入输出展示了算法的效果。

这里的HOJ指的是哈工大的HOJ不是杭电的HOJ= =杭电的大家一般都叫他HDUOJ~

Red and Black(HOJ1797)

Source : Japan Domestic 2004
Time limit : 1 sec Memory limit : 32 M

There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can’t move on red tiles, he can move only on black tiles.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.


Input

The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
‘.’ - a black tile
‘#’ - a red tile
‘@’ - a man on a black tile(appears exactly once in a data set)


Output

For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).


Sample Input

6 9
….#.
…..#
……
……
……
……
……
#@…#
.#..#.
11 9
.#………
.#.#######.
.#.#…..#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#…….#.
.#########.
………..
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
…@…
###.###
..#.#..
..#.#..
0 0


Sample Output

45
59
6
13


如果我能有幸让你看到这篇文章,说明你和我一样也是初学者,让我们一起努力吧(((o(゚▽゚)o)))

很基础的搜索入门题,DFS深度优先遍历或者BFS宽度优先遍历都可以,而且给的数据范围允许我们用搜索来做,这种每个点的值都要考虑的题也只能用搜索来做了,DFS一般以栈或递归调用实现,而BFS一般用队列实现。

DFS代码

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;
bool vis[105][105];
char grid[105][105];
int n, m, CNT;

bool dfs(int x, int y, int depth)
{
/** 这里完全可以把返回值设为空,返回值为bool的dfs函数多用于判断迷宫中能否达到目的地之类的问题。这里dfs函数的作用是标记vis用来表示哪些点是能够被访问到的,然后在扫描vis数组判断被标记出来的点有多少个,然后输出答案
*/
    if(x < 0 || y < 0 || x >=m || y >= n || vis[x][y])
        return false;

    if(grid[x][y] == '#') 
        return false;
/*判断数组下标是否越界,并且如果下标越界或者该点被访问过,返回false(但其实主要作用是避免把该点vis[x][y]标记为true)*/

    vis[x][y] = true;
/*vis[x][y]用于标记grid[x][y]是否已经访问过,作用是防止死循环,并且提高程序效率*/
    if(dfs(x,y+1,depth+1)) return true;
    if(dfs(x,y-1,depth+1)) return true;
    if(dfs(x-1,y,depth+1)) return true;
    if(dfs(x+1,y,depth+1)) return true;
    //四个方向都递归调用dfs

    return false; 
}

int main()
{
    while(true)
    {
        memset(vis, false, sizeof(vis));
        //不要忘记初始化;
        cin >> n >> m;
        if(n == 0 && m == 0) break;
        for(int i=0; i<m; i++)
            scanf("%s", grid[i]);
        CNT = 0;
        int posx = 0, posy = 0;

        for(int i=0; i<m; i++)
        {
            for(int j=0; j<n; j++)
            {
                if(grid[i][j] == '@')
                {
                    posx = i;
                    posy = j;
                    break;
                    //找到起始位置
                }
            }
        }
        dfs(posx, posy, 0);

        for(int i=0; i<m; i++)
            for(int j=0; j<n; j++)
                if(vis[i][j] == true) CNT++;
        cout << CNT << endl;
    }

    return 0;
}

BFS的思路类似,也是从初始位置开始搜索,然后每搜索到一个符合条件的(!= ‘#’)没有被访问过的(vis[x][y] != true)的点就把vis[x][y]标记成true,不符合条件就不再以这个点为基础向外遍历了,在该点的搜索停止,再在其他点的基础上搜索,最后再统计被标记的点的数量。

BFS:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue> 
//需要用到stl中的队列

using namespace std;
bool vis[105][105];
char grid[105][105];
int n, m;
int CNT;
int step[] = {0, 0, 1, -1}; 
//在有很多个遍历方向的时候用几层循环搭配step数组,这样会精简你的代码,具体实现往下看
struct p
{
    int x, y;
};
//bfs常会用到结构体

void bfs(int posx, int posy, queue<p> q)
{
    p point;
    while(!q.empty())
    {
        posx = q.front().x;
        posy = q.front().y;
        for(int i=0; i<4; i++)
        {
            for(int j=0; j<4; j++)
            {
                int x = posx+step[i];
                int y = posy+step[j];
                if(step[i]*step[j] == 0 && x < m && y < n && x*y >= 0 &&)
                {
                    if(grid [x][y] != '#' && vis[x][y] != true)
                    {
                        point.x = x;
                        point.y = y;
                        vis[point.x][point.y] = true;
                        q.push(point);
                    }
                }
            }
        }
        q.pop();//不要忘记弹出原有的元素,否则循环会死的-_-#
    }
    return;
}
int main()
{
    while(true)
    {
        queue<p> q;

        memset(vis, false, sizeof(vis));
        cin >> n >> m;
        if(n == 0 && m == 0) break;
        for(int i=0; i<m; i++)
            scanf("%s", grid[i]);
        CNT = 0;
        int posx = 0, posy = 0;

        for(int i=0; i<m; i++)
        {
            for(int j=0; j<n; j++)
            {
                if(grid[i][j] == '@')
                {
                    posx = i;
                    posy = j;
                    break;
                }
            }
        }
        p point;
        point.x = posx;
        point.y = posy;
        q.push(point);

        bfs(posx, posy, q);

        for(int i=0; i<m; i++)
            for(int j=0; j<n; j++)
                if(vis[i][j] == true) CNT++;

        cout << CNT << endl;
    }

    return 0;
}
内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值