zoukankan      html  css  js  c++  java
  • 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
    ", 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;
    }
    
  • 相关阅读:
    前端页面存取数据
    jquery获取元素内容-text()和val()
    jquery选择器的一些处理
    Js判断一个字符串是否包含一个子串
    防止重复点击:
    Juery实现选项卡
    行间事件传this的问题:
    从数据库中导出数据到.csv文件
    表单限制只能填入正整数
    WAMP环境配置-Mysql安装
  • 原文地址:https://www.cnblogs.com/P6174/p/8159296.html
Copyright © 2011-2022 走看看