水流(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. permutation求全排列

    include <iostream> #include <string> using namespace std; void swap(char &c1, char & ...

  2. NSArray的排序问题

    1.关于NSArray的排序问题,首先想到的是不是冒泡排序,插入排序....... oc中的NSArray有一种更简单的方式: 例如: NSArray *stringArray=[NSArray ar ...

  3. Android Studio 如何使用jni

    在project视图下,main文件夹下,创建jniLibs文件夹,然后把so文件放入即可:

  4. isPostBack原理

    从 到输入用户名,点击提交按钮 这个过程就叫做postback(是两个不同的状态)     利用ispostback原理,实现是否第一次进入处理程序(上一个用用户名判断的不好,会导致在用户名空的情况下 ...

  5. lvs + keepalived + httpd 高可用集群(转)

    实验信息和拓扑:备注:Centos 6.5 selinux –disabled iptables off ServerName Ipaddress information LVSMaster 172. ...

  6. Oracle where 0=1 or 1=1

    本文转载自:http://www.cnblogs.com/junyuz/archive/2011/03/10/1979646.html sql where 1=1和 0=1 的作用   where 1 ...

  7. Java 反射实例

    实体类:Userpackage com.reflect.model; public class User{ private User(int id, String username, String p ...

  8. web-service客户端与服务器端的连接

    1 首先讲解下xfire XFire是新一代的Java Web服务引擎,XFire使得在JavaEE应用中发布Web服务变得轻而易举.和其他Web服务引擎相比,XFire的配置非常简单,可以非常容易地 ...

  9. Hibernate 系列教程7-双向一对一

    双向一对一 一对一主要用在 一个一方需要的信息比较少,比如注册的登录信息 另一个一方存储的信息比较多,比如注册之后用户填写的详细信息 实现方式常用的主要有2种: java模型都是一样,其中一个映射文件 ...

  10. popoverController(iPad)

    一.设置尺寸 提示:不建议,像下面这样吧popover的宽度和高度写死. 1 //1.新建一个内容控制器 2 YYMenuViewController *menuVc=[[YYMenuViewCont ...