SSL-ZYC 2324 cell problem

;

Main idea:
A rectangular array consists of the numbers 0 to 9. The numbers 1 to 9 represent cells. The definition of cells is whether the number of cells is the same cell along the top and bottom of the cell number or the cell number. Find the number of cells in a given rectangular array.

For example:

There are 4 cells in all.

Ideas:
This is a problem of BFS. Search a[n][m] from a[1][1], find a cell, then clear the cell and the cell next to it, and count.

That is to say, BFS is carried out once every cell is found, and the next cell is searched after clearing the adjacent cells.

(warm tip: the numbers 1, 2, 3, 4, 5, 6, 7, 8 and 9 are actually the same. They are all cells and do not need to be treated separately.)

code:

#include <iostream>
#include <cstdio>
using namespace std;
bool a[101][101];
int n,m,father[10001],state[3][10001],sum;
int dx[5]={0,0,0,1,-1};  
int dy[5]={0,-1,1,0,0};

void bfs(int x,int y)
{
    int head,tail,i,j,qx,qy;
    head=0;
    tail=1;
    father[1]=0;
    a[x][y]=0;
    state[1][1]=x;
    state[2][1]=y;  //initialization 
    do
    {
        head++;
        for (i=1;i<=4;i++)
        {
            qx=state[1][head]+dx[i];  //qxRepresent line 
            qy=state[2][head]+dy[i];  //qyPresentation column 
            if (qx>0&&qx<=n&&qy>0&&qy<=m&&a[qx][qy])  //If it's not out of bounds and it's a cell 
            {
               tail++;  
               father[tail]=head;  //Join the team 
               state[1][tail]=qx;  
               state[2][tail]=qy;  
               a[qx][qy]=0;
            }
        }
    }
    while(head<tail);
}
int main()
{
    int i,j;
    char read;
    scanf("%d%d\n",&n,&m);
    for (i=1;i<=n;i++)  
    {  
       for (j=1;j<=m;j++)  
       {  
          read=getchar();  
          if (read>='1'&&read<='9') a[i][j]=1; 
       }  
       read=getchar();  
    }  
    for (i=1;i<=n;i++)
     for (j=1;j<=m;j++)  //Cell by cell 
     {
      if (a[i][j]==1)  //If this is a cell 
      {
        bfs(i,j);  //Look for the cell next to it 
        sum++;  //count 
      }
     }
    printf("%d",sum);
    return 0;
}

(this problem is very similar to finding oil, basically the same way.)

Posted by dt_gry on Tue, 05 May 2020 17:54:28 -0700