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;
}

原文地址:https://www.cnblogs.com/P6174/p/8159296.html

时间: 2024-12-17 03:53:00

LA 7155. ACM-ICPC World Finals 2015 F. Keyboarding的相关文章

[UVa 213]Message Decoding,ACM/ICPC World Finals 1991 信息解码

A children's board game consists of a square array of dots that contains lines connecting some of the pairs of adjacent dots. One part of the game requires that the players count the number of squares of certain sizes that are formed by these lines.

2016 ACM/ICPC亚洲区大连站 F - Detachment 【维护前缀积、前缀和、二分搜索优化】

F - Detachment In a highly developed alien society, the habitats are almost infinite dimensional space. In the history of this planet,there is an old puzzle. You have a line segment with x units' length representing one dimension.The line segment can

Puzzle, ACM/ICPC World Finals 1993, UVa227

有一个5*5的网格,其中恰好有一个格子是空的,其他格子各有一个字母.一共有4种指令:A, B, L, R,分别表示把空格上.下.左.右的相邻字母移到空格中.输入初始网格和指令序列(以数字0结束),输出指令执行完毕后的网格.如果有非法指令,应输出"Thispuzzle has no final configuration.",例如,图3-5中执行ARRBBL0后,效果如图3-6所示. 写的比较简陋,先写了容器,然后填充,写移动条件. 以下是用C写的源码: #include<stdi

[算法竞赛入门经典] Crossword Answers ACM/ICPC World Finals 1994,UVa232

Description A crossword puzzle consists of a rectangular grid of black and white squares and two lists of definitions (or descriptions). One list of definitions is for "words" to be written left to right across white squares in the rows and the

[算法竞赛入门经典]Message Decoding,ACM/ICPC World Finals 1991,UVa213

Description Some message encoding schemes require that an encoded message be sent in two parts. The first part, called the header, contains the characters of the message. The second part contains a pattern that represents the message. You must write

信息解码,ACM/ICPC World Finals 1991

问题 考虑下面的01串序列: 0, 00, 01, 10, 00, 001, 010, 011, 100, 101, 110, 0000, 0001, --, 1101, 1110, 00000, -- 首先是长度为1的串,然后是长度为2的串,依次推类.如果看成二进制,相同长度的后一个串等于前一个串加1.上述序列中不存在全为1的串: 编写一个解码程序.首先输入一个编码头(例如 AB#TANCnrtXc), 则上述序列的每个串依次对应编码头的每个字符.例如0对应A,00对应B,01对应#,--:

(并查集)Travel -- hdu -- 5441(2015 ACM/ICPC Asia Regional Changchun Online )

http://acm.hdu.edu.cn/showproblem.php?pid=5441 Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2061    Accepted Submission(s): 711 Problem Description Jack likes to travel around the wo

【题解】 2015 ACM/ICPC Asia Regional Shenyang Online

[1006] FangFang (暴力枚举) Fang Fang Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 871    Accepted Submission(s): 364 Problem Description Fang Fang says she wants to be remembered. I promise her.

2015 ACM/ICPC Asia Regional Changchun Online HDU 5444 Elven Postman【二叉排序树的建树和遍历查找】

Elven Postman Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 591    Accepted Submission(s): 329 Problem Description Elves are very peculiar creatures. As we all know, they can live for a very