Source: HDU 3360 national treasures
Question: If a [I] [J]! =-1 convert it to binary. A maximum of 12 digits represent the 12 positions in the graph of the question. If the corresponding position is 1, a guard can be placed there to hold a [I] [J]. location.
Train of Thought: overwrite the obviously dead minimum point to create an odd-even matching Graph
# Include <cstdio> # include <cstring> # include <vector> using namespace STD; const int maxn = 55; int vis [maxn * maxn]; int y [maxn * maxn]; vector <int> G [maxn * maxn]; int n, m; int A [maxn] [maxn]; int dir [12] [2] = {-1,-2,-2,-1,-2, 1,-1, 2, 1, 2, 1, 2,-1, 1,-2,-1, 0, 0, 1, 1, 0, 0,-1}; bool DFS (int u) {for (INT I = 0; I <G [u]. size (); I ++) {int v = G [u] [I]; If (vis [v]) continue; vis [v] = true; if (Y [v] = -1 | DFS (Y [v]) {Y [v] = u; return true;} return false;} int match () {int ans = 0; memset (Y,-1, sizeof (y); For (INT I = 0; I <n; I ++) {for (Int J = 0; j <m; j ++) {If (I + J) % 2) {memset (VIS, 0, sizeof (VIS); If (DFS (I * m + J )) ans ++ ;}}return ans ;}int main () {int CAS = 1; while (scanf ("% d", & N, & M) & (N | M) {for (INT I = 0; I <n; I ++) for (Int J = 0; j <m; j ++) scanf ("% d", & A [I] [J]); For (INT I = 0; I <n * m; I ++) g [I]. clear (); For (INT I = 0; I <n; I ++) {for (Int J = 0; j <m; j ++) {int x = A [I] [J]; If (x =-1) continue; For (int K = 0; k <12; k ++, x> = 1) {If (! X) break; If (! (X & 1) continue; int xx = I + dir [k] [0]; int YY = J + dir [k] [1]; if (XX <0 | XX> = n | YY <0 | YY> = m) continue; if (a [XX] [YY] =-1) continue; If (I + J) % 2) g [I * m + J]. push_back (XX * m + YY); elseg [XX * m + YY]. push_back (I * m + J) ;}} printf ("% d. % d \ n ", CAS ++, match ();} return 0 ;}