水流(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)的更多相关文章

  1. 水流(water)(BFS)(DFS)

    水流(water) 时间限制: 1 Sec  内存限制: 64 MB提交: 9  解决: 2[提交][状态][讨论版] 题目描述 全球气候变暖,小镇A面临水灾,于是你必须买一些泵把水抽走.泵的抽水能力 ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. 417 Pacific Atlantic Water Flow 太平洋大西洋水流

    详见:https://leetcode.com/problems/pacific-atlantic-water-flow/description/ C++: class Solution { publ ...

  6. 【JZOJ】1341. water(水流)

    题目大意 你必须买一些泵把水抽走.泵的抽水能力可以认为是无穷大,但你必须把泵放在合适的位置,小镇可以认为是N * M的矩阵.矩阵里的每个单元格都是一个‘a’- ‘z’小写字母,该小写字母表示该格子的高 ...

  7. [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 ...

  8. water 解题报告

    water 题目描述 有一块矩形土地被划分成\(n\times m\)个正方形小块.这些小块高低不平,每一小块都有自己的高度.水流可以由任意一块地流向周围四个方向的四块地中,但是不能直接流入对角相连的 ...

  9. 暑期集训20190727 水(water)

    [题目描述] 有一块矩形土地被划分成n×m个正方形小块.这些小块高低不平,每一小 块都有自己的高度.水流可以由任意一块地流向周围四个方向的四块地中,但 是不能直接流入对角相连的小块中. 一场大雨后,由 ...

随机推荐

  1. ios 修改程序显示名称

    当你创建一个project时,会要求你输入product name & company identifier,这两个property的值should和你在apple developer mem ...

  2. 兼容IE低版本

    1,IE6PNG透明的bug,只需要把png图另存为无杂边的png-8格式 2,在IE6用overflow:hidden清除浮动,要加上zoom:1 3,IE6下盒子的最小高度为20px 如果要小于2 ...

  3. passwd总结

    1.当前用户是root root用户修改密码 ,直接 passwd[不要输入当前用户密码] 如果修改其他用户密码,需要 passwd 用户名 如: passwd sc 短短的密码,如123也能通过,因 ...

  4. jQuery执行流程:

    实例1: 源码: <!DOCTYPE html> <html> <head> <script src="//ajax.googleapis.com/ ...

  5. docker installation on ubuntu

    Ubuntu Docker is supported on these Ubuntu operating systems: Ubuntu Xenial 16.04 (LTS) Ubuntu Trust ...

  6. iptables查看、添加、删除规则

    1.查看iptables -nvL –line-number -L 查看当前表的所有规则,默认查看的是filter表,如果要查看NAT表,可以加上-t NAT参数-n 不对ip地址进行反查,加上这个参 ...

  7. libevent linux安装

    wget http://monkey.org/~provos/libevent-1.4.13-stable.tar.gzwget http://downloads.sourceforge.net/le ...

  8. kafuka常用的shell命令

    kafka常用shell命令: ------------------------------------ 1.创建topic bin/kafka-topics.sh --create --zookee ...

  9. 2.10 工具使用 after effects(图形视频处理软件)

    ................... ..................... 暂无 教程素材网 http://img.yipinsucai.com/

  10. # 泰语字符串字符分割 --- UTF-8编码格式

    1.泰语编码格式 泰语用的编码格式是:ISO 8859-11,这个是Latin编码系列,是从"ISO-8859-1"发展过来的,采用的是8bit一个字,所以泰语中的英文字母或者数字 ...