UVA 10010 Where's Waldorf? (DFS)

分析:英文题就是难懂,看了好久也没看明白,原来是一次只能朝一个方向深搜到底。此坑一解,本题就类似于油田

问题了。



#include<iostream>
#include<cstring>
#include<cstdio>
#include<cctype>
#include<algorithm>

using namespace std;

int m,n;

int pX;
int pY;

char pic[60][60];

int dx[] = {-1,0,1,1,1,0,-1,-1};//
int dy[] = {1,1,1,0,-1,-1,-1,0};//八方向

bool is_in(int x,int y)//确定是否在矩阵中
{
    if(x>=0&&x<m&&y>=0&&y<n)
        return 1;
    else return 0;
}
void dfs(char input[])//搜索成立的字符串
{
    int i,j,k;
    int Len=strlen(input);
    for(i=0; i<m; i++)
        for(j=0; j<n; j++)
            for(k=0;k<8;k++)//八方向遍历寻找适合的字符串
            {
                char s[50];
                s[0]=pic[i][j];
                int newx=i+dx[k];
                int newy=j+dy[k];
                int t=1;
                while(is_in(newx,newy))
                {
                    s[t++]=pic[newx][newy];
                    if(t==Len)
                        break;
                    newx+=dx[k];
                    newy+=dy[k];
                }
                s[t]='\0';
                if(strcmp(s,input)==0)
                {
                    pX=i+1;
                    pY=j+1;
                    return;
                }
            }
    return;
}
int main()
{
    int i,j;
    int t=0;
    int N;
    cin >> N;
    while(N--)
    {
        if(t!=0)
            cout<<endl;
        t++;
        cin>>m>>n;
        for(i=0; i<m; i++)
            for(j=0; j<n; j++)//转化为小写
            {
                cin>>pic[i][j];
                pic[i][j]=tolower(pic[i][j]);
            }
        int t;
        cin>>t;
        while(t--)
        {
            char str[60];
            cin>>str;
            int len=strlen(str);
            for(j=0; j<len; j++)
            {
                str[j]=tolower(str[j]);
            }
            dfs(str);
            cout<<pX<<" "<<pY<<endl;
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值