武汉科技大学网络赛1575: Gingers and Mints

1575: Gingers and Mints

Time Limit: 1 Sec   Memory Limit: 128 MB   64bit IO Format: %lld
Submitted: 18   Accepted: 12
[ Submit][ Status][ Web Board]

Description

fcbruce owns a farmland, the farmland has n * m grids. Some of the grids are stones, rich soil is the rest. fcbruce wanna plant gingers and mints on his farmland, and each plant could occupy area as large as possible. If two grids share the same edge, they can be connected to the same area. fcbruce is an odd boy, he wanna plant gingers, which odd numbers of areas are gingers, and the rest area, mints. Now he want to know the number of the ways he could plant.

Input

The first line of input is an integer T (T < 100), means there are T test cases.
For each test case, the first line has two integers n, m (0 < n, m < 100).
For next n lines, each line has m characters, 'N' for stone, 'Y' for rich soil that is excellent for planting.

Output

For each test case, print the answer mod 1000000007 in one line. 

Sample Input 

2
3 3
YNY
YNN
NYY
3 3
YYY
YYY
YYY

Sample Output

4
1

HINT

For the first test case, there are 3 areas for planting. We marked them as A, B and C. fcbruce can plant gingers on A, B, C or ABC. So there are 4 ways to plant gingers and mints.


#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
const int maxn = 105;
const int MOD = 1000000007;
int m,n;
char maze[maxn][maxn];
int vis[maxn][maxn];
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};

struct Node
{
    int x,y;
    Node() {}
    Node(int a,int b):x(a),y(b){}
}pre;

void bfs(int i,int j)
{
    queue <Node> que;
    que.push(Node(i,j));
    vis[i][j] = 1;
    while(!que.empty())
    {
        pre = que.front();
        que.pop();
        for(int i = 0; i < 4; i++)
        {
            int xx = pre.x + dir[i][0];
            int yy = pre.y + dir[i][1];
            if(xx < 0 || xx >= n || yy < 0 || yy >= m)
                continue;
            if(maze[xx][yy] == 'Y' && !vis[xx][yy])
            {
                vis[xx][yy] = 1;
                que.push(Node(xx,yy));
            }
        }
    }
}

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d %d", &n,&m);
        for(int i = 0; i < n; i++)
            scanf("%s", maze[i]);
        memset(vis, 0, sizeof(vis));
        int ans = 0;
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
                if(maze[i][j] == 'Y' && !vis[i][j])
                {
                    ans++;
                    bfs(i,j);
                }
        ans--;
        int sum = 1;
        while(ans--)
            sum = (sum*2)%MOD;
        printf("%d\n", sum);
    }
    return 0;
}

你可能感兴趣的:(武汉科技大学网络赛1575: Gingers and Mints)