【algorithm】BFS实例一

来源:互联网 发布:()知劲草,路遥知马力 编辑:程序博客网 时间:2024/05/22 00:43

简易BFS 模板


typedef struct _queue
{
 int x;
 int y;
 int count;
}Queue;

int num = 0;
int top, bottom;
Queue queue[NUM];
int visit[MAX][MAX] = { 0, };
char data[MAX][MAX];
int dir_x[4] = { -1, 1, 0, 0 };
int dir_y[4] = { 0, 0, -1, 1 };

top = bottom = 0;
  queue[top].x = r;
  queue[top].y = q;
  queue[top].count = num++;
  top++;
  visit[r][q] = 1;

  int tempx, tempy;
  while (top != bottom)
  {
   x = queue[bottom].x;
   y = queue[bottom].y;

   for (i = 0; i < 4; i++)
   {
    tempx = x + dir_x[i];
    tempy = y + dir_y[i];
    if (0 <= tempx && tempx < M && 0 <= tempy && tempy < N && !visit[tempx][tempy])
    {
     if (//这里写你想要的条件)
     {
      visit[tempx][tempy] = 1;
      queue[top].x = tempx;
      queue[top].y = tempy;
      queue[top].count = num++;
      top++;
     }
    }
   }

   bottom++;
  }

例题:

Problem Explanation

 

ABC Remote sensing is a government scientific organization to analyze various data provided by remote sensing satellites. Based on satellite data they received one assignment to analyze a large forest. The remote sensed values input data is received as a rectangular Map of 24-bit raw image pixel values. From it scientist are able to process it further and categorize each pixel value to range0 to 5. Each value0 ~ 5 indicates a particular type of tree or vegetation for the purpose of further research.

 

Scientists as ABC Remote sensing organization are working on a research paper, which is proposing a new concept on forest vegetation.  They apply a special filter on the rectangular input map having cell values0 ~ 5 with certain rules.

 

Ifup, down, left and right neighboring cells of acell A are of same value ascell A value, they are treated as “connected” cells.  The group of cells that are connected together and hence form achain of connected cells calledClusters.

 

You have to develop an algorithm for this processing of input map with a special filter as explained below.

 

Example:

 

Consider below Input Map of 5 x 5 size:

 

5

5

1

4

4

3

0

2

2

2

5

0

0

2

0

5

0

3

0

1

1

3

3

4

1

Below highlighted map shows one such case where focus is on a cluster ofvalue 0 (cluster size 4) shown marked withyellow color. The neighbor cluster values (2,3 and5) are highlighted in different colors to help visualize the neighbor clusters.

 

5

5

1

4

4

3

0

2

2

2

5

0

0

2

0

5

0

3

0

1

1

3

3

4

1

 

Below table describes the Overall Max occurring Neighbor cluster for each cluster of above input map:

 

Value

Cluster Size

Neighbor Value

(Max Occurring Neighbor Cluster)

Total Occurrence count

(of Max Neighbor Cluster)

Remark

0

4

5

4

Neighbor Value 2 occurs 4 times.

Neighbor Value 3 occurs 4 times.

Neighbor Value 5 occurs 4 times.

So max occurring neighbor cluster values are 2, 3 and 5.

But value 5 is greater than 3 and 2, so neighbor value 5 gets selected

0

1

2

4

 

0

1

2

4

 

1

1

2

4

 

1

2

0

2

 

1

1

3

3

 

2

4

0

6

 

3

1

5

4

Value 5 and 0 both occur 4 times in neighbor groups.

Since 5 is greater than 0, so 5 gets selected.

3

3

0

5

 

4

2

2

4

 

4

1

3

3

 

5

2

0

4

 

5

2

0

4

 

 

You have to find maximum occurring neighbor clusters and replace the cluster value with the value of max occurring neighbor cluster.   It has to be done for all clusters occurring in the input map. All the replacement is done after doing the final calculation of all the clusters occurring in input map.

 

So, in above example case, the final output Map becomes:

 

0

0

2

2

2

5

5

0

0

0

0

5

5

0

2

0

5

0

2

0

3

0

0

3

0

 

Constraints:

1. The values in clusters will be from 0 to 5 only.

2. The number of row (NR) or number of columns (NC) is not more than 100.

2. The number of row (NR) or number of columns (NC) is greater than or equal to 2.

3. There is no case that all cell values in input map have same value.

4. So, it is sure that there will always be more than 1 cluster in given input map.

 

Input:

First line contains the value indicating number of test cases (T). Then T test cases follow further in input. Each test case contains the first row as two numbers indicating number of rows (NR) and number of columns (NC) respectively. After that NR numbers of lines corresponding to each input row appear. Each line related to a row contains NC cell values separated by a space.

 

Output:

The first output line for each test case should be“Case #tn, wheretn is the test case number. The second line contains the number of clusters in the final Map that is made by replacing the max neighbor clusters as explained above inProblem Explanation section.

 

Sample Input:

6

5 5             (Note: It is same input as above explained example)

5 5 1 4 4

3 0 2 2 2

5 0 0 2 0

5 0 3 0 1

1 3 3 4 1

5 5

5 5 1 4 4

3 0 2 2 2

5 0 0 2 0

5 0 3 0 1

1 3 3 4 1

5 5

5 5 1 4 4

5 0 2 2 2

5 0 0 2 0

5 0 3 0 1

1 3 3 4 1

2 5

1 0 1 0 1

2 2 0 0 1

6 7

0 1 1 0 0 3 0

0 1 1 0 0 3 3

0 0 0 5 0 4 0

2 2 0 5 5 4 0

2 2 2 0 0 4 0

0 0 0 5 5 4 0

2 2

1 2

3 4

 

 

Sample Output:

Case #1

11

Case #2

11

Case #3

11

Case #4

5

Case #5

9

Case #6

4



分析:

1)这道题目的难点在于找到转化的矩阵,至于之后求有几个区域就是最基本的BFS

