博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WustOJ 1575 Gingers and Mints(快速幂 + dfs )
阅读量:4450 次
发布时间:2019-06-07

本文共 2536 字,大约阅读时间需要 8 分钟。

1575: Gingers and Mints

Time Limit: 1 Sec  Memory Limit: 128 MB   64bit IO Format: %lld
Submitted: 24  Accepted: 13
[][][]

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

 
23 3YNYYNNNYY3 3YYYYYYYYY

 

Sample Output

41

 

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
#include
using namespace std;#define maxn 200char pic[maxn][maxn];int vis[maxn][maxn];int dx[4] = {-1,1,0,0};int dy[4] = {
0,0,-1,1};int n,m;void dfs(int x, int y){ for(int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = 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); } }}int pow_mod(int a,int n,int m){ if(n==0) return 1; int x=pow_mod(a,n/2,m); long long ans=(long long)x*x%m; if(n%2==1) ans=ans*a%m; return (int )ans;}int main(){ int t; scanf("%d", &t); while(t--) { scanf("%d%d", &n, &m); memset(vis, 0, sizeof vis); for(int i = 0; i < n; i++) scanf("%s", pic[i]); int cnt = 0; for(int i = 0; i < n; i++) for(int j = 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;}

 

转载于:https://www.cnblogs.com/ZP-Better/p/5405839.html

你可能感兴趣的文章
WUSTOJ 1298: 操作格子(Java)
查看>>
第一章整理
查看>>
POJ 2689 Prime Distance (素数筛选法,大区间筛选)
查看>>
HDU 4901 多校4 经典计数DP
查看>>
iOS通过dSYM文件分析crash
查看>>
使用行为树(Behavior Tree)实现游戏AI
查看>>
[转]解读Unity中的CG编写Shader系列二
查看>>
学生管理系统的优化过程
查看>>
魔都之行
查看>>
【OpenCV & CUDA】OpenCV和Cuda结合编程
查看>>
【译】索引列,列选择率和等式谓词
查看>>
activeMQ启动失败61616port被占用问题
查看>>
linux下oracle11G DG搭建(三):环绕备库搭建操作
查看>>
rsync配置文件的参数详解
查看>>
简单题
查看>>
MySQL(十)操纵表及全文本搜索
查看>>
begin again
查看>>
VS2008.Net下使用WPF开发Web应用程序小例
查看>>
数据库设计三大范式
查看>>
步步为营——算法初阶 1.算法概述
查看>>