Hướng dẫn giải của Bãi cỏ ngon nhất


Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.

Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này

Code mẫu của flashmt

var m,n,re,i,j:longint;
    a:array[1..100] of string;
    d:array[1..100,1..100] of boolean;

begin
        readln(m,n);
        for i:=1 to m do readln(a[i]);
        for i:=1 to m do
            for j:=1 to n do
               if not d[i,j] and (a[i][j]='#') then
               begin
                    inc(re);
                    if (i<m) and (a[i+1,j]='#') then d[i+1,j]:=true;
                    if (j<n) and (a[i,j+1]='#') then d[i,j+1]:=true;
               end;
        writeln(re);
end.

Code mẫu của happyboy99x

#include <cstdio>
#include <utility>
using namespace std;

typedef pair<int, int> ii;
#define fi first
#define se second

int r, c;
char s[105][105];
int dX[] = {-1, 0, 0, 1}, dY[] = {0, -1, 1, 0};
ii q[10005];

int main() {
    scanf( "%d%d\n", &r, &c ); int res = 0;
    for( int i = 0; i < r; ++i ) gets(s[i]);
    for( int i = 0; i < r; ++i ) for( int j = 0; j < c; ++j )
        if( s[i][j] == '#' ) {
            ++res;
            s[i][j] = '.'; int l = 0, h = 1; q[0] = ii(i, j);
            do {
                for( int i = 0; i < 4; ++i ) {
                    int x = q[l].fi + dX[i], y = q[l].se + dY[i];
                    if ( x >= 0 && x < r && y >= 0 && y < c && s[x][y] == '#' ){
                        q[h++] = ii(x, y);
                        s[x][y] = '.';
                    }
                }
            } while( ++l < h );
        }
    printf( "%d\n", res );
    return 0;
}

Code mẫu của ladpro98

program vbgrass;
uses    math;
const   fi='';
var     a:array[1..101,1..101] of char;
        r,c,res:longint;

procedure input;
var     inp:text;
        i,j:longint;
begin
        assign(inp,fi);
        reset(inp);
        readln(inp,r,c);
        for i:=1 to r do
        begin
                for j:=1 to c do
                read(inp,a[i,j]);
                readln(inp);
        end;
        close(inp);
end;

procedure process;
var     i,j:longint;
begin
        for i:=1 to r do
        for j:=1 to c do
        begin
                if a[i,j] = '#' then
                begin
                        inc(res);
                        if a[i,j+1] = '#' then
                        begin
                                a[i,j+1]:='.';
                                continue;
                        end;
                        if a[i+1,j] = '#' then
                                a[i+1,j]:='.';
                end;
        end;
end;

procedure output;
begin
        write(res);
end;

begin
        input;
        process;
        output;
end.

Code mẫu của RR

var
  a:array[1..111,1..111] of char;
  cnt,n,m,i,j:longint;
begin
  readln(m,n); cnt:=0;
  for i:=1 to m do
    begin
      for j:=1 to n do
        read(a[i,j]);
      readln;
    end;

  for i:=1 to m do
  for j:=1 to n do
    if a[i,j]='#' then
      begin
        inc(cnt);
        if a[i,j+1]='#' then a[i,j+1]:='.';
        if a[i+1,j]='#' then a[i+1,j]:='.';
      end;
  writeln(cnt);
end.

Code mẫu của hieult

#include <stdio.h>
main()
{
char s[101][101],a[101][101];
int n,m,u=0;
scanf("%d %d",&n,&m);
for(int i=0;i<n;i++)
  for(int j=0;j<m+1;j++)
    {
    scanf("%c",&s[i][j]);
    a[i][j]=0;
    }
for(int i=0;i<n;i++)
  for(int j=0;j<m+1;j++)
    if(s[i][j]=='#')
    {
    if(s[i-1][j]=='#'&&a[i-1][j]!=1)
    a[i][j]=1;
    else if(s[i][j-1]=='#'&&a[i][j-1]!=1)
      {
      a[i][j]=1;
      a[i][j-1]=1;
      }
    else
      u++;
    }
printf("%d",u);
}

Code mẫu của ll931110

Program VBGRASS;
        Const
                input  = '';
                output = '';
        Var
                a: array[1..101,1..101] of boolean;
              r,c: integer;

Procedure init;
          Var
                 f: text;
                ch: char;
               i,j: integer;
          Begin
                Assign(f, input);
                        Reset(f);
                                Fillchar(a, sizeof(a), false);
                                Readln(f, r, c);
                                For i:= 1 to r do
                                    Begin
                                        For j:= 1 to c do
                                                Begin
                                                        Read(f, ch);
                                                        If ch = '#' then a[i,j]:= true;
                                                End;
                                        Readln(f);
                                    End;
                Close(f);
          End;


Procedure solve;
          Var
                    f: text;
                k,i,j: integer;
          Begin
                Assign(f, output);
                        Rewrite(f);
                                k:= 0;
                                For i:= 1 to r do
                                        For j:= 1 to c do
                                                If a[i,j] then
                                                        Begin
                                                              a[i,j]:= false;
                                                              inc(k);

                                                              If a[i, j + 1] then a[i, j + 1]:= false;
                                                              If a[i + 1, j] then a[i + 1, j]:= false;
                                                        End;
                                Writeln(f, k);
                Close(f);
          End;

Begin
        init;
        solve;
End.

Code mẫu của khuc_tuan

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws Exception {
        // BufferedReader kb = new BufferedReader(new
        // InputStreamReader(System.in));
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int n = sc.nextInt();
        char[][] a = new char[m][];
        for (int i = 0; i < m; ++i)
            a[i] = sc.next().toCharArray();
        boolean[][] vs = new boolean[m][n];
        int dem = 0;
        for (int i = 0; i < m; ++i)
            for (int j = 0; j < n; ++j)
                if (!vs[i][j] && a[i][j] == '#') {
                    ++dem;
                    vs[i][j] = true;
                    Queue<int[]> q = new LinkedList<int[]>();
                    q.add(new int[] { i, j });
                    while (!q.isEmpty()) {
                        int[] ar = q.remove();
                        int u = ar[0];
                        int v = ar[1];
                        for (int dx = -1; dx <= 1; ++dx)
                            for (int dy = -1; dy <= 1; ++dy)
                                if ((dx == 0) ^ (dy == 0)) {
                                    int x = dx + u;
                                    int y = dy + v;
                                    if (x < 0 || x >= m || y < 0 || y >= n)
                                        continue;
                                    if (vs[x][y])
                                        continue;
                                    if (a[x][y] == '#') {
                                        vs[x][y] = true;
                                        q.add(new int[] { x, y });
                                    }
                                }
                    }
                }
        System.out.println(dem);
    }
}

Bình luận

Hãy đọc nội quy trước khi bình luận.


Không có bình luận tại thời điểm này.