1575: Gingers and Mints

Time Limit: 1 Sec  Memory Limit: 128 MB   64bit IO Format: %lld
Submitted: 24  Accepted: 13
[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.

Source

Author

fcbruce

思路:
1.先用DFS求出联通块个数。
2.再根据数学知识 C{n,1} + C{n,3} + C{n,5} + ... = 2^(n-1) 用快速幂求出。
#include<cstdio>
#include<cstring>
using namespace std; #define maxn 200
char pic[maxn][maxn];
int vis[maxn][maxn];
int dx[] = {-,,,};
int dy[] = {,,-,};
int n,m; void dfs(int x, int y)
{
for(int i = ; i < ; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if(!vis[nx][ny] && pic[nx][ny] == 'Y' && nx >= && nx < n && ny >= && ny < m)
{
vis[nx][ny] = ;
dfs(nx,ny);
}
}
}
int pow_mod(int a,int n,int m)
{ if(n==)
return ;
int x=pow_mod(a,n/,m);
long long ans=(long long)x*x%m;
if(n%==)
ans=ans*a%m;
return (int )ans;
} int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &n, &m);
memset(vis, , sizeof vis);
for(int i = ; i < n; i++)
scanf("%s", pic[i]);
int cnt = ;
for(int i = ; i < n; i++)
for(int j = ; j < m; j++)
{
if(!vis[i][j] && pic[i][j] == 'Y')
{
vis[i][j] = ;
cnt++;
dfs(i,j);
}
}
printf("%d\n",pow_mod(,cnt-,));
}
return ;
}

WustOJ 1575 Gingers and Mints(快速幂 + dfs )的更多相关文章

  1. HDU.1575 Tr A ( 矩阵快速幂)

    HDU.1575 Tr A ( 矩阵快速幂) 点我挑战题目 题意分析 直接求矩阵A^K的结果,然后计算正对角线,即左上到右下对角线的和,结果模9973后输出即可. 由于此题矩阵直接给出的,题目比较裸. ...

  2. HDU - 1575——矩阵快速幂问题

    HDU - 1575 题目: A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973.  Input数据的第一行是一个T,表示有T组数据. 每组数据的第一行有n( ...

  3. K. Random Numbers(Gym 101466K + 线段树 + dfs序 + 快速幂 + 唯一分解)

    题目链接:http://codeforces.com/gym/101466/problem/K 题目: 题意: 给你一棵有n个节点的树,根节点始终为0,有两种操作: 1.RAND:查询以u为根节点的子 ...

  4. HDU 1575 Tr A 【矩阵经典2 矩阵快速幂入门】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1575 Tr A Time Limit: 1000/1000 MS (Java/Others)    Me ...

  5. hdu 1575 Tr A(矩阵快速幂)

    今天做的第二道矩阵快速幂题,因为是初次接触,各种奇葩错误整整调试了一下午.废话不说,入正题.该题应该属于矩阵快速幂的裸题了吧,知道快速幂原理(二进制迭代法,非递归版)后,剩下的只是处理矩阵乘法的功夫了 ...

  6. hdu 1575 Tr A(矩阵快速幂,简单)

    题目 和 LightOj 1096 - nth Term  类似的线构造一个符合题意的矩阵乘法模版,然后套快速幂的模版,具体的构造矩阵我就不作图了,看着代码也能理解吧 #include<stdi ...

  7. HDU 1575(裸矩阵快速幂)

    emmmmm..就是矩阵快速幂,直接附代码: #include <cstdio> using namespace std; ; ; struct Matrix { int m[maxn][ ...

  8. hdu 1575 求一个矩阵的k次幂 再求迹 (矩阵快速幂模板题)

    Problem DescriptionA为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input数据的第一行是一个T,表示有T组数据.每组数据的第一行有 ...

  9. 简单矩阵快速幂(HDU Tr A 1575)

    题目中所给的方阵就是一个矩阵,而就是只要将题目所给矩阵不断进行相乘即可,本题中我采用的是直接重载运算符*,使矩阵每一个都进行运算,可以简化为只对对角线上的元素进行运算.最后所得结果就只需将最终的矩阵上 ...

随机推荐

  1. Java使用poi包读取Excel文档

    项目需要解析Excel文档获取数据,就在网上找了一些资料,结合自己这次使用,写下心得: 1.maven项目需加入如下依赖: <dependency> <groupId>org. ...

  2. 面向对象CSS (OOCSS)

    新版 OOCSS 请关注 http://www.oocss.cc/ 时下流行面向对象,那么有没有可能把样式表也面向对象一下呢,将现在的CSS(Cascading Style Sheets层叠样式表)进 ...

  3. EditText设置可以编辑和不可编辑状态

    1.首先想到在xml中设置android:editable="false",但是如果想在代码中动态设置可编辑状态,没有找到对应的函数 2.然后尝试使用editText.setFoc ...

  4. C++ 推断进程是否存在

    [cpp] view plaincopyprint? #include <windows.h> #include "psapi.h" #include"std ...

  5. 好看的Select下拉框是如何制造的

    现在在大多数网站中都能看到很华丽的Select下拉框,他们是如何实现的呢?使用默认select肯定是不好实现,我们可以使用div+js去模拟出来select的功能,并且又能很简单的去美化它. CSS代 ...

  6. js中this的指向

    在js中this的指向对于新手来说一定是个难题,但是如果你真正理解了的话,也就没什么问题啦,下面就来讲讲this吧. JS中,this的值取决于调用的模式(调用对象),而JS中共有4种调用模式: 1. ...

  7. 3.2:pandas数据的导入与导出【CSV,JSON】

    一:CSV数据 一]:导入数据 1)从CSV文件读入数据:pd.read_csv("文件名"),默认以逗号为分隔符 D:\data\ex1.csv文件内容:             ...

  8. 将GridView中的数据导出到Excel代码与注意事项

    //gv:需要导出数据的GridView,filename:导出excel文件名 public void ExportToExcel(GridView gv, string filename) { s ...

  9. windows internal读书笔记

    程序:指一个静态的指令序列,而进程则是一个容器,其中包含了当执行一个程序特定实例时所用到的各种资源.

  10. [c#]asp.net开发微信公众平台(6)阶段总结、服务搭建、接入

    经过前5篇,跟着一步步来的话,任何人都能搭建好一个能处理各种微信消息的框架了,总结一下最容易忽略的问题: 1.文本消息中可以使用换行符\n    : 2.微信发来的消息中带的那个长整型的时间,我们完全 ...