【BFS】coj 1102 Region of Interest

来源:互联网 发布:大数据公司 编辑:程序博客网 时间:2024/05/20 23:30

Region of Interest

Time Limit: 1000ms
Memory Limit: 65536KB
64-bit integer IO format: lld     
Java class name:Main

在一幅遥感图像上,分布着N个湖泊(N<=10),小明想通过计算机知道,其中一个或多个湖泊的面积和,他的做法如下:1、在他想计算的湖泊上,每个湖泊各取一个特征点(在计算机中用像素表示)
2、在遥感图像上,把在所选点周围,属性和该点一样的其他像素点也选上,所有被选中的点的总和就是湖泊的面积。
现在,我们用一个二维字符数组来代替遥感图像(如样例所示),图像中,每个点的属性就是字符的值:湖泊用L表示,其他地物用S来表示,湖泊中,选取了的特征点用T表示,特征点也属于湖的一部分。
然后,请你计算所选中的湖泊面积(用点的和来表示)。

Input

第一行:遥感图像的行、列数m、n
其中m和n是不超过30的正整数
第二行:m行n列的二维字符数组,表示遥感图像,湖泊用L表示,其他地物用S来表示,湖泊中,选取了的特征点用T表示。
注意:需要计算的湖泊个数可以是一个,也可以是多个,而一个湖泊只放一个特征点

Output

所求的,湖泊总面积,一个整数

Sample Input

10 10SSSSSSSSSSSLLLSSSSSSSLTLSSSSSSSSSSSSSSSSLLLLLLSSSSLLLSSSSSTSSSSSSSSSSSSSSSLLLLLSSSSSSLLTLSSSSSSLLLLS

Sample Output

20///AC代码
#include <iostream>#include <set>#include <map>#include <stack>#include <cmath>#include <queue>#include <cstdio>#include <bitset>#include <string>#include <vector>#include <iomanip>#include <cstring>#include <algorithm>#include <functional>#define PI acos(-1)#define eps 1e-8#define inf 0x3f3f3f3f#define debug(x) cout<<"---"<<x<<"---"<<endltypedef long long ll;using namespace std;int m, n;char str[35][35];bool vis[35][35];struct node{    int x, y;};int dix[4] = {1, -1, 0, 0};int diy[4] = {0, 0, 1, -1};int cango(int x, int y){    return x >= 0 && x < m && y >= 0 && y < n && vis[x][y] == false;}int bfs(int x, int y){    queue<node> qu;    node gg;    gg.x = x;    gg.y = y;    qu.push(gg);    memset(vis, false, sizeof(vis));    vis[x][y] = true;    int sum = 0;    while (!qu.empty())    {        gg = qu.front();        qu.pop();        sum++;        for (int i = 0; i < 4; i++)        {            int _x = gg.x + dix[i];            int _y = gg.y + diy[i];            if (cango(_x, _y) && str[_x][_y] == 'L')            {                node hh;                hh.x = _x;                hh.y = _y;                qu.push(hh);                vis[_x][_y] = true;            }        }    }    return sum;}int main(){    while (cin >> m >> n)    {        int ans = 0;        for (int i = 0; i < m; i++)        {            cin >> str[i];        }        for (int i = 0; i < m; i++)        {            for (int j = 0; j < m; j++)            {                if (str[i][j] == 'T')                {                    ans += bfs(i, j);                }            }        }        cout << ans << endl;    }    return 0;}

 
原创粉丝点击