HDU - 1312 【DFS】(别踩红块儿~)

觉得自己把深搜忘干净了,递归着递归着就会了呢~【虽然改了贼久。。】

最简单深搜~


题目描述:

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

题目大意:

       给定一张地图,上面 “  .  ” 代表可以通行,“ # ”代表障碍物,不可通过;给定起点,问可到达多少点?

解题思路:

       深搜呀~,四个方向遍历呀~

     【无数的小错误让我感到头秃,,】

代码实现:

#include<iostream>
#include<cstring>
using namespace std;
int x,
    y,
    ans=0,
    mapp[21][21],
    biao[21][21],
    d[2][4]={{-1,+1, 0, 0},{ 0, 0,-1,+1}};

//DFS
void dfs(int sx,int sy){

    if(sx<=0||sx>y||sy<=0||sy>x||mapp[sx][sy]==1||biao[sx][sy]==1){
        return ;
    }
    if(biao[sx][sy]==0){
        ans++;
        biao[sx][sy]=1;
    }

    for(int i=0;i<=3;i++){
        dfs(sx+d[0][i],sy+d[1][i]);
    }
}

int main(){
    while(1){
        cin>>x>>y;
        if(x==0&&y==0){
            return 0;
        }

        //初始化
        ans=0;
        memset(mapp,0,sizeof(mapp));
        memset(biao,0,sizeof(biao));

        //输入
        int stx,sty;
        for(int i=1;i<=y;i++){
                char ch;
            for(int j=1;j<=x;j++){
                cin>>ch;
                if(ch=='.'){

                }else if(ch=='#'){
                    mapp[i][j]=1;
                    biao[i][j]=1;
                }else if(ch=='@'){
                    stx=i;
                    sty=j;
                }
            }
        }
        dfs(stx,sty);
        cout<<ans<<endl;
    }
}

PS:如果你想看代码内部实现过程,come here~【一般有这样丑丑的代码出现,就说明,debug的路程比较艰辛】

#include<iostream>
#include<cstring>
using namespace std;
int x,
    y,
    ans=0,
    mapp[21][21],
    biao[21][21],
    d[2][4]={{-1,+1, 0, 0},{ 0, 0,-1,+1}};

void dfs(int sx,int sy){
    cout<<"\n  sx="<<sx<<"  sy="<<sy;

    if(sx<=0||sx>y||sy<=0||sy>x||mapp[sx][sy]==1||biao[sx][sy]==1){
        cout<<"不可以"<<endl;
        return ;
    }
    if(biao[sx][sy]==0){
        ans++;
        biao[sx][sy]=1;
        cout<<"可以  "<<endl;
    }
cout<<"\n  biao="<<endl;
for(int i=1;i<=y;i++){
        cout<<"  ";
    for(int j=1;j<=x;j++){
        cout<<biao[i][j];
    }cout<<endl;
}cout<<"  ans="<<ans<<endl;
    for(int i=0;i<=3;i++){
        dfs(sx+d[0][i],sy+d[1][i]);
    }
}

int main(){
    while(1){
        cin>>x>>y;
        if(x==0&&y==0){
            return 0;
        }
        int stx,
            sty;
        ans=0;
        memset(mapp,0,sizeof(mapp));
        memset(biao,0,sizeof(biao));
        for(int i=1;i<=y;i++){
                char ch;
            for(int j=1;j<=x;j++){
                cin>>ch;
                if(ch=='.'){

                }else if(ch=='#'){
                    mapp[i][j]=1;
                    biao[i][j]=1;
                }else if(ch=='@'){
                    stx=i;
                    sty=j;
                }
            }
        }
cout<<"\nmap="<<endl;
for(int i=1;i<=y;i++){
    for(int j=1;j<=x;j++){
        cout<<mapp[i][j];
    }cout<<endl;
}
cout<<"\nbiao="<<endl;
for(int i=1;i<=y;i++){
    for(int j=1;j<=x;j++){
        cout<<biao[i][j];
    }cout<<endl;
}

        dfs(stx,sty);
        cout<<ans<<endl;
    }
}

猜你喜欢

转载自blog.csdn.net/sodacoco/article/details/84207844