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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章