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)
题目中所给的方阵就是一个矩阵,而就是只要将题目所给矩阵不断进行相乘即可,本题中我采用的是直接重载运算符*,使矩阵每一个都进行运算,可以简化为只对对角线上的元素进行运算.最后所得结果就只需将最终的矩阵上 ...
随机推荐
- [C++]memcpy 小记
#include <stdio.h> #include <stdlib.h> #include <string.h> int main () { char a = ...
- Wamp集成环境配置多站点
一.打开apache配置文件httpd.conf 二.修改httpd.conf配置文件 1.在配置文件httpd.conf中搜索 conf/extra/httpd-vhosts.conf,然后将该行代 ...
- firefox关于about:config的常用配置
about:config是火狐的设置页面,火狐提供了不少高级设置选项在这里以便让你可以更加详细地控制火狐的运行方式.(官方不推荐用户手工修改about:config的设置.所以,如果你对于你想修改的内 ...
- BNU10792:沙漠旅行者
有个旅行者计划横穿沙漠,沙漠中水资源很匮乏.旅行者需要依靠补给站的支持,才能横穿整个沙漠.假设所有的补给站都在一条直线上,而且旅行者一定沿着这条直线走.起点在1号补给站,终点在第N号补给站,起点和终点 ...
- Foreach语法
先看例子: Random rand = new Random(47); float f[] = new float[10]; for(int i = 0; i < 10; i++){ f[i] ...
- hdu 1301
最小生成树模板题 简单的prim算法 AC代码: #include <iostream> #include <stdio.h> #define INF 9999999 usin ...
- .IIS7如何设置301重定向详解
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.we ...
- 让 asp.net mvc 支持 带有+ _ 等特殊字符的路由
最近配置微信 业务域名 时,需要在服务器的根目录中上传一个文本文件,而这个文本文件需要放这样的目录中: 于在就在 服务器目录中创建了对应的文件夹,并将kuPv.txt上传,但是访问时,却怎么也访问不到 ...
- c - 将十进制转换为字符串.
递归实现: /* 输入:十进制整数. 输出:字符串. */ void conv(int decimal) { != ) conv(); putchar( + '); }
- C#字符串的比较
Console.WriteLine("输入字符1"); string n1 = Console.ReadLine(); Console.WriteLine("输入字符2& ...