Description
Give you a satellite photograph of a certain sea area, you need statistics: 1. The number of islands in the photo 2. The number of islands in the photo is 3. The number of islands in the photo with
different shapes is
as follows, "." Represents the ocean, "#" denotes land. An island is formed on a piece of land connected together in the "Up and down" four directions.
.####..
.....#.
####.#.
.....#.
.. ##.#.
The picture above shows a total of 4 islands, of which 3 are 4 and one area is 2, so the number of islands in different areas is 2, and the two shapes are "# # #", so the number of islands with different shapes is 3.
input
The first line contains two integers: N and M, (1≤n, m≤50), representing the number of rows and columns of the photo.
a matrix of N * M below that represents a photograph of the sea area.
Output
Outputs 3 integers, followed by the number of islands in the photo, the number of islands with different areas, and the number of islands with different shapes.
Sample Input
5 7.
# # # # # # #
.....#.
####.#.
.....#.
.. ##.#.
Sample Output
4 2 3
Thinking of solving problems
The first result and the second result are well handled, and the key is how the third one marks the shape of the island. Since the range is small, it starts with thinking that every island has a 50*50 array, each traversing the array of islands to determine if there has been, for the sake of comparison, the position of each point minus the starting point at the time of storage. That's 40 minutes.
Later found
5 4
.. #.
. ###
....
.. #.
.. ##
A set of data such as this does not produce the correct results, the first island starting point location is (0,2), the second row of the first column is (a), subtraction (1,-1), it is obviously wrong. The
is eventually converted into a one-dimensional array to hold the shape of the array, numbering the map from top left to right, and storing the number in the array (for comparison purposes, each position should be subtracted from the numbering value of the starting point). Code Implementation
#include <iostream> #include <cstdio> #include <cstring> #include <queue> using namespace std; #define MAXN 6 bool VAREA[MAXN];
Whether the marked area value has occurred in Char MAPS[MAXN][MAXN],STR[MAXN];
int A[MAXN][MAXN*MAXN],VISIT[MAXN][MAXN];
int M,n,area,countt;
int dir[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};
Queue<pair<int,int> >qu;
void BFs (int x,int y) {int cha=x*n+y;
Qu.push (Make_pair (x, y));
while (!qu.empty ()) {int Xx=qu.front (). First;
int Yy=qu.front (). Second; a[countt][area]= (XX*N+YY)-cha;
Storage Island Shape area++;
Qu.pop ();
for (int i=0; i<4; i++) {int nx=xx+dir[i][0];
int ny=yy+dir[i][1]; if (nx>=0&&nx<m&&ny>=0&&ny<n&&maps[nx][ny]== ' # ' &&!visit[nx]
[NY])
{Qu.push (Make_pair (Nx,ny));
Visit[nx][ny]=1; }}}} int main () {int ans,ansarea,ansdif; //represents three result values, bool flag, respectively;
while (~scanf ("%d%d%*c", &m,&n)) {ans=0;
ansarea=0;
ansdif=0;
Countt=-1;
memset (varea,0,sizeof (Varea));
memset (A,0,sizeof (a));
memset (visit,0,sizeof (visit));
for (int i=0; i<m; i++) {scanf ("%s", str);
for (int j=0; j<n; j + +) Maps[i][j]=str[j]; } for (int i=0, i<m; i++) {for (int j=0; j<n; J + +) {if (!visi
t[i][j]&&maps[i][j]== ' # ') {visit[i][j]=1;
countt++;
if (countt==0) ansdif++;
area=0;
BFS (I,J);
if (!varea[area]) {ansarea++;
Varea[area]=1;
} flag=true;
for (int k=0; k<countt; k++) {flag=true;
for (int p=0; p<maxn*maxn-1; p++) {if (a[countt][p]!=a[k][p])
{flag=false;
Break
}} if (flag) break;
} if (!flag) ansdif++;
ans++;
}}} printf ("%d%d%d\n", ans,ansarea,ansdif);
} return 0; }
PS:
Because of the principle of memory allocation, the address of the array (1,-1) will be the address of the last line, then the original method can also be passed, posted here to warn themselves can not be considered in this error-prone situation.
#include <iostream> #include <cstdio> #include <cstring> #include <queue> using namespace std;
#define MAXN-BOOL VAREA[MAXN*MAXN];
Char MAPS[MAXN][MAXN],STR[MAXN];
int A[1500][100][100],VISIT[MAXN][MAXN];
int M,n,area,countt;
int dir[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};
Queue<pair<int,int> >qu;
void BFs (int x,int y) {Qu.push (Make_pair (x, y));
while (!qu.empty ()) {int Xx=qu.front (). First;
int Yy=qu.front (). Second;
A[countt][xx-x][yy-y]=1;
Qu.pop ();
for (int i=0; i<4; i++) {int nx=xx+dir[i][0];
int ny=yy+dir[i][1]; if (nx>=0&&nx<m&&ny>=0&&ny<n&&maps[nx][ny]== ' # ' &&!visit[nx]
[NY])
{area++;
Qu.push (Make_pair (Nx,ny));
Visit[nx][ny]=1;
}}}} int main () {int ans,ansarea,ansdif;
BOOL Flag; while (~scanf ("%d%d%*c",&m,&n)) {ans=0;
ansarea=0;
ansdif=0;
Countt=-1;
memset (varea,0,sizeof (Varea));
memset (A,0,sizeof (a));
memset (visit,0,sizeof (visit));
for (int i=0; i<m; i++) {scanf ("%s", str);
for (int j=0; j<n; J + +) {Maps[i][j]=str[j];
}} for (int i=0, i<m; i++) {for (int j=0; j<n; J + +) {
if (!visit[i][j]&&maps[i][j]== ' # ') {countt++;
if (countt==0) ansdif++;
area=0;
BFS (I,J);
if (!varea[area]) {ansarea++;
Varea[area]=1;
} flag=true;
for (int k=0; k<countt; k++) {flag=true; for (int p=0, p<100; p++) {for (int q=0; q<100; q++)
{if (A[countt][p][q]!=a[k][p][q])
{flag=false;
Break
}} if (!flag) break;
} if (flag) break;
} if (!flag) ansdif++;
ans++;
}}} printf ("%d%d%d\n", ans,ansarea,ansdif);
} return 0;
}