Problem description a square with a side length of L can be divided into l*l small squares. There are n stones placed in n small squares, can divide it into n squares, so that each square is just a stone and the N square is exactly the whole square.
Input data first contains an integer t, indicating the number of test instances, and then the T Group of data, the first row of each group contains 2 integer L, n, followed by n rows of 2 integers per row, r,c, a small square in column R row C has a stone. 1<l<=20;1<n <=L*L; 1<=r,c<=l.
Output for each test instance, if it can be divided into n squares outputs Yes, otherwise output NO
Sample Input
35 82 43 33 43 54 24 44 55 53 21 13 32 41 11 22 12 2
Sample Output
Yesnoyes
Problem-solving ideas: from the upper left corner of the traversal, if there is a stone, then traverse down, and mark the area to scan, if not found, then recursion back, until the jump
#include <cstdio> #include <cstring>int L, N, a[22][22], vis[22][22], XX, yy;int js (int x, int y, int len) {//Kee Number of stones int cnt = 0; for (int i = 0, i < len; i++) for (int j = 0; J < Len; j + +) if (a[x+i][y+j]) cnt++; return CNT;} void mark (int x, int y, int len, int k) {//mark for (int i = 0; i < len; i++) for (int j = 0; J < Len; j + +) VIS[X+I][Y+J] = k;} void GetNext () {//Gets the next vertex for (int i = 1; I <= L; i++) for (int j = 1; J <= L; j + +) if (Vis[i] [j] = = False) {xx = i; yy = J; Return }}int my_find (int x, int y, int n) {int len, CNT; for (len = 1; x + len <= l + 1 && y + len <= L + 1; len++) {cnt = JS (x, y, Len); if (cnt = = 0) continue; else if (CNT > 1) return 0; Mark (x, Y, Len, 1); N-= len * len; if (!n) return 1; GetNext (); if (My_find (xx, yy, N)) return 1; ELSE {//Recursive recovery field mark (x, y, len, 0); n + = len * len; }} return 0;} int main () {int T, r, C; scanf ("%d", &t); while (t--) {memset (A, 0, sizeof (a)); memset (Vis, 0, sizeof (VIS)); scanf ("%d%d", &l, &n); for (int i = 1; I <= N; i++) {scanf ("%d%d", &r,&c); A[R][C] = 1; } if (My_find (1, 1, L * l)) printf ("yes\n"); else printf ("no\n"); } return 0;}
Square Partition "DFS"