第一题:山
作者:Drizzle 单位:山东科技大学
Drizzle 前往山地统计大山的数目,现在收到这片区域的地图,地图中用
0(平地)
和1(山峰)
绘制而成,请你帮忙计算其中的大山数目
山总是被平地四面包围着,每一座山只能在水平或垂直方向上连接相邻的山峰而形成。一座山峰四面被平地包围,这个山峰也算一个大山
另外,你可以假设地图的四面都被平地包围着。要求:
输入:第一行输入M,N分别表示地图的行列,接下来M行每行输入N个数字表示地图
输出:输出一个整数表示大山的数目示例:
输入:
4 5 1 1 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 1 1
输出:
3
范围:
对于 5% 的数据:M,N ≤ 10
对于 100% 的数据:M,N ≤ 2000
解释:先将所有山峰都视为是独立的,然后根据题意,四个方向上每有一个山峰就合并一个。
#include<iostream>
using namespace std;
const int N=2005;
int m,n;
//m,n分别是长宽
int ma[N][N];
//先开一个地图
int ans=0;
//ans用作输出
int vis[N][N];
//是否访问过该点
int mx[4]={0,1,-1,0};
int my[4]={1,0,0,-1};
//移动的方式
int nx,ny;
//下一步位置
void search(int x,int y){
vis[x][y]=1;
//当前一步走了
for(int i=0;i<4;i++){
nx=x+mx[i];
ny=y+my[i];
if(vis[nx][ny]==0&&ny>0&&ny<=n&&nx>0&&nx<=m){
if(ma[nx][ny]==1){ //此步是山峰
vis[nx][ny]=1; //走过的就不再走
ans--; //周围有一个山峰就减1
search(nx,ny);
}
}
}
}
int main(){
cin>>m>>n;
//地图大小
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
cin>>ma[i][j];
//输入该坐标是山峰还是平地
if(ma[i][j]==1) ans++;
}
}
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
if(ma[i][j]==1&&vis[i][j]==0){
search(i,j);
}
}
}
cout<<ans;
return 0;
}
上图为个人比较容易混淆的方位问题,程序中的x和y的对应和一般坐标轴的设定是相反的。
Second Question: P1596 Lake Counting S
题目提供者FarmerJohn2
题目描述
Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water ('W') or dry land ('.'). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors. Given a diagram of Farmer John's field, determine how many ponds he has.
输入格式
Line 1: Two space-separated integers: N and M * Lines 2..N+1: M characters per line representing one row of Farmer John's field. Each character is either 'W' or '.'. The characters do not have spaces between them.
输出格式
Line 1: The number of ponds in Farmer John's field.
题意翻译
由于近期的降雨,雨水汇集在农民约翰的田地不同的地方。我们用一个N×M(1≤N≤100,1≤M≤100) 的网格图表示。每个网格中有水(
W
) 或是旱地(.
)。一个网格与其周围的八个网格相连,而一组相连的网格视为一个水坑。约翰想弄清楚他的田地已经形成了多少水坑。给出约翰田地的示意图,确定当中有多少水坑。输入格式
第一行:两个空格隔开的整数:N 和 M。
第二行到第N+1行:每行 M 个字符,每个字符是
W
或.
,它们表示网格图中的一排。字符之间没有空格。输出格式:
一行,表示水坑的数量。
输入样例
10 12 W........WW. .WWW.....WWW ....WW...WW. .........WW. .........W.. ..W......W.. .W.W.....WW. W.W.W.....W. .W.W......W. ..W.......W.输出样例
3
解释:两题区别在于输入分别为数组和字符数组,同时判别从四个方向变为了八个方向。只要在上题的基础上做一些小修改。
#include<iostream>
using namespace std;
const int N=105;
int m,n;
char ma[N][N];
int ans=0;
int vis[N][N];
int mx[8]={0,1,-1,0,1,-1,1,-1};
int my[8]={1,0,0,-1,1,-1,-1,1};
int nx,ny;
void search(int x,int y){
vis[x][y]=1;
for(int i=0;i<8;i++){
nx=x+mx[i];
ny=y+my[i];
if(vis[nx][ny]==0&&ny>=0&&ny<n&&nx>=0&&nx<m){
if(ma[nx][ny]=='W'){
vis[nx][ny]=1;
ans--;
search(nx,ny);
}
}
}
}
int main(){
cin>>m>>n;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
cin>>ma[i][j];
if(ma[i][j]=='W') ans++;
}
}
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(ma[i][j]=='W'&&vis[i][j]==0){
search(i,j);
}
}
}
cout<<ans;
return 0;
}