Sicily 1862. Delicious cakes

来源:互联网 发布:什么是无线传感器网络 编辑:程序博客网 时间:2024/06/05 18:47

1862. Delicious cakes

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

Camy is good at cooking cakes.The delicious and funny cake she cooked is square all the time.



For example,the cake above is square.And you can find that it consists of 5*5 small blocks of the same size 1*1.The asterisks represent the jujubes Camy put on the cake.
Now a problem comes up.You have to decide whether you can divide the cake into small square pieces that each small piece has exactly one jujube.
Here is a feasible division for the above example:
 

Input

The first line of the input is a positive integer.It is the number of the test cases followed. 
The first line of each test case contains two integers L(0<L<20)and N(N>0).It represents that the cake contains L*L blocks and N jujubes.After that,N line followed.Each line contains two integers Xi and Yi.It means that there is a jujube in the block (Xi,Yi).Assume that Camy would not put more than one jujubes on one block.There may be one or several spaces between these integers. 

Output

The output of the program should consist of one line of output for each test case.The output for each test case contains a string “YES” or “NO” only.If you can find a feasible division,output “YES”,otherwise output “NO”. No any redundant spaces are needed. 

Sample Input

15 82 43 33 43 54 24 44 55 5

Sample Output

YES

// Problem#: 1862// Submission#: 3590189// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include <stdio.h>#include <string.h>const int MAXN = 100;long tot[MAXN][MAXN];bool isin[MAXN][MAXN];long L;int fill(long x, long y) {    long tx, ty, i, j, k, nNode, flag;    while (1) {        if (y > L) x++, y = 1;        if (x > L) return 1;        if (!isin[x][y]) break;        else y++;    }    for (k = 1; ; k++) {        tx = x + k - 1;        ty = y + k - 1;        if (tx > L || ty > L) break;        nNode = tot[tx][ty] - tot[tx][y - 1] - tot[x - 1][ty] + tot[x - 1][y - 1];        if (nNode > 1) break;        if (nNode == 1) {            for (i = x; i < x + k; i++)                for (j = y; j < y + k; j++) isin[i][j] = 1;            if (fill(x, y + 1)) return 1;            for (i = x; i < x + k; i++)                for (j = y; j < y + k; j++) isin[i][j] = 0;        }    }    return 0;}int main() {    long cases, i, j, N, x, y, a;    scanf("%ld", &cases);    while (cases--) {        scanf("%ld%ld", &L, &N);        memset(tot, 0, sizeof(tot));        while (N--) {            scanf("%ld%ld", &x, &y);            tot[x][y] = 1;        }        for (i = 1; i <= L; i++)            for (j = 1; j <= L; j++)                tot[i][j] = tot[i - 1][j] + tot[i][j - 1] - tot[i - 1][j - 1] + tot[i][j];        memset(isin, 0, sizeof(isin));        if (fill(1, 1)) printf("YES\n");        else printf("NO\n");    }    return 0;}                                 


0 0
原创粉丝点击