Title Description
As a result of global warming, the island's coastal waters continue to rise, in a precarious form.
The map of S country is made up of n*m grids, which consist of. and #, #代表的区域是陆地,. And the map is full of seawater.
Every day, the sea will erode the neighboring land, that is, if land is adjacent to the sea, it will become seawater the next day. The two blocks adjacent to each other represent a common side between the two blocks.
For example, the map of country S is like this:
.. ###...
.. ###...
.. ###...
...##...
.. ######
.. ######
...#####
Then a day later, S state becomes this:
........
...#....
...#....
........
...##...
...####.
........
This tragedy occurs every day, and in another 1 days the country will be engulfed by seawater.
Now the leader of S state wants to know what will become of T-day s state and how many islands there are. If two pieces of land belong to the same island, and only when two land is adjacent.
There are two islands in the map of s after 1 days above the chart.
Input
Multiple sets of samples are counted (not many samples).
The first line is N,m,t, which is described in the topic. (1<=n,m,t,<=2000)
Then there is a map of s country of n row m column, consisting of. and #.
Output
The first line, an integer x, represents the number of islands.
The next n lines draw a map of the country of T tin.
Sample input
7 8 1..###.....###.....###......##.....######. ######...#####
Sample output
2...........#.......#...............##......#### .....
The code of this topic is still very large.
Bfs+dfs
Similar to the fire! on petition
/************************************************author:guanjuncreated TIME:2016/3/8 8:53:07File name:ne u1682.cpp*************************************************/#include<iostream>#include<cstring>#include<cstdlib>#include<stdio.h>#include<algorithm>#include<vector>#include<queue>#include<Set>#include<map>#include<string>#include<math.h>#include<stdlib.h>#include<iomanip>#include<list>#include<deque>#include<stack>#defineull unsigned long Long#definell Long Long#defineMoD 90001#defineINF 0x3f3f3f3f#defineMAXN 10010#defineCLE (a) memset (A,0,sizeof (a))Constull inf = 1LL << A;Const Doubleeps=1e-5;using namespacestd;structnode{intx, y;}; Charmp[ .][2100];intvis[ .][ .];intdir[4][2]={1,0,0,1,0,-1,-1,0};intn,m,t;voidDfsintXinty) { for(intI=0;i<4; i++){ intnx=x+dir[i][0]; intny=y+dir[i][1]; if(!vis[nx][ny]&&nx<=n&&nx>=1&&ny<=m&&ny>=1&&mp[nx][ny]=='#') {Vis[nx][ny]=1; DFS (NX,NY); } }}voidprint () {intans=0; CLE (VIS); for(intI=1; i<=n;i++){ for(intj=1; j<=m;j++){ if(mp[i][j]=='#'&&!Vis[i][j]) {Vis[i][j]=1; DFS (I,J); Ans++; }}} printf ("%d\n", ans); for(intI=1; i<=n;i++){ for(intj=1; j<=m;j++) printf ("%c", Mp[i][j]); printf ("\ n"); }}voidBFs () {Queue<node>Q; inttmp=1; node U; T--; for(intI=0; i<=n+1; i++) {mp[i][0]='.'; Mp[i][m+1]='.'; } for(intI=0; i<=m+1; i++) {mp[0][i]='.'; Mp[n+1][i]='.'; } for(intI=0; i<=n+1; i++){ for(intj=0; j<=m+1; j + +){ if(mp[i][j]=='.'&&!Vis[i][j]) { for(intk=0;k<4; k++){ intnx=i+dir[k][0]; intny=j+dir[k][1]; //cout<<nx<< "" <<ny<<endl; if(nx<=n&&nx>=1&&ny<=m&&ny>=1){ if(mp[nx][ny]=='#'){ //cout<<nx<< "" <<ny<<endl;Node w;w.x=nx;w.y=NY; Q.push (w); Mp[nx][ny]='.'; //u.x=i,u.y=j,q.push (u);vis[nx][ny]=1; }}} Vis[i][j]=1; } } } if(t==0) {print (); } Else{Queue<node>p; //cout<<t<<endl; while(t--){ while(!Q.empty ()) {Node V=Q.front (); Q.pop (); //cout<<v.x<< "" <<v.y<<endl; for(intI=0;i<4; i++){ intnx=v.x+dir[i][0]; intny=v.y+dir[i][1]; if(nx<=n&&nx>=1&&ny<=m&&ny>=1){ if(mp[nx][ny]=='#') {Mp[nx][ny]='.'; u.x=nx,u.y=NY; P.push (U); } } //vis[nx][ny]=1; } } if(P.empty ()) Break; while(!Q.empty ()) {Q.push (P.front ());p. Pop (); }} print (); }}intMain () {#ifndef Online_judge freopen ("In.txt","R", stdin); #endif //freopen ("OUT.txt", "w", stdout); while(cin>>n>>m>>t) { for(intI=1; i<=n;i++) {scanf ("%s", mp[i]+1); } BFS (); } return 0;}
1682: Global warming