水流(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个正方形小块.这些小块高低不平,每一小 块都有自己的高度.水流可以由任意一块地流向周围四个方向的四块地中,但 是不能直接流入对角相连的小块中. 一场大雨后,由 ...
随机推荐
- List<T> 求差集
List<, , , , , }; List<, , , , , }; List<int> c = b.Except(a).ToList(); foreach (int i i ...
- git 使用系列(二)---- 分支和合并
Branching and Merging The Git feature that really makes it stand apart from nearly every other SCM o ...
- calc()使用笔记
calc()可以给元素做运算, calc(expression) eg: .a { width: -webkit-calc(100% - 2px); width: -moz-calc(100% - 2 ...
- 转 当当网资深DBA:DB运维四大现代化的实现
位好,今天我的主题是 <DB运维的四个现代化> ,看标题就能明白,是关于DBA自动化运维平台的事情.http://dbaplus.cn/news-21-855-1.html 主要是分享下我 ...
- springmvc 关于controller的字符编码
在使用springMVC框架构建web应用,客户端常会请求字符串.整型.json等格式的数据,通常使用@ResponseBody注解使 controller回应相应的数据而不是去渲染某个页面.如果请求 ...
- java中json数据生成和解析(复杂对象演示)
1.json简单介绍 1.1 json是最流行和广泛通用的数据传输格式,简称JavaScript Object Notation,最早在JavaScript中使用. 1.2 举个例子,下面是一个jso ...
- Hibernate Session & Transaction详解
Hibernate Session & Transaction详解 HIbernate中的Session Session是JAVA应用程序和Hibernate进行交互时使用的主要接口,它也是持 ...
- Android上掌纹识别第一步:基于OpenCV的6种肤色分割 源码和效果图
Android上掌纹识别第一步:基于OpenCV的6种肤色分割 源码和效果图 分类: OpenCV图像处理2013-02-21 21:35 6459人阅读 评论(8) 收藏 举报 原文链接 ht ...
- iOS 6 Passbook 入门 1/2
http://www.raywenderlich.com/zh-hans/23066/ios-6-passbook-%E5%85%A5%E9%97%A8-12 iOS 6 Passbook 入门 1/ ...
- hdu 3345 War Chess
War Chess Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Sub ...