WustOJ 1575 Gingers and Mints(快速幂 + dfs )
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
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
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
#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 )的更多相关文章
- HDU.1575 Tr A ( 矩阵快速幂)
HDU.1575 Tr A ( 矩阵快速幂) 点我挑战题目 题意分析 直接求矩阵A^K的结果,然后计算正对角线,即左上到右下对角线的和,结果模9973后输出即可. 由于此题矩阵直接给出的,题目比较裸. ...
- HDU - 1575——矩阵快速幂问题
HDU - 1575 题目: A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input数据的第一行是一个T,表示有T组数据. 每组数据的第一行有n( ...
- K. Random Numbers(Gym 101466K + 线段树 + dfs序 + 快速幂 + 唯一分解)
题目链接:http://codeforces.com/gym/101466/problem/K 题目: 题意: 给你一棵有n个节点的树,根节点始终为0,有两种操作: 1.RAND:查询以u为根节点的子 ...
- HDU 1575 Tr A 【矩阵经典2 矩阵快速幂入门】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1575 Tr A Time Limit: 1000/1000 MS (Java/Others) Me ...
- hdu 1575 Tr A(矩阵快速幂)
今天做的第二道矩阵快速幂题,因为是初次接触,各种奇葩错误整整调试了一下午.废话不说,入正题.该题应该属于矩阵快速幂的裸题了吧,知道快速幂原理(二进制迭代法,非递归版)后,剩下的只是处理矩阵乘法的功夫了 ...
- hdu 1575 Tr A(矩阵快速幂,简单)
题目 和 LightOj 1096 - nth Term 类似的线构造一个符合题意的矩阵乘法模版,然后套快速幂的模版,具体的构造矩阵我就不作图了,看着代码也能理解吧 #include<stdi ...
- HDU 1575(裸矩阵快速幂)
emmmmm..就是矩阵快速幂,直接附代码: #include <cstdio> using namespace std; ; ; struct Matrix { int m[maxn][ ...
- hdu 1575 求一个矩阵的k次幂 再求迹 (矩阵快速幂模板题)
Problem DescriptionA为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input数据的第一行是一个T,表示有T组数据.每组数据的第一行有 ...
- 简单矩阵快速幂(HDU Tr A 1575)
题目中所给的方阵就是一个矩阵,而就是只要将题目所给矩阵不断进行相乘即可,本题中我采用的是直接重载运算符*,使矩阵每一个都进行运算,可以简化为只对对角线上的元素进行运算.最后所得结果就只需将最终的矩阵上 ...
随机推荐
- Oracle 表分析
ANALYZE TABLE SeikyuTbl COMPUTE Statistics FOR TABLE FOR ALL COLUMNS FOR ALL INDEXES ; 一.优化器的优化方式 Or ...
- (转)苹果iOS开发者账号过期临时解决方法
苹果iOS开发者账号过期临时解决办法 苹果iOS开发者账号一年的费用是99美金,作者最近由于各种原因,导致renew没能在账号过期之前支付好,所以在账号过期等待renew的期间,试了试一些非正常手段, ...
- struts2.1.*中再实现了一个servlet的方法
学习Struts2也有一段时间了,今天用Servlet写了一个验证码,然后搬到Struts2中,惊奇地发现Servlet无法访问,出现404错误!后来折腾了半天,终于找出原因了.这也算我学习中的一个重 ...
- Android Fragment 详解(一)
Android从3.0开始引入fragment,主要是为了支持更动态更灵活的界面设计,比如在平板上的应用.平板机上拥有比手机更大的屏幕空间来组合和交互界面组件们.Fragment使你在做那样的设计时, ...
- DevExpress GridView.CustomSummaryCalculate 实现自定义Group Summary
--首发于博客园, 转载请保留链接 博客原文 DevExpress Documentation官方地址:GridView.CustomSummaryCalculate Event 1. 概要 界面上 ...
- js中时间戳与日期转换-js日期操作
常用的一些日期操作. 用js获取一个时间戳. <script type="text/javascript"> var date = new Date();//当前时间 ...
- 网页CSS1
样式的属性 1,背景与前景 background-color: //背景的颜色 background-image:url //背景图片 background-attachment:fixed; //背 ...
- Could not find the Visual SourceSafe Internet Web Service connection information
Visual SourceSafe Internet---------------------------Could not find the Visual SourceSafe Internet W ...
- 关于.net根目录路径的问题
今天做了一个项目,用了url重写,但是在本地目录是localhost/BK/index.aspx,而其他目录也必须带bk,不带的话就找不到页面,(iis里正常) 应该主目录去掉bk,设置方法,右击网站 ...
- SVN 密码破解,svn密码本地找回 忘记密码
svn 密码被保存在本地文件中 C:\Users\[your computer name]\AppData\Roaming\Subversion\auth\svn.simple 文件下. 加密保存 到 ...