Skip to content

Instantly share code, notes, and snippets.

@sturgle
Last active December 29, 2015 01:59
Show Gist options
  • Save sturgle/7597164 to your computer and use it in GitHub Desktop.
Save sturgle/7597164 to your computer and use it in GitHub Desktop.
Square Detector
#include <vector>
#include <iostream>
#include <string>
using namespace std;
int main()
{
int kase;
cin>> kase;
for (int k = 1; k <= kase; k++)
{
int n;
cin >> n;
vector<vector<int> > vec(n);
for (int i = 0; i < n; i++)
{
vec[i].resize(n);
}
int first_x = -1;
int first_y = -1;
bool found_black = false;
// init and look for the first black
for (int i = 0; i < n; i++)
{
string line;
cin >> line;
for (int j = 0; j < n; j++) {
if (line[j] == '#')
{
vec[i][j] = 1;
if (!found_black)
{
first_x = i;
first_y = j;
found_black = true;
}
}
else
{
vec[i][j] = 0;
}
}
}
if (!found_black)
{
cout << "Case #" << k << ": NO" << endl;
}
// get the len of the square
int len = 0;
for (int j = first_y; j < n; j++)
{
if (vec[first_x][j] == 1)
len++;
else
break;
}
// go through the matrix and to hit testing
bool valid = true;
for (int i = 0; i < n && valid; i++)
{
for (int j = 0; j < n && valid; j++)
{
if (i < first_x || j < first_y
|| i >= first_x + len || j >= first_y + len) // outside
{
if (vec[i][j] == 1)
{
valid = false;
}
}
else if (vec[i][j] == 0)
{
valid = false;
}
}
}
if (!valid)
{
cout << "Case #" << k << ": NO" << endl;
}
else
{
cout << "Case #" << k << ": YES" << endl;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment