HOJ1797 Red and Black 基础搜索

这里的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;
}
相关推荐
<p> 课程演示环境:Windows10  </p> <p> 需要学习<span>Ubuntus</span>系统<span>YOLOv4-tiny</span>的同学请前往《<span>YOLOv4-tiny</span>目标检测实战:训练自己的数据集》 <span></span> </p> <p> <span> </span> </p> <p> <span style="color:#E53333;">YOLOv4-tiny</span><span style="color:#E53333;">来了!速度大幅提升!</span><span></span> </p> <p> <span> </span> </p> <p> <span>YOLOv4-tiny</span>在<span>COCO</span>上的性能可达到:<span>40.2% AP50, 371 FPS (GTX 1080 Ti)</span>。相较于<span>YOLOv3-tiny</span>,<span>AP</span>和<span>FPS</span>的性能有巨大提升。并且,<span>YOLOv4-tiny</span>的权重文件只有<span>23MB</span>,适合在移动端、嵌入式设备、边缘计算设备上部署。<span></span> </p> <p> <span> </span> </p> <p> 本课程将手把手地教大家使用<span>labelImg</span>标注和使用<span>YOLOv4-tiny</span>训练自己的数据集。课程实战分为两个项目:单目标检测(足球目标检测)和多目标检测(足球和梅西同时检测)。<span></span> </p> <p> <span> </span> </p> <p> 本课程的<span>YOLOv4-tiny</span>使用<span>AlexAB/darknet</span>,在<span>Windows10</span>系统上做项目演示。包括:<span>YOLOv4-tiny</span>的网络结构、安装<span>YOLOv4-tiny</span>、标注自己的数据集、整理自己的数据集、修改配置文件、训练自己的数据集、测试训练出的网络模型、性能统计<span>(mAP</span>计算<span>)</span>和先验框聚类分析。 <span> </span> </p> <p> <span> </span> </p> <p> 除本课程《<span>Windows</span>版<span>YOLOv4-tiny</span>目标检测实战:训练自己的数据集》外,本人推出了有关<span>YOLOv4</span>目标检测的系列课程。请持续关注该系列的其它视频课程,包括:<span></span> </p> <p> 《<span>Windows</span>版<span>YOLOv4</span>目标检测实战:训练自己的数据集》<span></span> </p> <p> 《<span>Windows</span>版<span>YOLOv4</span>目标检测实战:人脸口罩佩戴识别》<span></span> </p> <p> 《<span>Windows</span>版<span>YOLOv4</span>目标检测实战:中国交通标志识别》<span></span> </p> <p> 《<span>Windows</span>版<span>YOLOv4</span>目标检测:原理与源码解析》<span></span> </p> <p> <span> <img alt="" src="https://img-bss.csdnimg.cn/202007061503586145.jpg" /></span> </p> <p> <span><img alt="" src="https://img-bss.csdnimg.cn/202007061504169339.jpg" /><br /> </span> </p>
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页