1501 Country in a map

本文介绍了一种算法,用于解决给定一张矩形大陆地图(由不同颜色表示的国家构成),如何找出并计算出最大国家(连续同色区域)的面积。输入包括地图的宽度和高度以及地图本身,输出则是最大国家的面积。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值