Facebook 资格赛 A
Square Detector20 points
- Download Input File
-
Time Expired
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 printYESif all the black cells form a completely filled square with edges parallel to the grid of cells. Otherwise printNO.
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.
5 4 ..## ..## .... .... 4 ..## ..## #... .... 4 #### #..# #..# #### 5 ##### ##### ##### ##### ..... 5 ##### ##### ##### ##### #####
Case #1: YES Case #2: NO Case #3: NO Case #4: NO Case #5: YES
1 #include <cstdio> 2 #include <iostream> 3 #include <vector> 4 #include <set> 5 #include <cstring> 6 #include <string> 7 #include <map> 8 #include <cmath> 9 #include <ctime> 10 #include <algorithm> 11 #include <queue> 12 13 using namespace std; 14 #define INF 0x7fffffff 15 #define maxm 1001 16 #define mp make_pair 17 #define pb push_back 18 #define rep(i,n) for(int i = 0; i < (n); i++) 19 #define re return 20 #define fi first 21 #define se second 22 #define sz(x) ((int) (x).size()) 23 #define all(x) (x).begin(), (x).end() 24 #define sqr(x) ((x) * (x)) 25 #define sqrt(x) sqrt(abs(x)) 26 #define y0 y3487465 27 //#define y1 y8687969 28 #define fill(x,y) memset(x,y,sizeof(x)) 29 30 typedef vector<int> vi; 31 typedef long long ll; 32 typedef long double ld; 33 typedef double D; 34 typedef pair<int, int> ii; 35 typedef vector<ii> vii; 36 typedef vector<string> vs; 37 typedef vector<vi> vvi; 38 39 template<class T> T abs(T x) { re x > 0 ? x : -x; } 40 41 const int maxn = 10005; 42 43 int n, m, t, s, k, x; 44 //string s; 45 int a[maxn][maxn]; 46 char str[maxn]; 47 int main(){ 48 int cas = 1; 49 scanf("%d", &t); 50 while (t--){ 51 memset(a, 0, sizeof a); 52 scanf("%d", &n); 53 for (int i = 0; i < n; i++){ 54 scanf("%s", str); 55 for (int j = 0; j < n; j++) 56 if (str[j] == '#')a[i][j] = 1; 57 } 58 int x1 = 21, y1 = 21, x2 = -1, y2 = -1; 59 for (int i = 0; i < n; i++) 60 for (int j = 0; j < n; j++) 61 if (a[i][j])x1 = i, y1 = j, i = n, j = n; 62 for (int i = n - 1; i >= 0; i--) 63 for (int j = n - 1; j >= 0; j--) 64 if (a[i][j])x2 = i, y2 = j, i = -1, j = -1; 65 s = 0, k = 0; 66 for (int i = 0; i < n; i++) 67 for (int j = 0; j < n; j++){ 68 if (i >= x1&&j >= y1&&i <= x2&&j <= y2&&a[i][j])k++; 69 if (a[i][j])s++; 70 } 71 printf("Case #%d: ", cas++); 72 if (s == k&&s == (x2 - x1 + 1)*(y2 - y1 + 1)&&(x2-x1+1)==(y2-y1+1))printf("YES\n"); 73 else printf("NO\n"); 74 } 75 return 0; 76 }
浙公网安备 33010602011771号