zoukankan      html  css  js  c++  java
  • UVA10010的类似BFS做法

    Where's Waldorf? 

    Given a m by n grid of letters, ( $1 \leq m,n \leq 20$), and a list of words, find the location in the grid at which the word can be found. A word matches a straight, uninterrupted line of letters in the grid. A word can match the letters in the grid regardless of case (i.e. upper and lower case letters are to be treated as the same). The matching can be done in any of the eight directions either horizontally, vertically or diagonally through the grid.

    Input 

    The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

    The input begins with a pair of integers, m followed by n, $1 \leq
m,n \leq 50$ in decimal notation on a single line. The next m lines contain n letters each; this is the grid of letters in which the words of the list must be found. The letters in the grid may be in upper or lower case. Following the grid of letters, another integer k appears on a line by itself ( $1 \leq k \leq 20$). The next k lines of input contain the list of words to search for, one word per line. These words may contain upper and lower case letters only (no spaces, hyphens or other non-alphabetic characters).

    Output 

    For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

    For each word in the word list, a pair of integers representing the location of the corresponding word in the grid must be output. The integers must be separated by a single space. The first integer is the line in the grid where the first letter of the given word can be found (1 represents the topmost line in the grid, and m represents the bottommost line). The second integer is the column in the grid where the first letter of the given word can be found (1 represents the leftmost column in the grid, and n represents the rightmost column in the grid). If a word can be found more than once in the grid, then the location which is output should correspond to the uppermost occurence of the word (i.e. the occurence which places the first letter of the word closest to the top of the grid). If two or more words are uppermost, the output should correspond to the leftmost of these occurences. All words can be found at least once in the grid.

    Sample Input 

    1
    
    8 11
    abcDEFGhigg
    hEbkWalDork
    FtyAwaldORm
    FtsimrLqsrc
    byoArBeDeyv
    Klcbqwikomk
    strEBGadhrb
    yUiqlxcnBjf
    4
    Waldorf
    Bambi
    Betty
    Dagbert
    

    Sample Output 

    2 5
    2 3
    1 2
    7 8
    


    我能告诉你这个题目卡了N久,就是因为输出空行的问题吗?不过发现很多人写的关于这个题目都是这个问题,这个题目我创新了一个类似BFS的做法,向八个方向搜索,但是每个搜索方向都是固定好了的,所以在定义结构体的时候,我定义了一个dire量,用了专门控制方向不改变,然后就是BFS的典型做法,将搜到的点判断,讲不合法的点T出去,然后把合法的点放入队列,为了判断是否找到,还定义了一个cur变量 用来记录此时已经搜到第几个字母,一旦cur变量等于所搜索的字符串长度,意味着成功找到
     
    The outputs of two consecutive cases will be separated by a blank line.题目里的这句话是亮点,一直通不过就是因为它。。我之前的理解是每次输出都要多添加一个空行。。但实际意思是每个大的情况才输出空行隔开。。。。


    代码千改万改,之前也没计划好,临时想用队列来做,可以说是我写得最乱七八糟的一次代码
    #include <iostream>
    #include <cstdio>
    #include <queue>
    #include <cstring>
    using namespace std;
    char grid[100][100];
    int dir[][2]={{1,0},{0,1},{0,-1},{-1,0},{1,1},{-1,-1},{1,-1},{-1,1}};
    struct node{
    int x;
    int y;
    int cur;
    int dire;
    };
    int main()
    {
        char find[110];
        int t;
        scanf("%d",&t);
        for (int q1=1;q1<=t;q1++)
        {
        int m,n;
        scanf("%d %d",&m,&n);
        int asc;
    
        for (int i=0;i<m;i++){
            scanf("%s",grid[i]);
          for (int j=0;j<n;j++){
             asc=grid[i][j];
             if (asc<97) grid[i][j]=asc+32;
          }
        }
        int k;
        scanf("%d",&k);
        for (int w1=0;w1<k;w1++){
          scanf("%s",find);
          int len=strlen(find);
          for (int r=0;r<len;r++){
              int asc2=find[r];
             if (asc2<97)
              find[r]=asc2+32;
          }
          int sx=0,sy=0;
          bool flag=false;
          for (int k=0;k<m;k++){
            for (int p=0;p<n;p++){
                if (grid[k][p]==find[0])
                {
                    queue<node> q;
                    node a;
                    for (int u=0;u<8;u++){
                    a.x=k,a.y=p,a.cur=1,a.dire=u;
                    q.push(a);
                    }
                    while (!q.empty()){
                     node b=q.front();
                     q.pop();
                     int x1=b.x;
                     int y1=b.y;
                        x1=x1+dir[b.dire][0];
                        y1=y1+dir[b.dire][1];
                       if (x1>=m||y1>=n||x1<0||y1<0) {continue;}
                       if (find[b.cur]!=grid[x1][y1]) {continue;}
                       node c;
                       c.x=x1;c.y=y1;c.cur=b.cur+1;c.dire=b.dire;
                       q.push(c);
                       if (c.cur==len) {flag=true;sx=k+1;sy=p+1;break;}
                    }
                }
                if (flag) break;
            }
            if (flag) break;
          }
          printf("%d %d\n",sx,sy);
        }
          if (q1!=t) putchar('\n');
        }
        return 0;
    }
  • 相关阅读:
    BFC
    js异常处理
    vue双向数据绑定的简单实现
    cookie封装,localstage封装
    问题 1476: [蓝桥杯][基础练习VIP]龟兔赛跑预测 (模拟)
    HDU 6205 (模拟) card card card
    HDU 4545 (模拟) 魔法串
    HDU 4521 小明系列问题——小明序列 (线段树 单点更新)
    基础动态规划 讲解
    HDU 1561 The more, The Better (有依赖背包 || 树形DP)
  • 原文地址:https://www.cnblogs.com/kkrisen/p/3030000.html
Copyright © 2011-2022 走看看