1. 程式人生 > >LA 7155. ACM-ICPC World Finals 2015 F. Keyboarding

LA 7155. ACM-ICPC World Finals 2015 F. Keyboarding

str acm-icpc pre ret %d mark cst als truct

BFS 正解就是亂搞系列

#include <cstdio>
#include <cstring>

char G[50][50];
bool mk[50][50][10001];
struct QueueNode {
  int x, y, cur, dist;
  QueueNode (int x = 0, int y = 0, int cur = 0, int dist = 0):
    x(x), y(y), cur(cur), dist(dist) {}
} Q[50 * 50 * 10001], *hd, *tl;
char txt[10002];

void Qpush(int x, int
y, int cur, int dist) { *tl++ = QueueNode(x, y, cur, dist); mk[x][y][cur] = true; } int main() { int r, c; scanf("%d%d", &r, &c); for (int i = 0; i < r; i++) for (int j = 0; j < c; j++) scanf(" %c", *(G + i) + j); scanf("%s", txt); strcat(txt, "*"); int n = strlen(txt); Q[0
] = QueueNode(); for (hd = Q, tl = Q + 1; hd != tl; hd++) { if (G[hd->x][hd->y] == txt[hd->cur]) { if (hd->cur + 1 == n) { printf("%d\n", hd->dist + 1); break; } else Qpush(hd->x, hd->y, hd->cur + 1, hd->dist + 1); } else
{ int nx, ny; for (nx = hd->x - 1; nx >= 0 && G[hd->x][hd->y] == G[nx][hd->y]; nx--); if (nx >= 0 && !mk[nx][hd->y][hd->cur]) Qpush(nx, hd->y, hd->cur, hd->dist + 1); for (nx = hd->x + 1; nx < r && G[hd->x][hd->y] == G[nx][hd->y]; nx++); if (nx < r && !mk[nx][hd->y][hd->cur]) Qpush(nx, hd->y, hd->cur, hd->dist + 1); for (ny = hd->y - 1; ny >= 0 && G[hd->x][hd->y] == G[hd->x][ny]; ny--); if (ny >= 0 && !mk[hd->x][ny][hd->cur]) Qpush(hd->x, ny, hd->cur, hd->dist + 1); for (ny = hd->y + 1; ny < c && G[hd->x][hd->y] == G[hd->x][ny]; ny++); if (ny < c && !mk[hd->x][ny][hd->cur]) Qpush(hd->x, ny, hd->cur, hd->dist + 1); } } return 0; }

LA 7155. ACM-ICPC World Finals 2015 F. Keyboarding