Square Detector
You want to write an image detection system that is able to recognize different geometric shapes. In the first version of the system you settled with just being able to detect filled squares on a grid.
You are given a grid of N×N square cells. Each cell is either white or black. Your task is to detect whether all the black cells form a square shape.
Input
The first line of the input consists of a single number T, the number of test cases.
Each test case starts with a line containing a single integer N. Each of the subsequent N lines contain N characters. Each character is either "." symbolizing a white cell, or "#" symbolizing a black cell. Every test case contains at least one black cell.
Output
For each test case i numbered from 1 to T, output "Case #i: ", followed by YES or NO depending on whether or not all the black cells form a completely filled square with edges parallel to the grid of cells.
Constraints
1 ≤ T ≤ 20
1 ≤ N ≤ 20
Example
Test cases 1 and 5 represent valid squares. Case 2 has an extra cell that is outside of the square. Case 3 shows a square not filled inside. And case 4 is a rectangle but not a square.
Example input
5 4 ..## ..## .... .... 4 ..## ..## #... .... 4 #### #..# #..# #### 5 ##### ##### ##### ##### ..... 5 ##### ##### ##### ##### #####
Example output
Case #1: YES Case #2: NO Case #3: NO Case #4: NO Case #5: YESNote: Just omit the last line in your outpfile cause that is extra and your file won't be
accepted.
SOLUTION :
#include <iostream> #include <vector> #include <algorithm> #include <fstream> using namespace std; int main() { //ifstream in("in.txt"); //ofstream out("out.txt"); char a[21][21]; int t,num=0,n; cin>>t; //INPUT FROM THE USER while(t--) { num++; int i,j,flag=0,k,x,count=0,final_count=0; vector<int> y; cin>>n; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { cin>>a[i][j]; if (a[i][j]=='#') { final_count++; } } } //STORING FIRST ROW WITH THE OCCURENCE OF "#" for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if (a[i][j]=='#') { flag=1; x=i; y.push_back(j); //cout<<i<<","<<j<<endl; } } if(flag==1) { break; } } int z=0; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if (a[i][j]=='#') { for(k=0;k<y.size();k++) { if(j==y[k]) { count++; } } } } if(count==y.size()) { //cout<<"row "<<i<<"matched"<<endl; if(x==i) { z++; x++; } } count=0; } //cout<<"z "<<z <<" y.size() "<<y.size()<<endl; cout<<"Case #"<<num; if(final_count!=(z*z)) { cout<<": NO"<<endl; } else if(z==y.size()) { cout<<": YES"<<endl; } else { cout<<": NO"<<endl; } } return 0; }
WANT A PROPER EXPLANATION FOR THIS SOLUTION, JUST Comment below . #Happy Coding
#CodeScripter
Oh great !! finally you did it :D
ReplyDeleteYes ,Did it yesterday only. But In my submission i forgot to put ":" .
ReplyDelete