How Many Islands?--SOJ 2013week2.1001
2013-03-17 16:11
387 查看
Description
You are given a marine area map that is a mesh of squares, each representing either a land or sea area. Figure B-1 is an example of a map.
You can walk from a square land area to another if they are horizontally, vertically, or diagonally adjacent to each other on the map. Two areas are on the same island if and only if you can walk from one to the other possibly through other land areas. The
marine area on the map is surrounded by the sea and therefore you cannot go outside of the area on foot.
You are requested to write a program that reads the map and counts the number of islands on it. For instance, the map in Figure B-1 includes three islands.
Input
The input consists of a series of datasets, each being in the following format.
w h
c1,1 c1,2 ... c1,w
c2,1 c2,2 ... c2,w
...
ch,1 ch,2 ... ch,w
w and h are positive integers no more than 50 that represent the width and the height of the given map, respectively. In other words, the map consists of w×h squares
of the same size. w and h are separated by a single space.
ci, j is either 0 or 1 and delimited by a single space. If ci, j = 0, the square that is the i-th
from the left and j-th from the top on the map represents a sea area. Otherwise, that is, if ci, j = 1, it represents a land area.
The end of the input is indicated by a line containing two zeros separated by a single space.
Output
For each dataset, output the number of the islands in a line. No extra characters should occur in the output.
Sample Input
Copy sample input to clipboard
Sample Output
0
1
1
3
1
9// Problem#: 7675
// Submission#: 1966541
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/ // All Copyright reserved by Informatic Lab of Sun Yat-sen University
/*
可以走水平 垂直
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
bool map[58][58];
bool vis[58][58];
int heng[]={-1,-1,-1,0,0,1,1,1};
int zong[]={-1,0,1,-1,1,-1,0,1};
int w,h;//w是列m,h是行
int sum;
void dfs(int r,int c)
{
vis[r][c]=1;
for(int i=0;i<8;i++)
{
int rr=r+heng[i];
int cc=c+zong[i];
if((rr>=1&&rr<=h)&&(cc>=1&&cc<=w))
{
if(!vis[rr][cc]&&map[rr][cc])
{
dfs(rr,cc);
}
}
}
}
int main()
{
while(scanf("%d%d",&w,&h)==2&&(w||h))
{
int a;
sum=0;
memset(vis,0,sizeof(vis));
for(int i=1;i<=h;i++)
{
for(int j=1;j<=w;j++)
{
cin>>a;
map[i][j]=a;
}
}
for(int i=1;i<=h;i++)
{
for(int j=1;j<=w;j++)
{
if(map[i][j]&&!vis[i][j])
{
sum++;
dfs(i,j);
}
}
}
cout<<sum<<endl;
}
return 0;
}
You are given a marine area map that is a mesh of squares, each representing either a land or sea area. Figure B-1 is an example of a map.
You can walk from a square land area to another if they are horizontally, vertically, or diagonally adjacent to each other on the map. Two areas are on the same island if and only if you can walk from one to the other possibly through other land areas. The
marine area on the map is surrounded by the sea and therefore you cannot go outside of the area on foot.
You are requested to write a program that reads the map and counts the number of islands on it. For instance, the map in Figure B-1 includes three islands.
Input
The input consists of a series of datasets, each being in the following format.
w h
c1,1 c1,2 ... c1,w
c2,1 c2,2 ... c2,w
...
ch,1 ch,2 ... ch,w
w and h are positive integers no more than 50 that represent the width and the height of the given map, respectively. In other words, the map consists of w×h squares
of the same size. w and h are separated by a single space.
ci, j is either 0 or 1 and delimited by a single space. If ci, j = 0, the square that is the i-th
from the left and j-th from the top on the map represents a sea area. Otherwise, that is, if ci, j = 1, it represents a land area.
The end of the input is indicated by a line containing two zeros separated by a single space.
Output
For each dataset, output the number of the islands in a line. No extra characters should occur in the output.
Sample Input
Copy sample input to clipboard
1 1 0 2 2 0 1 1 0 3 2 1 1 1 1 1 1 5 4 1 0 1 0 0 1 0 0 0 0 1 0 1 0 1 1 0 0 1 0 5 4 1 1 1 0 1 1 0 1 0 1 1 0 1 0 1 1 0 1 1 1 5 5 1 0 1 0 1 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 1 0 1 0 1 0 0
Sample Output
0
1
1
3
1
9// Problem#: 7675
// Submission#: 1966541
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/ // All Copyright reserved by Informatic Lab of Sun Yat-sen University
/*
可以走水平 垂直
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
bool map[58][58];
bool vis[58][58];
int heng[]={-1,-1,-1,0,0,1,1,1};
int zong[]={-1,0,1,-1,1,-1,0,1};
int w,h;//w是列m,h是行
int sum;
void dfs(int r,int c)
{
vis[r][c]=1;
for(int i=0;i<8;i++)
{
int rr=r+heng[i];
int cc=c+zong[i];
if((rr>=1&&rr<=h)&&(cc>=1&&cc<=w))
{
if(!vis[rr][cc]&&map[rr][cc])
{
dfs(rr,cc);
}
}
}
}
int main()
{
while(scanf("%d%d",&w,&h)==2&&(w||h))
{
int a;
sum=0;
memset(vis,0,sizeof(vis));
for(int i=1;i<=h;i++)
{
for(int j=1;j<=w;j++)
{
cin>>a;
map[i][j]=a;
}
}
for(int i=1;i<=h;i++)
{
for(int j=1;j<=w;j++)
{
if(map[i][j]&&!vis[i][j])
{
sum++;
dfs(i,j);
}
}
}
cout<<sum<<endl;
}
return 0;
}
相关文章推荐
- 2013秋13级预备队集训练习4 --D - How Many Points of Intersection?
- soj 3137 Simple Computing 容斥原理 hdu 1796 How many integers can you find
- Next Mayor--SOJ 2013week2.1000
- hdu 1796 How many integers can you find (容斥原理)
- hdu 2157 How many ways??
- UVa 696 How Many Knights (想法题)
- UVA 10061 - How many zero's and how many digits ?
- [HDU2157]How many ways??(DP + 矩阵优化)
- UVA11038- How Many O's?(组合数学)
- UVA 10061 How many zero's and how many digits ?
- [ZOJ3557]How Many Sets II(组合数学Lucas定理)
- hdu 3191 How Many Paths Are There(次短路) 很纠结题目。。。
- hdu2157:How many ways??
- HDU 3191 How Many Paths Are There
- How many ways
- D - How Many Answers Are Wrong HDU - 3038 带权并查集
- How Many Answers Are Wrong
- UVaOj How many pieces of land?
- hdu 2157 How many ways??(矩阵经典应用)
- HDU 3191 How Many Paths Are There