听峰问雨 听峰问雨
首页
导航站
  • 编程语言

    • Python
  • 数据结构与算法
  • 设计模式
  • UVA
  • LeetCode
  • 《Go语言实战》
  • 《Go Web编程》
  • 《算法精粹 经典计算机科学问题的Python实现》
  • 学习
  • 博客搭建
  • 本站

    • 分类
    • 标签
    • 归档
  • 我的

    • 收藏
    • 关于
GitHub (opens new window)

zfprotectors

默默学习er
首页
导航站
  • 编程语言

    • Python
  • 数据结构与算法
  • 设计模式
  • UVA
  • LeetCode
  • 《Go语言实战》
  • 《Go Web编程》
  • 《算法精粹 经典计算机科学问题的Python实现》
  • 学习
  • 博客搭建
  • 本站

    • 分类
    • 标签
    • 归档
  • 我的

    • 收藏
    • 关于
GitHub (opens new window)
  • UVA

    • UVA340 - Master-Mind Hints
    • UVA401 - Palindromes
    • UVA409 - Excuses, Excuses!
    • UVA414 - Machined Surfaces
    • UVA424 - Integer Inquiry
    • UVA445 - Marvelous Mazes
    • UVA457 - Linear Cellular Automata
    • UVA458 - The Decoder
    • UVA465 - Overflow
    • UVA488 - Triangle Wave
    • UVA489 - Hangman Judge
    • UVA490 - Rotating Sentences
    • UVA494 - Kindergarten Counting Game
    • UVA537 - Artificial Intelligence?
    • UVA644 - Immediate Decodability
    • UVA694 - The Collatz Sequence
    • UVA748 - Exponentiation
    • UVA10010 - Where's Waldorf?
      • 问题描述
      • 思路
      • 代码
    • UVA10055 - Hashmat the Brave Warrior
    • UVA10071 - Back to High School Physics
    • UVA10106 - Product
    • UVA10115 - Automatic Editing
    • UVA10250 - The Other Two Trees
    • UVA10300 - Ecological Premium
    • UVA10361 - Automatic Poetry
    • UVA10420 - List of Conquests
    • UVA10494 - If We Were a Child Again
    • UVA10815 - Andy's First Dictionary
    • UVA10878 - Decode the tape
  • LeetCode

  • ACM
  • UVA
zfprotectors
2022-05-18
目录

UVA10010 - Where's Waldorf?

# 问题描述

 输入n*m字符矩阵,有k个字符串(全部均在字符矩阵中),判断这些字符串在字符矩阵中开始的哪个位置。

# 思路

暴力搜索,DFS(深度优先搜索)。从字符矩阵的第一个位置开始往它的八个方向开始比对,如果字符不一样,则往另一个方向进行查找,如果字符串不一样,则从字符矩阵的第二个位置开始继续查找,如果字符串相同,则输出该字符矩阵的下标。

# 代码

#include <iostream>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
using namespace std;
char s[100][100];
char f[30];
int dir[8][2]={{-1,-1},{-1,0},{-1,1},{0,1},
                {1,1},{1,0},{1,-1},{0,-1}};
int x,y,n,m;
void dfs(int a,int b,char f[])
{
    char g[30];
    int i,j,k;
    int len=strlen(f);
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(s[i][j]!=f[0])
                continue;
            for(k=0;k<8;k++)
            {
                g[0]=s[i][j];
                int dx=i+dir[k][0];
                int dy=j+dir[k][1];
                int t=1;
                while(dx>=0&&dx<n&&dy>=0&&dy<m)
                {
                    g[t++]=s[dx][dy];
                    if(t==len)
                        break;
                    dx+=dir[k][0];
                    dy+=dir[k][1];
                }
                g[t]='\0';
                if(strcmp(g,f)==0)
                {
                    x=i+1;
                    y=j+1;
                    return;
                }
            }
        }
    }
}
int main()
{
    int t,i,j,k;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        for(i=0;i<n;i++)
        {
            scanf("%s",s[i]);
            for(j=0;j<m;j++)
                s[i][j]=tolower(s[i][j]);
        }
        cin>>k;
        for(i=0;i<k;i++)
        {
            scanf("%s",f);
            for(j=0;f[j]!='\0';j++)
                f[j]=tolower(f[j]);
            x=y=0;
            dfs(x,y,f);
            cout<<x<<" "<<y<<endl;
        }
        if(t)
            cout<<endl;

    }
    return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
编辑 (opens new window)
#ACM#C++#UVA
上次更新: 2022/05/18, 20:28:19
UVA748 - Exponentiation
UVA10055 - Hashmat the Brave Warrior

← UVA748 - Exponentiation UVA10055 - Hashmat the Brave Warrior→

最近更新
01
LeetCode88 - 合并两个有序数组
06-22
02
LeetCode1 - 两数之和
06-22
03
LeetCode1603 - 设计停车系统
06-21
更多文章>
Theme by Vdoing | Copyright © 2021-2022 zfprotectors | 闽ICP备2021014222号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式