题目大意 你必须买一些泵把水抽走。泵的抽水能力可以认为是无穷大,但你必须把泵放在合适的位置,小镇可以认为是N * M的矩阵。矩阵里的每个单元格都是一个‘a’- ‘z’小写字母,该小写字母表示该格子的高度,字母大的表示该单元格比较高,反之,表示该格子高度比较低。当前单元格的水可以流到上、下、左、右四个格子,一共要多少个泵?

这道题目可以用搜索做,为什么呢?

我们可以把小镇当做一个地图来看*mn**的地图,我知道每一个格子的深度,我要找最深的那一个,只能用上下左右来进行寻找

我们就可以转化成,在一张n*m(个格子组成的)大小的地图中,找到最大的那一个格子。

于是我们便可以想到用dfs,但并不完全是dfs

我们可以用一个数组来保存状态,s[i][j],如果这一个格子没有被标记过(也就是没有走过),并且这个格子在我正张地图的范围之内,便可以执行我们的dfs函数 最后输出ans

在dfs函数中我们先用s[i][j]来保存当前状态, 之后再进行搜索,我们有上下左右四个方向进行移动,循环来保存新的x,y;之后再进行判断新的x与y是否再范围之内,我们的s数组是否标记过,再是判断当前位置是否离开了原位(很重要必须得离开原位),之后再将我们得到得新的x与y,进行调用,再做标记(记得要清零,标记放循环前)

其实这dfs很像递归但又像dfs。。。。

这样这道题就完成了;

上代码吧

 include<iostream>
include<cstdio>
include<cmath>
include<algorithm>
include<cstring>
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]; if(nx>=&&nx<N&& ny>=&&ny<M&&s[nx][ny]==&&maze[nx[ny]>=maze[x][y]) {
dfs(nx,ny); } }
}//一个很简单的搜索 int main() { int t=; cin>>t; while(t--) { cin>>N>>M; for(int i=;i<N;i++) { cin>>maze[i]; } memset(s,,sizeof(s)); int ans=; 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); ans++; } } } } cout<<ans<<endl;
return ;
}

【JZOJ】1341. water(水流)的更多相关文章

  1. 2019中山纪念中学夏令营-Day2[JZOJ]

    博客的开始,先聊聊代码实现: 每次比赛以后,要有归纳错误的习惯. 错误小结: 1.读入:scanf(“%c”)会读入回车和空格,但cin不会. 2.对于二维数组的输入,不能把m,n搞混了,会引起严重的 ...

  2. 水流(water)

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

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

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

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

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

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

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

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

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

  9. water 解题报告

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

随机推荐

  1. WAF的部署方式——有直路部署和旁路部署

    随着电子商务.网上银行.电子政务的盛行,WEB服务器承载的业务价值越来越高,WEB服务器所面临的安全威胁也随之增大,因此,针对WEB应用层的防御成为必然趋势,WAF(WebApplicationFir ...

  2. Rust中的泛型

    go没有的,rust有呢~~ fn largest<T: PartialOrd + Copy>(list: &[T]) -> T { let mut largest = li ...

  3. 201871010131-张兴盼《面向对象程序设计(java)》第十三周学习总结

    项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...

  4. 代码审计-数组返回NULL绕过

    <?php $flag = "flag"; if (isset ($_GET['password'])) { if (ereg ("^[a-zA-Z0-9]+$&q ...

  5. python-新建文件夹

    import tensorflow as tf import os categories = ['folder1', 'folder2'] for folderName in categories: ...

  6. window.devicePixelRatio ,px,rem

    window属性:devicePixelRatio 设备像素比 https://www.w3cschool.cn/fetch_api/fetch_api-atvq2nma.html devicePix ...

  7. onchange onpropertychange 和 oninput 事件的区别

    onchange 事件在内容改变(两次内容有可能还是相等的)且失去焦点时触发. onpropertychange 事件却是实时触发,即每增加或删除一个字符就会触发,通过 js 改变也会触发该事件,但是 ...

  8. [LeetCode] 925. Long Pressed Name 长按键入的名字

    Your friend is typing his name into a keyboard.  Sometimes, when typing a character c, the key might ...

  9. [LeetCode] 843. Guess the Word 猜单词

    This problem is an interactive problem new to the LeetCode platform. We are given a word list of uni ...

  10. [LeetCode] 34. Find First and Last Position of Element in Sorted Array 在有序数组中查找元素的第一个和最后一个位置

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...