水流(water)(BFS)(DFS)
水流(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)的更多相关文章
- 水流(water)
水流(water) 题目描述 全球气候变暖,小镇A面临水灾,于是你必须买一些泵把水抽走.泵的抽水能力可以认为是无穷大,但你必须把泵放在合适的位置,从而能使所有的水能流到泵里.小镇可以认为是N×M的矩阵 ...
- POJ 2227 The Wedding Juicer (优先级队列+bfs+dfs)
思路描述来自:http://hi.baidu.com/perfectcai_/item/701f2efa460cedcb0dd1c820也可以参考黑书P89的积水. 题意:Farmer John有一个 ...
- 邻结矩阵的建立和 BFS,DFS;;
邻结矩阵比较简单,, 它的BFS,DFS, 两种遍历也比较简单,一个用队列, 一个用数组即可!!!但是邻接矩阵极其浪费空间,尤其是当它是一个稀疏矩阵的时候!!!-------------------- ...
- Collect More Jewels(hdu1044)(BFS+DFS)
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- Cleaning Robot (bfs+dfs)
Cleaning Robot (bfs+dfs) Here, we want to solve path planning for a mobile robot cleaning a rectangu ...
- LeetCode:BFS/DFS
BFS/DFS 在树专题和回溯算法中其实已经涉及到了BFS和DFS算法,这里单独提出再进一步学习一下 BFS 广度优先遍历 Breadth-First-Search 这部分的内容也主要是学习了labu ...
- (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 ...
- 搜索入门_简单搜索bfs dfs大杂烩
dfs题大杂烩 棋盘问题 POJ - 1321 和经典的八皇后问题一样. 给你一个棋盘,只有#区域可以放棋子,同时同一行和同一列只能有一个棋子. 问你放k个棋子有多少种方案. 很明显,这是搜索题. ...
- POJ 3414 Pots (BFS/DFS)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7783 Accepted: 3261 Special Ju ...
随机推荐
- MFC 相关类、函数
timeSetEvent()函数 CRectTracker类的使用 SetLocalTime设置本地时间 AdjustTokenPrivileges启用权限
- 工具——SVN常用命令
SVN一般都是团队合作做一个项目所需用到的,为了是版本的统一 ;1. Check out——从服务器端取得代码 把服务器资料库里存放的某个项目代码取出来,放到本地主机中,这个动作叫做“check ...
- 【CF MEMSQL 3.0 E. Desk Disorder】
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- Any gotchas at all with converting from MyISAM to InnoDB?
Q: I'm ready to move from MyISAM to InnoDB but wanted to know if there was a full list of things to ...
- codeforces 1060 A
https://codeforces.com/contest/1060/problem/A 题意:电话号码是以8开头的11位数,给你n 个数问最多可以有多少个电话号码 题解:min(8的个数,n/11 ...
- POJ 1050 To the Max 二维最大子段和
To the MaxTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 52281 Accepted: 27633Description ...
- 【转载】深入理解PHP Opcode缓存原理
转载地址:深入理解PHP Opcode缓存原理 什么是opcode缓存? 当解释器完成对脚本代码的分析后,便将它们生成可以直接运行的中间代码,也称为操作码(Operate Code,opcode).O ...
- Spring学习--Bean 之间的关系
Bean 之间的关系:继承.依赖. Bean 继承: Spring 允许继承 bean 的配置 , 被继承的 bean 称为父 bean , 继承这个父 bean 的 bean 称为子 bean. 子 ...
- P3076 [USACO13FEB]出租车Taxi
题目描述 Bessie is running a taxi service for the other cows on the farm. The cows have been gathering a ...
- 解决mysql报错:- Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'information_schema.PROFILING.SEQ'
mysql执行报错: - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated c ...