水流(water)

时间限制: 1 Sec  内存限制: 64 MB
提交: 9  解决: 2
[提交][状态][讨论版]

题目描述

全球气候变暖,小镇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

【分析】我是用BFS写的,从z遍历到a,我们实验室有位大神用DFS过了一发,不过时间比我长,下面是两段AC代码。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define inf 0x3f3f3f3f
#define mod 1000000007
typedef long long ll;
using namespace std;
const int N=;
int n,dp[N],len;
string w[N];
int vis[N][N];
int m,t;
int d[][]={,,,,-,,,-};
set<int>p;
int minn=inf;
struct man {
int x,y;
int st;
};
queue<man>q;
void bfs(int aa,int bb,char ch) {
man s;s.x=aa;s.y=bb;s.st=ch;q.push(s);
vis[aa][bb]=;
while(!q.empty())
{
man t=q.front();q.pop();
for(int i=;i<;i++)
{
int xx=t.x+d[i][],yy=t.y+d[i][];
if(xx>=&&xx<n&&yy>=&&yy<m&&w[xx][yy]>=t.st&&vis[xx][yy]==)
{
man k;k.x=xx;k.y=yy;k.st=w[xx][yy];vis[xx][yy]=;q.push(k);
}
}
}
} int main() {
cin>>t;
while(t--) {
int ans=;
while(!q.empty())q.pop();
memset(vis,,sizeof(vis));
cin>>n>>m;
for(int i=; i<n; i++)cin>>w[i];
for(char ch='a'; ch<='z'; ch++) {
for(int i=; i<n; i++) {
for(int j=; j<m; j++) {
if(w[i][j]==ch&&vis[i][j]==)
{
ans++;
bfs(i,j,ch);
}
}
}
}
cout<<ans<<endl;
}
return ;
}

BFS

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<vector>
#include <climits>
#include <map>
using namespace std;
const int MAXN = ;
int N, M;
char maze[MAXN][MAXN];
char s[MAXN][MAXN];
int xi[] = {, , -, };
int yi[] = {, -, , };
void dfs(int x, int y) {
s[x][y] = ;
for(int i = ; i < ; i++) {
int nx = x + xi[i];
int ny = y + yi[i];
//printf("%d %d %c\n", nx, ny,maze[nx][ny]);
if(nx>= && nx < N && ny >= && ny < M && s[nx][ny] == && maze[nx][ny] >= maze[x][y]) {
dfs(nx, ny);
}
}
}
int main() {
int t = ;
scanf("%d", &t);
while(t--) {
scanf("%d%d", &N, &M);
for(int i = ; i < N; i++) {
scanf("%s", maze[i]);
}
memset(s, , sizeof(s));
int cnt = ;
for(char c = 'a'; c <= 'z'; c++) {
for(int i = ; i < N; i++) {
for(int j = ; j < M; j++) {
if(s[i][j] == && maze[i][j] == c){
dfs(i, j);
cnt++;
}
}
}
}
printf("%d\n", cnt); } return ;
}

DFS

												

水流(water)(BFS)(DFS)的更多相关文章

  1. 水流(water)

    水流(water) 题目描述 全球气候变暖,小镇A面临水灾,于是你必须买一些泵把水抽走.泵的抽水能力可以认为是无穷大,但你必须把泵放在合适的位置,从而能使所有的水能流到泵里.小镇可以认为是N×M的矩阵 ...

  2. POJ 2227 The Wedding Juicer (优先级队列+bfs+dfs)

    思路描述来自:http://hi.baidu.com/perfectcai_/item/701f2efa460cedcb0dd1c820也可以参考黑书P89的积水. 题意:Farmer John有一个 ...

  3. 邻结矩阵的建立和 BFS,DFS;;

    邻结矩阵比较简单,, 它的BFS,DFS, 两种遍历也比较简单,一个用队列, 一个用数组即可!!!但是邻接矩阵极其浪费空间,尤其是当它是一个稀疏矩阵的时候!!!-------------------- ...

  4. Collect More Jewels(hdu1044)(BFS+DFS)

    Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  5. Cleaning Robot (bfs+dfs)

    Cleaning Robot (bfs+dfs) Here, we want to solve path planning for a mobile robot cleaning a rectangu ...

  6. LeetCode:BFS/DFS

    BFS/DFS 在树专题和回溯算法中其实已经涉及到了BFS和DFS算法,这里单独提出再进一步学习一下 BFS 广度优先遍历 Breadth-First-Search 这部分的内容也主要是学习了labu ...

  7. (BFS/DFS) leetcode 200. Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  8. 搜索入门_简单搜索bfs dfs大杂烩

    dfs题大杂烩 棋盘问题  POJ - 1321 和经典的八皇后问题一样.  给你一个棋盘,只有#区域可以放棋子,同时同一行和同一列只能有一个棋子. 问你放k个棋子有多少种方案. 很明显,这是搜索题. ...

  9. POJ 3414 Pots (BFS/DFS)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7783   Accepted: 3261   Special Ju ...

随机推荐

  1. 洛谷 P1251 餐巾计划问题

    题目链接 最小费用最大流. 每天拆成两个点,早上和晚上: 晚上可以获得\(r_i\)条脏毛巾,从源点连一条容量为\(r_i\),费用为0的边. 早上要供应\(r_i\)条毛巾,连向汇点一条容量为\(r ...

  2. [洛谷P2568]GCD

    题目大意:给你$n(1\leqslant n\leqslant 10^7)$,求$\displaystyle\sum\limits_{x=1}^n\displaystyle\sum\limits_{y ...

  3. [WC2007]剪刀石头布——费用流

    比较有思维含量的一道题 题意:给混合完全图定向(定向为竞赛图)使得有最多的三元环 三元环条件要求比较高,还不容易分开处理. 正难则反 考虑,什么情况下,三元组不是三元环 一定是一个点有2个入度,一个点 ...

  4. Codeforces Round #348 (VK Cup 2016 Round 2, Div. 2 Edition) B

    B. Little Artem and Grasshopper time limit per test 2 seconds memory limit per test 256 megabytes in ...

  5. B. Minimum Ternary String (这个B有点狠)

    B. Minimum Ternary String time limit per test 1 second memory limit per test 256 megabytes input sta ...

  6. AWS nat monitor and route switch script

    This script will monitor another NAT instance and take over its routes if communication with the oth ...

  7. SpringMVC学习 -- REST

    REST:表现层状态转化. REST 是目前最流行的一种互联网软件架构.他结构清晰.符合标准.易于理解.扩展方便 , 所以正得到越来越多网站的采用. 状态转化:浏览器 form 表单只支持 GET 和 ...

  8. canvas知识01

    本文转自:http://www.cnblogs.com/jsdarkhorse/archive/2012/06/29/2568451.html 更多参考:http://www.cnblogs.com/ ...

  9. 转:Mybatis系列之集合映射

    转:Mybatis系列之集合映射 上篇文章我们讲了关联映射,实现了销售与登录用户之间的关联.本文我们接着来讲一讲集合映射,实现销售与客户的多对多关系. 实现销售与客户多对多关系 本文中仍延用<M ...

  10. HDFS 的Trash回收站

    1)在core-site.xml文件中添加这个配置 在每个节点(不仅仅是主节点)上添加配置 core-site.xml,增加如下内容 <property> <name>fs.t ...