hrbust 1759 Lawnmower【思维题】

来源:互联网 发布:java的框架 编辑:程序博客网 时间:2024/05/01 07:27

Lawnmower

Time Limit: 1000 MS

Memory Limit: 65535 K

 

Total Submit: 46(22 users)

Total Accepted: 24(20 users)

Rating: 

Special Judge: No

 

Description

Alice and Bob have a lawn in front of their house, shaped like an N meter by M meter rectangle. Each year, they try to cut the lawn in some interesting pattern. They used to do their cutting with shears, which was very time-consuming; but now they have a new automatic lawnmower with multiple settings, and they want to try it out.

The new lawnmower has a height setting - you can set it to any height h between 1 and 100 millimeters, and it will cut all the grass higher than h it encounters to height h. You run it by entering the lawn at any part of the edge of the lawn; then the lawnmower goes in a straight line, perpendicular to the edge of the lawn it entered, cutting grass in a swath 1m wide, until it exits the lawn on the other side. The lawnmower's height can be set only when it is not on the lawn.

Alice and Bob have a number of various patterns of grass that they could have on their lawn. For each of those, they want to know whether it's possible to cut the grass into this pattern with their new lawnmower. Each pattern is described by specifying the height of the grass on each 1m x 1m square of the lawn.

The grass is initially 100mm high on the whole lawn.

Input

There are multiple test cases, processing to the-end-of-file.

Each test case begins with a line containing two integers: N and M. Next follow N lines, with the ith line containing M integers ai,j each, the number ai,j describing the desired height of the grass in the jth square of the ith row.

Limits

1 ≤ N, M ≤ 100.

1 ≤ ai,j ≤ 100.

Output

For each test case, output one line containing "y", y is either the word "YES" if it's possible to get the x-th pattern using the lawnmower, or "NO", if it's impossible (quotes for clarity only).

Sample Input

3 3

2 1 2

1 1 1

2 1 2

5 5

2 2 2 2 2

2 1 1 1 2

2 1 2 1 2

2 1 1 1 2

2 2 2 2 2

1 3

1 2 1

Sample Output

YES

NO

YES


 题目大意:

Alice和Bob有一个n*m的草地,初始的时候可以当做每个格子里边的草都是无向高的,现在他俩有一个除草机,然而这个除草机,只能除一行或者一列的草,当然可以除任意高度(也就是说剩下的高度也是任意的),然后给你一个渴望获得的n*m的草地的高度图,问你能否得到这样一个图。


思路:


1、首先我们知道,一个格子最后剩下的高度要么是横着出来的,要么是竖着出来的,因为方案比较复杂,我们不讨论一个格子是如何得到的,我们讨论一个格子是否能够得到。


2、一个格子最后剩下的高度要么是横着出来的,要么是竖着出来的,那么如果其当前格子的行里边有任意一个格子的高度高于这个当前格子的高度,那么我们能够确定这个格子一定不是通过横着得到的,因为如果这个格子是横着得到的,那么一定不会出现比这个格子的高度还高的格子出现,只会出现比这个格子低或者是同样高的格子。那么同理,如果当前格子列里边有任意一个格子的高度高于当前格子的高度,那么这个格子不能够通过横着或者是竖着除草的方式获得,那么这个格子就是一个非法格子。


3、那么我们暴力判断每一个格子是否合法,如果不合法(当前格子行里边有一个比这个格子高的格子,并且当前格子列里边也有一个比这个格子高的格子)进行标记即可、其时间复杂度O(n*m*(n+m))显然不会超时。


Ac代码:

#include<stdio.h>#include<string.h>using namespace std;int n,m;int a[500][500];int judgecol(int x,int y,int val){    int xx=x;    int yy=y;    while(1)    {        yy--;        if(yy>=0)        {            if(a[xx][yy]>val)return 1;        }        else break;    }    xx=x;    yy=y;    while(1)    {        yy++;        if(yy<m)        {            if(a[xx][yy]>val)return 1;        }        else break;    }    return 0;}int judgerow(int x,int y,int val){    int xx=x;    int yy=y;    while(1)    {        xx--;        if(xx>=0)        {            if(a[xx][yy]>val)return 1;        }        else break;    }    xx=x;    yy=y;    while(1)    {        xx++;        if(xx<n)        {            if(a[xx][yy]>val)return 1;        }        else break;    }    return 0;}int main(){    while(~scanf("%d%d",&n,&m))    {        for(int i=0;i<n;i++)        {            for(int j=0;j<m;j++)            {                scanf("%d",&a[i][j]);            }        }        int biaoji=0;        for(int i=0;i<n;i++)        {            for(int j=0;j<m;j++)            {                int flag=0;                if(judgerow(i,j,a[i][j])==1)flag++;                if(judgecol(i,j,a[i][j])==1)flag++;                if(flag==2)biaoji=1;            }        }        if(biaoji==1)printf("NO\n");        else printf("YES\n");    }}






0 0