URAL ——1249——————【想法题】

 Ancient Necropolis
Time Limit:5000MS     Memory Limit:4096KB     64bit IO Format:%I64d & %I64u

Description

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.

Sample Input

inputoutput
2 2
0 1
1 1
No
3 3
0 0 1
1 1 0
1 1 0
Yes

 

 

题目大意:问你在给出的n*m的矩阵中,所有1组成的连通块儿是不是矩形。如果是,输出“Yes”否则输出“No”。

解题思路:在开始的时候,没看内存,只看了时限,感觉可以先找到矩形的长,然后暴力宽,把矩形标记出来,如果发现有不合格的,就直接退出。但是交上去发现超内存。。。最后通过用bitset优化,终于过了内存,但是却又超时了。。。感觉方法有问题,赛后看了别人的代码,发现原来别人的思路更简单,就是每次枚举一个2*2的正方形,判断是不是形成了三角形,发现如果不是矩形的话,必然要形成一个三角形,然后就判断是不是有三个1。如果是三角形的话,说明不满足条件,标记一下结果。   其实我们只要存储两行的结果就行了,奇行、偶行,然后交替存储。

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<bitset>
#include<iostream>
using namespace std;
const int maxn = 3100;
int r1[maxn], r2[maxn];
bool jud(int a,int b,int c,int d){
    int ret = 0;
    if(a==1) ret++;
    if(b==1) ret++;
    if(c==1) ret++;
    if(d==1) ret++;
    if(ret == 3)
        return true;
    return false;
}
int main(){
    int n,m;
    while(cin>>n>>m){
        int flag = 0;
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                if(i%2){
                    scanf("%d",&r1[j]);
                }else{
                    scanf("%d",&r2[j]);
                }
                if(i != 1 && j != 1 && !flag)
                    flag = jud(r1[j],r1[j-1],r2[j],r2[j-1]);
            }
        }
        if(flag) puts("No\n");
        else puts("Yes");
    }
    return 0;
}


/*
3 5
0 0 0 0 0
0 1 1 1 0
1 0 1 0 1

3 5
0 0 0 0 0
0 1 1 1 0
0 1 1 1 0

3 3
0 0 1
1 1 0
0 0 1
*/

  

 

posted @ 2015-12-15 18:06  tcgoshawk  阅读(244)  评论(0编辑  收藏  举报