Description
During the summit, the armed forces will be highly active. the police will monitor Prague streets, the army will guard buildings, the Czech air space will be full of American F-16s. moreover, the ships and battle cruisers will be sent to guard the banks of the Vltava River. unfortunately, in the case of any incident, the Czech Admiralty have only a few captains able to control over the large sea battle. therefore, it was decided to educate new admirals. as an excellent preparation, the game of "sea battle" was chosen to help with their study program.
In this well-known game, a predefined number of ships of predefined shapes are placed on the square board in such a way that they cannot contact one another even with their corners. in this task, we will consider rectangular shaped ships only. the unknown number of rectangular ships of unknown sizes are placed on a rectangular board. all the ships are full rectangles built of hash characters. write a program that counts the total number of ships present in the field.
Input
The input consists of more scenarios. the description of each scenario begins with two integer Numbers R and C separated with a single space, 1 <= R, C <= 1000. these numbers give the number of rows and columns in the game field.
After these two numbers, there are R lines, each of them containing c characters. each character is either Hash ("#") or dot (". "). hashes denote ships, dots water.
Then, the next scenario description begins. At the end of the input, there will be a line containing two zeros instead of the field size.
Output
Output a single line for every scenario. if the ships were placed correctly (I. E ., there are only rectangles that do not touch each other even with a corner), print the sentence "there are s ships. "Where S is the number of ships.
Otherwise, print the sentence "Bad placement .".
Sample Input
6 6.....###...###...#..#..#.....#######6 8.....#.###.....###.....#.......##......##..#...#0 0
Sample output
Bad placement.There are 5 ships.
Source
CTU open2002
#
Question: # It must be a matrix, that is, a rectangle or a square, and the corners of each # rectangle cannot be co-located, for example, # In the first figure because they have a point in total, the question is output as long as there is supply
Bad placement.
Otherwise, the number of output rectangles
There was no good idea during the competition. After a long time, I had to look at other people's code and the bunker. Every time I saw #, I went to DFS and recorded the largest X, Y, minimum X, and y found, and # Number temp, if (temp = (Maxx-Minx + 1) * (Maxy-miny + 1), then the condition is met, otherwise the output is not met
Bad placement.
No, I got the code.
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define N 1005char a[N][N];int n,m;int minx,maxx,miny,maxy;int temp;void dfs(int x,int y){ a[x][y]='.'; temp++; int i,j; minx=min(minx,x); maxx=max(maxx,x); miny=min(miny,y); maxy=max(maxy,y); for(i=-1;i<=1;i++) for(j=-1;j<=1;j++) { int xx=x+i; int yy=y+j; if(xx>=0&&xx<n&&yy>=0&&yy<m&&a[xx][yy]=='#') dfs(xx,yy); }}int main(){ int i,j; while(scanf("%d%d",&n,&m),n+m) { for(i=0;i<n;i++) scanf("%s",a[i]); int ans=0,flag=0; for(i=0;i<n;i++) for(j=0;j<m;j++) if(a[i][j]=='#') { minx=maxx=i; miny=maxy=j; temp=0; dfs(i,j); if(temp==(maxx-minx+1)*(maxy-miny+1)) ans++; else { flag=1; i=n; j=m; } } if(flag) printf("Bad placement.\n"); else printf("There are %d ships.\n",ans); } return 0;}