[codevs1569]最佳绿草 dfs练习

dfs,每次搜到打标签,制成false,把整个图搜一遍,每次搜到联通块时答案加一。

var
        map:array[0..110,0..110] of boolean;
        i,j,n,c,ans:longint;
        ok:boolean;
        ch:char;

        function dfs(i,j:longint):boolean;
        var t:longint;
        begin
                if not(map[i][j]) then exit(false);
                t:=0;
                ok:=false;
                if map[i][j] then
                begin
                        inc(t);
                        ok:=true;
                        map[i][j]:=false;
                        dfs(i,j-1);
                        dfs(i,j+1);
                        dfs(i+1,j);
                        dfs(i-1,j);
                end;
                exit(ok);
        end;



        begin
                fillchar(map,sizeof(map),false);
                readln(n,c);
                for i:=1 to n do
                begin
                for j:=1 to c do
                begin
                        read(ch);
                        if ch='#' then map[i][j]:=true;
                end;
                readln;


                end;

                        for i:=1 to n do
                        for j:=1 to c do
                        if dfs(i,j)then inc(ans);
                writeln(ans);
        end.

 喜欢就收藏一下,vic私人qq:1064864324,加我一起讨论问题,一起进步^-^

posted @ 2015-09-23 21:03  ROLL-THE-FIRST  阅读(134)  评论(0编辑  收藏  举报