ural 1249. Ancient Necropolis
1249. Ancient Necropolis
Time limit: 5.0 second
Memory limit: 4 MB
Memory limit: 4 MB
Aerophotography data provide a bitmap picture of a hard-to-reach region. According to the suggestions of scientists, this region is a cemetery of an extinct civilization. Indeed, the picture, having been converted to a binary form, shows distinctly visible areas, dark (marked with symbols 1) and light (marked with 0). It seems that the dark areas are tombstones. It's easy to either confirm or reject the hypothesis since the race that lived in the region knew astronomy, so tombstones were always oriented along the Earth's parallels and meridians. That is why the dark areas in the picture should have the form of rectangles with the sides parallel to the axes. If it is so, then we indeed have a picture of a cemetery of an extinct race. Otherwise, new hypotheses should be suggested.
Input
The first input line contains two integers N and M, which are the dimensions of the picture provided by the aerophotography. Each of the next N lines contains M zeros or ones separated with a space. The numbers N and М do not exceed 3000.
Output
Output "Yes" if all connected dark areas in the picture are rectangles and "No" otherwise.
Samples
input | output |
---|---|
2 2 0 1 1 1 |
No |
3 3 0 0 1 1 1 0 1 1 0 |
Yes |
Problem Author: Nikita Shamgunov and Leonid Volkov
Problem Source: Open collegiate programming contest for student teams, Ural State University, March 15, 2003
Problem Source: Open collegiate programming contest for student teams, Ural State University, March 15, 2003
Tags: none
Difficulty: 210
题意:n*m的矩阵中,所有1组成的连通块是不是矩形。
分析:暴力。
但注意存下整张图不可能,所以只能用相邻两行比较。
1 /** 2 Create By yzx - stupidboy 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <cstdlib> 7 #include <cmath> 8 #include <deque> 9 #include <vector> 10 #include <queue> 11 #include <iostream> 12 #include <algorithm> 13 #include <map> 14 #include <set> 15 #include <ctime> 16 #include <iomanip> 17 using namespace std; 18 typedef long long LL; 19 typedef double DB; 20 #define MIT (2147483647) 21 #define INF (1000000001) 22 #define MLL (1000000000000000001LL) 23 #define sz(x) ((int) (x).size()) 24 #define clr(x, y) memset(x, y, sizeof(x)) 25 #define puf push_front 26 #define pub push_back 27 #define pof pop_front 28 #define pob pop_back 29 #define ft first 30 #define sd second 31 #define mk make_pair 32 33 inline int Getint() 34 { 35 int ret = 0; 36 char ch = ' '; 37 bool flag = 0; 38 while(!(ch >= '0' && ch <= '9')) 39 { 40 if(ch == '-') flag ^= 1; 41 ch = getchar(); 42 } 43 while(ch >= '0' && ch <= '9') 44 { 45 ret = ret * 10 + ch - '0'; 46 ch = getchar(); 47 } 48 return flag ? -ret : ret; 49 } 50 51 const int N = 3010; 52 int n, m, graph[N], data[N]; 53 54 inline void Input() 55 { 56 // scanf("%d%d", &n, &m); 57 n = Getint(); 58 m = Getint(); 59 } 60 61 inline void Move(int &l, int &r, int *arr) 62 { 63 for(l = r + 1; l <= m && !arr[l]; l++); 64 for(r = l; r < m && arr[r + 1]; r++); 65 } 66 67 inline void Solve() 68 { 69 bool ans = 1; 70 for(int i = 1; i <= n && ans; i++) 71 { 72 for(int j = 1; j <= m; j++) 73 data[j] = Getint(); 74 75 int l1, r1 = 0, l2, r2 = 0; 76 for(l1 = 1; l1 <= m && !graph[l1]; l1++); 77 for(r1 = l1; r1 < m && graph[r1 + 1]; r1++); 78 for(l2 = 1; l2 <= m && !data[l2]; l2++); 79 for(r2 = l2; r2 < m && data[r2 + 1]; r2++); 80 while(l1 <= m && l2 <= m) 81 { 82 if(r1 < l2) Move(l1, r1, graph); 83 else if(r2 < l1) Move(l2, r2, data); 84 else if(l1 == l2 && r1 == r2) 85 { 86 Move(l1, r1, graph); 87 Move(l2, r2, data); 88 } 89 else 90 { 91 ans = 0; 92 break; 93 } 94 } 95 96 for(int j = 1; j <= m; j++) graph[j] = data[j]; 97 } 98 99 puts(ans ? "Yes" : "No"); 100 } 101 102 int main() 103 { 104 freopen("a.in", "r", stdin); 105 Input(); 106 Solve(); 107 return 0; 108 }