您的位置:首页 > 运维架构

ural 1249. Ancient Necropolis

2015-12-26 10:28 323 查看

1249. Ancient Necropolis

Time limit: 5.0 second
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

inputoutput
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

Tags: none (hide tags for unsolved problems)
Difficulty: 210

题意:n*m的矩阵中,所有1组成的连通块是不是矩形。
分析:暴力。
但注意存下整张图不可能,所以只能用相邻两行比较。

/**
Create By yzx - stupidboy
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
#include <iomanip>
using namespace std;
typedef long long LL;
typedef double DB;
#define MIT (2147483647)
#define INF (1000000001)
#define MLL (1000000000000000001LL)
#define sz(x) ((int) (x).size())
#define clr(x, y) memset(x, y, sizeof(x))
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define ft first
#define sd second
#define mk make_pair

inline int Getint()
{
int ret = 0;
char ch = ' ';
bool flag = 0;
while(!(ch >= '0' && ch <= '9'))
{
if(ch == '-') flag ^= 1;
ch = getchar();
}
while(ch >= '0' && ch <= '9')
{
ret = ret * 10 + ch - '0';
ch = getchar();
}
return flag ? -ret : ret;
}

const int N = 3010;
int n, m, graph
, data
;

inline void Input()
{
//    scanf("%d%d", &n, &m);
n = Getint();
m = Getint();
}

inline void Move(int &l, int &r, int *arr)
{
for(l = r + 1; l <= m && !arr[l]; l++);
for(r = l; r < m && arr[r + 1]; r++);
}

inline void Solve()
{
bool ans = 1;
for(int i = 1; i <= n && ans; i++)
{
for(int j = 1; j <= m; j++)
data[j] = Getint();

int l1, r1 = 0, l2, r2 = 0;
for(l1 = 1; l1 <= m && !graph[l1]; l1++);
for(r1 = l1; r1 < m && graph[r1 + 1]; r1++);
for(l2 = 1; l2 <= m && !data[l2]; l2++);
for(r2 = l2; r2 < m && data[r2 + 1]; r2++);
while(l1 <= m && l2 <= m)
{
if(r1 < l2) Move(l1, r1, graph);
else if(r2 < l1) Move(l2, r2, data);
else if(l1 == l2 && r1 == r2)
{
Move(l1, r1, graph);
Move(l2, r2, data);
}
else
{
ans = 0;
break;
}
}

for(int j = 1; j <= m; j++) graph[j] = data[j];
}

puts(ans ? "Yes" : "No");
}

int main()
{
freopen("a.in", "r", stdin);
Input();
Solve();
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: