1501 Country in a map

来源:互联网 发布:淘宝上做代理赚钱吗 编辑:程序博客网 时间:2024/06/05 20:18

Accept: 264    Submit: 456
Time Limit: 1000 mSec    Memory Limit : 32768 KB

Problem Description

You are given a rectangular map of a continent. The map is a grid of squares, each square has a color. A country is a connected set of squares (squares are connected if they share an edge) that have the same color. Neighboring countries have different colors. Find the size (in squares) of the largest country.

There will be at least one country in the map.

Input

Input data will be provided as standard input to the program. The first line of input contains the number of test cases (<=10). Each test case begins with a line containing two numbers W and H, the width and height of the map, separated by a single space, 1 <= W,H <= 10. The next H lines contain W characters each | they describe the map. Colors are represented by the letters A-Z. Water (no country) is represented by a dot.

Output

For each test case output one line containing the size of the largest country.

Sample Input

1
6 5
...BBB
.AAAAB
..BBA.
..BB..
......

Sample Output

5

Source

FOJ月赛-2007年5月
我的程序:
#include<iostream>
using namespace std;
typedef struct{
 char val;
 bool flag;
}VF;
VF map[100][100];
char ch;
int thisarea=0;
int m,n;
int visit(int i,int j)
{
 if(map[i][j].flag==false && map[i][j].val!='.' && map[i][j].val==ch)
 {
  map[i][j].flag=true;
  thisarea++;
  if(i-1>=0)
   visit(i-1,j);
  if(i+1<m)
   visit(i+1,j);
  if(j-1>=0)
   visit(i,j-1);
  if(j+1<n)
   visit(i,j+1);
 }
 return thisarea;
}
int main()
{
 int i,j,count,area;
 cin>>count;
 while(count>0)
 {
  area=0; 
              cin>>n>>m;
  for(i=0;i<m;i++)
   for(j=0;j<n;j++)
   {
    cin>>map[i][j].val;
    map[i][j].flag=false;
   }
  for(i=0;i<m;i++)
   for(j=0;j<n;j++)
   {
    ch=map[i][j].val;
    thisarea=0;
    visit(i,j);
    if(thisarea>area)
     area=thisarea;
   }
   count--;
   cout<<area<<endl;
    }
 return 0;
}