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