zoukankan      html  css  js  c++  java
  • 求细胞数量pascal题解

    这题我是用广度优先搜索来做的

    一开始先一个一个点找,如果这个点是细胞,那么就搜索。

    注意:可以把数组开大点


    const
    dx:array[1..4]of longint=(0,-1,0,1);
    dy:array[1..4]of longint=(1,0,-1,0);
    var
    n,m,i,j,tj:longint;
    a:array[-10..100,-10..100]of boolean;
    x:char;
    state:array[0..4000,0..4000]of longint;


    procedure bfs(x,y:longint);
    var
    head,tail,i:longint;
    begin
        inc(tj);
        a[x,y]:=false;
        head:=0;tail:=1;
        state[1,1]:=x;state[1,2]:=y;
        repeat
             inc(head);
             for i:=1 to 4 do
             begin
                 x:=state[head,1]+dx[i];
                 y:=state[head,2]+dy[i];
                 if (x>0)and(x<=m)and(y>0)and(y<=n)and(a[x,y]=true) then
                 begin
                     inc(tail);
                     state[tail,1]:=x;
                     state[tail,2]:=y;
                     a[x,y]:=false;
                 end;
             end;
        until head>=tail;
    end;


    begin
        readln(m,n);
        fillchar(a,sizeof(a),true);
        for i:=1 to m do
        begin
            for j:=1 to n do
            begin
                read(x);
                if x='0' then a[i,j]:=false;
            end;
            readln;
        end;
        for i:=1 to m do
        for j:=1 to n do
        if a[i,j]=true then bfs(i,j);
        write(tj);
    end.




  • 相关阅读:
    ArcPad 10 的安装部署
    各种机械键盘轴的差别,究竟什么轴好
    default argument given of parameter 的问题
    Quartz中时间表达式的设置-----corn表达式
    图像切割之(一)概述
    SMTP协议分析
    Android学习小Demo(19)利用Loader来实时接收短信
    qml动画控制器AnimationController
    httpclient 文件上传
    Java习题10.24
  • 原文地址:https://www.cnblogs.com/YYC-0304/p/9500231.html
Copyright © 2011-2022 走看看