2)找转化矩阵也是用BFS算法实现的,对于一个点要考虑3中情况:

情况一:相邻的点和自己相等,这种点要继续去找其本身相邻的点,找到的点仍然可能是三种情况之一。

情况二:相邻的点和自己不等,这种点的周围要继续去找第三种点。

情况三: 相邻的点的相邻的点,和相邻的点相等的。这种找到了之后也只能继续找第三种点。



实现:

#include <stdio.h>
#include <string.h>
typedef struct _queue
{
 int x;
 int y;
 int count;
}Queue;

int Answer;
#define MAX 101
int A[MAX][MAX];
int NR, NC;
int New[MAX][MAX];
Queue Q[MAX*MAX];

int visit[MAX][MAX];
int dir_x[4] = { -1, 1, 0, 0 };
int dir_y[4] = { 0, 0, -1, 1 };

int count[6];


int bfs(int x, int y)
{
 int i,j;
 int value = A[x][y];
 memset(count, 0, sizeof(count));
 memset(visit, 0, sizeof(visit));


 int top=0;
 int bottom=0;

 Q[top].x = x;
 Q[top].y =y;
 top++;
 visit[x][y]=1;
 while(top!=bottom)
 {
  Queue temp = Q[bottom];
  visit[temp.x][temp.y]=1;
  for(i=0;i<4;i++)
  {
   int tx = temp.x+dir_x[i];
   int ty = temp.y+dir_y[i];

   if(visit[tx][ty]==0 && tx>0 && tx<=NR && ty>0 && ty<=NC )
   {
    if( A[temp.x][temp.y] ==value && A[tx][ty] != value)
    {
     visit[tx][ty]=1;     
     count[A[tx][ty]]++;
     Q[top].x = tx;
     Q[top].y =ty;
     top++; 
    }

    if( A[temp.x][temp.y] ==A[tx][ty] && A[temp.x][temp.y]!=value)
    { 
     visit[tx][ty]=1;
     count[A[tx][ty]]++;
     Q[top].x = tx;
     Q[top].y =ty;
     top++; 
    }

    if( A[temp.x][temp.y] == A[tx][ty] && A[tx][ty] ==value )
    {
     visit[tx][ty]=1;
     Q[top].x = tx;
     Q[top].y =ty;
     top++;
    }
   }  
  }
  bottom++;
 }

 int max =-1;
 int pos = 0;
 for(i=0;i<6;i++)
 {
  if(count[i]>=max)
  {
   max=count[i];
   pos = i;
  }
 }
 
 return  pos;

}

void bfs2(int x,int y)
{

 int i;
 int top=0;
 int bottom=0;

 Q[top].x = x;
 Q[top].y = y;
 top++;
 visit[x][y]=1;
 while(top!=bottom)
 {
  Queue temp = Q[bottom];
  visit[temp.x][temp.y]=1;
  for(i=0;i<4;i++)
  {
   int tx = temp.x+dir_x[i];
   int ty = temp.y+dir_y[i];

   if(visit[tx][ty]==0 && tx>0 && tx<=NR && ty>0 && ty<=NC && New[tx][ty] == New[temp.x][temp.y])
   {    
     visit[tx][ty]=1;
     Q[top].x = tx;
     Q[top].y =ty;
     top++; 
   }
  }
  bottom++;
 }


}

int main(void)
{
 int T, test_case;
 setbuf(stdout, NULL);
 scanf("%d", &T);
 for(test_case = 0; test_case < T; test_case++)
 {
  scanf("%d %d", &NR, &NC);
  int i,j;
  for(i=1;i<=NR;i++)
  {
   for(j=1;j<=NC;j++)
   {
    scanf("%d", &A[i][j]);   
   }
  }
  memset(visit, 0, sizeof(visit));
  memset(New, 0, sizeof(New));

  for(i=1;i<=NR;i++)
  {
   for(j=1;j<=NC;j++)
   {
    if(i-1>0 && (A[i][j] == A[i-1][j]))
    {
     New[i][j] = New[i-1][j];
    }
    else if(j-1>0 && (A[i][j] == A[i][j-1]))
    {
     New[i][j] = New[i][j-1];
    }
    else
    {
     New[i][j] = bfs(i,j);
    }
   }
  }

  memset(visit, 0, sizeof(visit));
  Answer = 0;

  for(i=1;i<=NR;i++)
  {
   for(j=1;j<=NC;j++)
   {     
    if(visit[i][j] == 0)
    {
     bfs2(i,j);
     Answer++;
    }
   }   
  }


  printf("Case #%d\n", test_case+1);
  printf("%d\n", Answer);
 }

 return 0;//Your program should return 0 on normal termination.
}


0 0
原创粉丝点击