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

Posted Planet6174

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LA 7155. ACM-ICPC World Finals 2015 F. Keyboarding相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

LA 7150. ACM-ICPC World Finals 2015 A. Amalgamated Artichokes

acm-icpc 有一道题(好像是World Final的),要求判断一个多项式是不是恒为整数,求这道题,急!

2018 ACM-ICPC World Finals Gym-102482C Conquer the World

2018 ACM-ICPC World Finals - Beijing

2018 ACM-ICPC World Finals Gym-102482I Triangles

World Cup(The 2016 ACM-ICPC Asia China-Final Contest dfs搜索)