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).
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).
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.
6
000000
000000
012100
024200
012100
000000
Yes
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; }