水流(water)
水流(water)
题目描述
全球气候变暖,小镇A面临水灾,于是你必须买一些泵把水抽走。泵的抽水能力可以认为是无穷大,但你必须把泵放在合适的位置,从而能使所有的水能流到泵里。小镇可以认为是N×M的矩阵,矩阵里的每个单元格都是一个a~z小写字母,该小写字母表示该格子的高度,字母大的表示该单元格比较高,反之表示该格子高度比较低。当前单元格的水可以流到上、下、左、右四个格子,但必须满足这些格子的高度是小于或者等于当前格子的高度。现在,给你一些NXM的矩阵,你至少要买多少个泵,才能把所有格子的水都抽走?
输入
多组测试数据。
第1行:K,表示有K组测试数据,1≤K≤5。
接下来有K组测试数据,每组测试数据格式如下:
第1行:两个正整数,N,M。1≤N,M≤50,表示小镇的大小。
接下来有N行,每行有M个小写字母,表示小镇的地图。
输出
共K行,每行对应一组数据。至少要买多少个泵,才能把所有格子的水都抽走。
样例输入
2
5 5
ccccc
cbbbc
cbabc
cbbbc
ccccc
4 9
cbabcbabc
cbabcbabc
cbabcbabc
cbabcbabc
样例输出
1
2
分析:枚举当前最小字母dfs即可;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <ext/rope>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define vi vector<int>
#define pii pair<int,int>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=1e4+;
const int dis[][]= {{,},{-,},{,-},{,}};
using namespace std;
using namespace __gnu_cxx;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,cnt,vis[][];
pair<int,pii >a[maxn];
char p[][];
void dfs(int x,int y)
{
vis[x][y]=;
for(int i=; i<; i++)
{
int l=x+dis[i][],r=y+dis[i][];
if(l>=&&l<n&&r>=&&r<m&&!vis[l][r]&&p[l][r]>=p[x][y])
dfs(l,r);
}
}
int main()
{
int i,j,k,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
cnt=;
j=;
memset(vis,,sizeof(vis));
rep(i,,n-)
scanf("%s",p[i]);
rep(i,,n-)rep(k,,m-)
a[j++]=mp(p[i][k],mp(i,k));
sort(a,a+j);
rep(i,,j-)if(!vis[a[i].se.fi][a[i].se.se])
{
++cnt,dfs(a[i].se.fi,a[i].se.se);
}
printf("%d\n",cnt);
}
//system ("pause");
return ;
}
水流(water)的更多相关文章
- 水流(water)(BFS)(DFS)
水流(water) 时间限制: 1 Sec 内存限制: 64 MB提交: 9 解决: 2[提交][状态][讨论版] 题目描述 全球气候变暖,小镇A面临水灾,于是你必须买一些泵把水抽走.泵的抽水能力 ...
- [Swift]LeetCode417. 太平洋大西洋水流问题 | Pacific Atlantic Water Flow
Given an m x n matrix of non-negative integers representing the height of each unit cell in a contin ...
- [LeetCode] Pacific Atlantic Water Flow 太平洋大西洋水流
Given an m x n matrix of non-negative integers representing the height of each unit cell in a contin ...
- [LeetCode] 417. Pacific Atlantic Water Flow 太平洋大西洋水流
Given an m x n matrix of non-negative integers representing the height of each unit cell in a contin ...
- 417 Pacific Atlantic Water Flow 太平洋大西洋水流
详见:https://leetcode.com/problems/pacific-atlantic-water-flow/description/ C++: class Solution { publ ...
- 【JZOJ】1341. water(水流)
题目大意 你必须买一些泵把水抽走.泵的抽水能力可以认为是无穷大,但你必须把泵放在合适的位置,小镇可以认为是N * M的矩阵.矩阵里的每个单元格都是一个‘a’- ‘z’小写字母,该小写字母表示该格子的高 ...
- [LeetCode] Swim in Rising Water 在上升的水中游泳
On an N x N grid, each square grid[i][j] represents the elevation at that point (i,j). Now rain star ...
- water 解题报告
water 题目描述 有一块矩形土地被划分成\(n\times m\)个正方形小块.这些小块高低不平,每一小块都有自己的高度.水流可以由任意一块地流向周围四个方向的四块地中,但是不能直接流入对角相连的 ...
- 暑期集训20190727 水(water)
[题目描述] 有一块矩形土地被划分成n×m个正方形小块.这些小块高低不平,每一小 块都有自己的高度.水流可以由任意一块地流向周围四个方向的四块地中,但 是不能直接流入对角相连的小块中. 一场大雨后,由 ...
随机推荐
- read file in a single line
String text= new Scanner(File('input.txt')).useDelimiter("\\A").next(); java version!
- dfs Codeforces Round #356 (Div. 2) D
http://codeforces.com/contest/680/problem/D 题目大意:给你一个大小为X的空间(X<=m),在该空间内,我们要尽量的放一个体积为a*a*a的立方体,且每 ...
- wpf无法加载程序集的元数据 可能已从web下载
wpf无法加载程序集的元数据 可能已从web下载 别人写的wpf程序,源代码放到自己电脑上,设计器加载出现问题:无法加载程序集“XXX”的元数据. 解除锁定步骤:找到你的dll->右键属 ...
- 查找mysql数据库中所有包含特定名字的字段所在的表
整个数据库查找 placement 字段: select * from INFORMATION_SCHEMA.columns where COLUMN_NAME Like '%placement%'; ...
- Python Django Learning Notes..
The first time I came across django was last month.. Since then I was considering it as the better c ...
- Android简单逐帧动画Frame的实现(三)
android之动画(三)通过AnimationDrawable控制逐帧动画 android与逐帧动画: 效果图: 当我们点击按钮时,该图片会不停的旋转,当再次点击按钮时,会停止在当前的状态. ...
- lucene的两种分页操作
基于lucene的分页有两种: lucene3.5之前分页提供的方式为再查询方式(每次查询全部记录,然后取其中部分记录,这种方式用的最多),lucene官方的解释:由于我们的速度足够快.处理海量数据时 ...
- Android开发 R cannot be resolved to a variable问题的分析
R文件是系统自动生成的,如果没出现的话,你的XML文件是不是有错误?是否之前修改过res文件夹下面.xml文件 R文件没有生成的情况有几种: 1.项目没有自动编译:这种时候只需要简单的编译一下工程就会 ...
- Chapter 1 First Sight——10
Instead, I was ivory-skinned, without even the excuse of blue eyes or red hair, despite the constant ...
- 移动WEB开发资源
很多移动开发的资源 http://www.cnblogs.com/PeunZhang/p/3407453.html