Recover Polygon (easy)

The zombies are gathering in their secret lair! Heidi will strike hard to destroy them once and for all. But there is a little problem... Before she can strike, she needs to know where the lair is. And the intel she has is not very good.

Heidi knows that the lair can be represented as a rectangle on a lattice, with sides parallel to the axes. Each vertex of the polygon occupies an integer point on the lattice. For each cell of the lattice, Heidi can check the level of Zombie Contamination. This level is an integer between 0 and 4, equal to the number of corners of the cell that are inside or on the border of the rectangle.

As a test, Heidi wants to check that her Zombie Contamination level checker works. Given the output of the checker, Heidi wants to know whether it could have been produced by a single non-zero area rectangular-shaped lair (with axis-parallel sides).

Input

The first line of each test case contains one integer N, the size of the lattice grid (5 ≤ N ≤ 50). The next N lines each contain Ncharacters, describing the level of Zombie Contamination of each cell in the lattice. Every character of every line is a digit between 0and 4.

Cells are given in the same order as they are shown in the picture above: rows go in the decreasing value of y coordinate, and in one row cells go in the order of increasing x coordinate. This means that the first row corresponds to cells with coordinates(1, N), ..., (N, N) and the last row corresponds to cells with coordinates (1, 1), ..., (N, 1).

Output

The first line of the output should contain Yes if there exists a single non-zero area rectangular lair with corners on the grid for which checking the levels of Zombie Contamination gives the results given in the input, and No otherwise.

Example

input

6000000000000012100024200012100000000

output

Yes

Note

The lair, if it exists, has to be rectangular (that is, have corners at some grid points with coordinates (x1, y1), (x1, y2), (x2, y1), (x2, y2)), has a non-zero area and be contained inside of the grid (that is, 0 ≤ x1 < x2 ≤ N, 0 ≤ y1 < y2 ≤ N), and result in the levels of Zombie Contamination as reported in the input.

分析:四个角是1,边上是2,内部是4,检查一下即可;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <ext/rope>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define vi vector<int>
#define pii pair<int,int>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=1e2+10;
const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
using namespace std;
using namespace __gnu_cxx;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int n,m,x[2],y[2];
char a[maxn][maxn];
bool check()
{
    int i,j;
    rep(i,0,1)rep(j,0,1)if(a[x[i]][y[i]]!=‘1‘)return false;
    rep(i,x[0]+1,x[1]-1)if(a[i][y[0]]!=‘2‘)return false;
    rep(i,x[0]+1,x[1]-1)if(a[i][y[1]]!=‘2‘)return false;
    rep(i,y[0]+1,y[1]-1)if(a[x[0]][i]!=‘2‘)return false;
    rep(i,y[0]+1,y[1]-1)if(a[x[1]][i]!=‘2‘)return false;
    rep(i,x[0]+1,x[1]-1)rep(j,y[0]+1,y[1]-1)if(a[i][j]!=‘4‘)return false;
    return true;
}
int main()
{
    int i,j,k,t;
    scanf("%d",&n);
    rep(i,0,n-1)scanf("%s",a[i]);
    x[0]=y[0]=inf,x[1]=y[1]=-1;
    rep(i,0,n-1)rep(j,0,n-1)
    if(a[i][j]!=‘0‘)x[0]=min(x[0],i),x[1]=max(x[1],i),y[0]=min(y[0],j),y[1]=max(y[1],j);
    if(check())puts("Yes");else puts("No");
    //system ("pause");
    return 0;
}
时间: 2024-08-05 15:24:31

Recover Polygon (easy)的相关文章

[lintcode easy]Recover rotated sorted array

Recover Rotated Sorted Array Given a rotated sorted array, recover it to sorted array in-place. Example [4, 5, 1, 2, 3] -> [1, 2, 3, 4, 5] Challenge In-place, O(1) extra space and O(n) time. Clarification What is rotated array? For example, the orgin

Uva 11971 Polygon 想法

多边形的组成条件是最长边不能占边长总和的一半,将木棒想象成圆多砍一刀,然后是简单概率. Polygon Time Limit: 1000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description U   - Polygon Time Limit: 1 sec Memory Limit: 32 MB John has been given a segment of lenght N, how

把数组排成最小的数/1038. Recover the Smallest Number

题目描述 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323. Given a collection of number segments, you are supposed to recover the smallest number from them. For example, given {32, 321, 3214, 0229, 87}, we can recov

地理数据可视化:Simple,Not Easy

如果要给2015年的地理信息行业打一个标签,地理大数据一定是其中之一.在信息技术飞速发展的今天,“大数据”作为一种潮流铺天盖地的席卷了各行各业,从央视的春运迁徙图到旅游热点预测,从大数据工程师奇货可居到马云布道“DT”时代,“大数据”被推到了一个前所未有的高度,连国家领导人出访演讲都言必称大数据.地理信息数据天生具有大数据属性,作为整天和地理信息数据打交道的地信人自然不甘落后,地理大数据概念脱颖而出. 地理大数据是什么?大体来说就是把社会经济.自然资源.商业信息等但凡具有一点空间维度的数据一股脑

CentOS 7 / RHEL 7 : Reset / Recover forgotten root password

CentOS 7 / RHEL 7 : Reset / Recover forgotten root password October 11, 2014 by sharad chhetri 4 Comments In this post we will learn, how to reset / recover forgotten root password on CentOS 7 / RHEL 7 (Red Hat Enterprise Linux 7). On RHEL 5/6 or Cen

哈理工2015暑假训练赛BNU16488 Easy Task(简单题)

A - Easy Task Time Limit:2000MS    Memory Limit:65536KB    64bit IO Format:%lld & %llu SubmitStatusPracticeZOJ 2969 Description Calculating the derivation of a polynomial is an easy task. Given a function f(x) , we use (f(x))' to denote its derivatio

使用GPC分解多边形样例(Generic Polygon Clipper)

使用GPC分解多边形 (Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu 转载请标明来源) GPC: Generic Polygon Clipper GPC支持分解多边形.多边形求差集.交集,异或.并集 GPC_DIFF,                         /* Difference                        */ GPC_INT,                          /* Intersection    

LeetCode: Recover Binary Search Tree

LeetCode: Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note: A solution using O(n) space is pretty straight forward. Could you devise a constant space s

Designing CSS Layouts With Flexbox Is As Easy As Pie

This article is an updated excerpt of the chapter "Restyle, Recode, Reimagine With CSS3″ from our Smashing Book #3, written by Lea Verou and David Storey. - Ed. Flexible box layout (or flexbox) is a new box model optimized for UI layout. As one of th