This is one of the most disgusting simulation problems I've ever done, WA has two times to pay attention to a lot of details, mainly test instructions is not easy to understand. The question test instructions is to give you a two-character matrix, the first one representing the key, and the second representing the lock. Ask you, the key can pass through the lock, if not, the output key to insert the maximum depth of the lock.
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include < queue> #define MAXN 1000#define maxm 10000#define Mem (A, B) memset (A, B, sizeof (a)) using namespace Std;char key[110][110 ], Lock[10010][1010];int R, C, D, W, ans, Tmpl, Tmpr;bool vis[1010][10110];bool JG (int l, int r, int kd, int ld) {int T MP = 0; for (int i = L;i <= r;i++) {if (key[kd][tmp] = = ' # ' &&lock[ld][i] = = ' # ') return false; tmp++; } return true; BOOL Jg2 (int ll, int rr, int dd) {int DDL = min (dd, d-1), DDK = min (r-1, r-1-(dd-d + 1)); while (DDK >= 0&&ddl >= 0) {if (!JG (ll, RR, DDK, DDL)) return false; ddk--; ddl--; } return true; void Dfs (int ll, int rr, int dd) {if (ans = = d + r) return; VIS[LL][DD] = 1; BOOL flag = 0; int tmp = DD; while (!flag) {int ddl = MIN (tmp, d-1), DDK = min (r-1, r-1-(tmp-d + 1)); Flag = 0; while (DDK >= 0&&ddl >= 0) {if (!JG (ll, RR, DDK, DDL)) {for (int j = ll;j >= 0;j--) {if (!vis[j][tmp-1]&&jg2 (J, J + c-1, tmp-1)) DFS (J, j + c-1, tmp-1); else break; } for (int j = ll + 1;j <= (w-c); j + +) {if (!vis[j][tmp-1]&& Jg2 (J, J + c-1, tmp-1)) Dfs (J, j + c-1, tmp-1); else break; } flag = 1; Break } ddk--; ddl--; } if (!flag) {tmp++; for (int j = ll;j >= 0;j--) {if (!vis[j][tmp-1]&&jg2 (J, J + c-1, tmp-1)) DFS (J, j + c-1, tmp-1); Else Break } for (int j = ll + 1;j <= (w-c); j + +) {if (!vis[j][tmp-1]&&jg2 (J, J + C -1, tmp-1)) Dfs (J, j + c-1, tmp-1); else break; }} if (tmp = = d + r) break; } ans = max (ans, tmp);} void Inits () {ans = 0; Mem (Vis, 0); return;} int main () {int cas; scanf ("%d", &cas); while (cas--) {scanf ("%d%d%*c", &r, &c); for (int i = 0;i < r;i++) scanf ("%s", Key[i]); scanf ("%d%d%*c", &d, &w); for (int i = 0;i < d;i++) scanf ("%s", Lock[i]); if (C > W) {printf ("The key falls to depth 0.\n"); Continue } inits (); for (int i = 0;i <= (w-c); i++) {int rr = i + c-1; int dd = 0; BOOL flag = 0; while (!flag) { int DDL = min (dd, d-1), DDK = min (r-1, r-1-(dd-d + 1)); Flag = 0; while (DDK >= 0&&ddl >= 0) {if (!JG (i, RR, DDK, DDL)) { for (int j = i;j >= 0;j--) {if (!vis[j][dd-1] &&jg2 (J, J + c-1, dd-1)) Dfs (J, j + c-1, dd-1); else break; } for (int j = i + 1;j <= (w-c); j + +) {if (!vi S[j][dd-1]&&jg2 (J, J + c-1, dd-1)) Dfs (J, j + c-1, dd-1); else break; } flag = 1; Break } ddk--; ddl--; } if (!flag) {VIS[I][DD] = 1, dd++; for (int j = i;j >= 0;j--) {if (!vis[j][dd-1]&&jg2 (J, j + c-1, DD -1) Dfs (J, j + c-1, dd-1); else break; } for (int j = i + 1;j <= (w-c); j + +) {if (!vis[j][dd-1] &&jg2 (J, J + c-1, dd-1)) Dfs (J, j + c-1, dd-1); else break; }} if (dd = = d + r) break; } ans = max (ans, DD); if (ans = = d + r) break; } if (ans >= D + R) printf ("The Key can Fall through.\n"); else printf ("The key falls to depth%d.\n", ans); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 1112 the Proper Key dfs+ simulation