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

input output
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
*/

  

最新文章

  1. KNN近邻算法
  2. ASP.NET SignalR 与 LayIM2.0 配合轻松实现Web聊天室(十二) 代码重构使用反射工厂解耦(一)缓存切换
  3. 为什么xcode7请求不成功
  4. 用C#编程的建议
  5. html 动态添加TABLE的行。
  6. 关于微信的jsapi_ticket的获取方法;
  7. ModelBinder——ASP.NET MVC Model绑定的核心
  8. jquery学习笔记2——jq效果
  9. RoboCup仿真3D TC笔记(2014年合肥中国公开赛 仿真3D比赛环境搭建)
  10. Flash与 Javascript 交互
  11. C++迭代器的使用和操作总结
  12. Python面向对象——重写与Super
  13. μCOS-Ⅲ——常用注意事项
  14. [转帖]漫画趣解Linux内核
  15. Django之MVC和MTV
  16. Python之路,第二篇:Python入门与基础2
  17. Python——signal
  18. Delphi7目录结构----初学者参考
  19. javax.servlet.jsp.JspException cannot be resolved to a type 和 javax.servlet.jsp.PageContext cannot be resolved to a type 解决办法
  20. 总结ing

热门文章

  1. Insus.NET最近想更换一部手机
  2. indexDB的使用和异步图片blob文件保存
  3. iOS App 内部跳转(设置、Wifi、蓝牙...)关键词
  4. Python 集合set()添加删除、交集、并集、集合操作详解
  5. mysql双机互相备份
  6. Java 中的main方法
  7. sql 语句设置主键
  8. sharding-jdbc springboot配置
  9. LeetCode268.缺失数字
  10. 关于 Gojs 你可能用到的方法 / gojs自定义 / gojs