Wustoj 1575 gingers and mints (Fast Power + Dfs)

Source: Internet
Author: User

1575:gingers and Mints time limit:1 Sec Memory limit:128 MB 64bit IO Format:%lld
Submitted:24 accepted:13
[Submit] [Status] [Web Board] Descriptionfcbruce owns a farmland, the farmland has n * m grids. Some of the grids is stones, and rich soil is the rest. Fcbruce wanna plant gingers and mints on he farmland, and each plant could occupy area as large as possible. If the grids share the same edge, they can be connected to the same area. Fcbruce is a odd boy, he wanna plant gingers, which odd numbers of areas was gingers, and the rest area, mints. Now he want to know the number of the ways he could plant.

Inputthe first line of input was an integer t (T < a), means there is t test cases. For each test case, the first line has a integers n, m (0 < N, m < 100). For next n lines, each of the line have m characters, ' N ' for Stone, ' Y ' for rich soil that's excellent for planting.

Output

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

Sample Input
3YYYYYYYYY 3ynyynnnyy3

Sample Output
41

HINT

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


Source

Wuhan University of Science and Technology, the second mobile Internet application Design Competition (Class A) and the Central Central Program Design Competition Professional Group Network game

Author

Fcbruce

Idea: 1. First use DFS to find the number of unicom blocks. 2. Again based on mathematical knowledge c{n,1} + c{n,3} + c{n,5} + ... = 2^ (n-1) is calculated with a fast power.
#include <cstdio>#include<cstring>using namespacestd;#defineMAXN 200CharPIC[MAXN][MAXN];intVIS[MAXN][MAXN];intdx[4] = {-1,1,0,0};intdy[4] = {0,0,-1,1};intn,m;voidDfsintXinty) {     for(inti =0; I <4; i++)    {        intNX = x +Dx[i]; intNY = y +Dy[i]; if(!vis[nx][ny] && pic[nx][ny] = ='Y'&& NX >=0&& NX < n && NY >=0&& NY <m) {Vis[nx][ny]=1;        DFS (NX,NY); }    }}intPow_mod (intAintNintm) {    if(n==0)    return  1; intX=pow_mod (a,n/2, M); Long LongAns= (Long Long) x*x%m; if(n%2==1) ans=ans*a%m; return(int) ans;intMain () {intT; scanf ("%d", &t);  while(t--) {scanf ("%d%d", &n, &m); memset (Vis,0,sizeofvis);  for(inti =0; I < n; i++) scanf ("%s", Pic[i]); intCNT =0;  for(inti =0; I < n; i++)             for(intj =0; J < M; J + +)            {                if(!vis[i][j] && pic[i][j] = ='Y') {Vis[i][j]=1; CNT++;                DFS (I,J); }} printf ("%d\n", Pow_mod (2, cnt-1,1000000007)); }    return 0;}

Wustoj 1575 gingers and mints (Fast Power + Dfs)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.