水题-poj1979

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了水题-poj1979相关的知识,希望对你有一定的参考价值。

#include <iostream>
#include <string>

using namespace std;

int row,col;
int dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}};
char map[20][20];
bool vised[20][20];

void dfs(int posX,int posY)
{
    for (int i = 0; i != 4; ++ i)
    {
        int offX,offY;
        offX = posX + dir[i][0];//i先为0,x方向移动0,
        offY = posY + dir[i][1];//i先为0,y方向移动-1
        if (offX >= 0 && offX < row && offY >= 0 && offY < col)
        {
            if (!vised[offX][offY] && map[offX][offY] == ‘.‘)
            {
                //cout<<"x is :"<<offX<<" "<<"y is :"<<offY<<endl;
                vised[offX][offY] = true;
                dfs(offX, offY);
            }
        }
    }
}
int main()
{
    while (cin>>col>>row)
    {
        int sX = 0,sY = 0;
        int cnt = 0;
        
        if (row <= 0 || col <= 0)
        {
            break;
        }
        memset(vised, false, sizeof(vised));
        //map
        for (int i = 0; i != row; ++ i)
        {
            for (int j = 0; j != col; ++ j)
            {
                cin>>map[i][j];
                if (map[i][j] == ‘@‘)
                {
                    sX = i;
                    sY = j;
                }
            }
        }
        vised[sX][sY] = 1;
        dfs(sX,sY);
        for (int i = 0; i != row; ++ i)
        {
            for (int j = 0; j != col; ++ j)
            {
                if (vised[i][j])
                {
                    cnt ++;
                }
            }
        }
        cout<<cnt<<endl;
    }
    return 0;
}

  dfs深度遍历。

以上是关于水题-poj1979的主要内容,如果未能解决你的问题,请参考以下文章

POJ1979

POJ 1979 Red and Black (简单dfs)

POJ 1979 DFS

poj1979 解题报告

[dfs] [FloodFill] POJ1979 Red And Black

POJ1979 Red and Black