2020-10-14 DFS加深理解

淘汰赛单词方阵

题目

Description

lyl有一个nn的由小写字母组成的方阵,他很喜欢一个单词S,所以他希望这个方阵里只存在这个单词,这个单词在方阵中是沿着同一方向连续摆放的。摆放可沿着 8 个方向的任一方向,同一单词摆放时不再改变方向,单词与单词之间可以交叉,因此有可能共用字母。请你输出这个方阵,为了凸显这些单词,将不是单词的位置用 '’ 代替。

Input
第一行输入一个n(1<=n<=150),表示方阵的大小。

第二行开始输入n*n的字母方阵。

第三行输入字符串S(1<=|S|<=15),表示lkx学姐喜欢的那个单词。

Output
输出凸显单词的n*n的方阵。

Sample Input 1

8
qyizhong
gydthkjy
nwidghji
orbzsfgz
hhgrhwth
zzzzzozo
iwdfrgng
yyyygggg
yizhong
Sample Output 1

yizhong
gy
*****
ni*****
oz
***
hh***
z
o**
i*****n

y
****g

易错点

DFS理解不深刻,对回溯半知半解,不知道如何保存路径,只会套用模板。
n声明了两次,导致DFS中n一直为0!!!!

代码

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>

using namespace std;

char s[155][155];
char ans[155][155];
string x;
int dx[8]={0,0,-1,-1,-1,1,1,1};
int dy[8]={1,-1,-1,0,1,1,-1,0};
int len,n;
int flag = 0;
void dfs(int a,int b,int c,int d,int step)
{
    if(a<0||a>=n||b<0||b>=n)
    {
        return;
    }
    if(x[step]!=s[a][b])
    {
        return;
    }
    if(step == len-1)
    {
        flag = 1;
        ans[a][b]=s[a][b];
        return;
    }
   dfs(a+c,b+d,c,d,step+1);
   if(flag)
    ans[a][b] = s[a][b];
    return ;
}
int main()
{
    std::ios::sync_with_stdio(false);
    int n;
    cin >> n;
    for(int i = 0;i < n;i++)
    {
        for(int j = 0;j < n;j++)
        {
            ans[i][j] = '*';
        }
    }
    string ss;
    for(int i = 0;i < n;i++)
    {
        cin >> ss;
        for(int j = 0;j < n;j++)
        {
            s[i][j] = ss[j];
        }
    }
    cin >> x;
    len = x.length();
    flag = 0;
    for(int i = 0;i < n;i++)
    for(int j = 0;j < n;j++)
    {
        if(s[i][j]==x[0])
        for(int k = 0;k < 8;k++)
        {
        flag = 0;
        dfs(i,j,dx[k],dy[k],0);
        }
    }

    for(int i = 0;i < n;i++)
    {
        for(int j = 0;j < n;j++)
        {
        cout << ans[i][j];
        }
        cout << endl;
    }

    return 0;
}


HDOJ 题目1501 Zipper

题目

Problem Description
Given three strings, you are to determine whether the third string can be formed by combining the characters in the first two strings. The first two strings can be mixed arbitrarily, but each must stay in its original order.

For example, consider forming “tcraete” from “cat” and “tree”:

String A: cat
String B: tree
String C: tcraete

As you can see, we can form the third string by alternating characters from the two strings. As a second example, consider forming “catrtee” from “cat” and “tree”:

String A: cat
String B: tree
String C: catrtee

Finally, notice that it is impossible to form “cttaree” from “cat” and “tree”.

Input
The first line of input contains a single positive integer from 1 through 1000. It represents the number of data sets to follow. The processing for each data set is identical. The data sets appear on the following lines, one data set per line.

For each data set, the line of input consists of three strings, separated by a single space. All strings are composed of upper and lower case letters only. The length of the third string is always the sum of the lengths of the first two strings. The first two strings will have lengths between 1 and 200 characters, inclusive.

Output
For each data set, print:

Data set n: yes

if the third string can be formed from the first two, or

Data set n: no

if it cannot. Of course n should be replaced by the data set number. See the sample output below for an example.

Sample Input

3
cat tree tcraete
cat tree catrtee
cat tree cttaree

Sample Output

Data set 1: yes
Data set 2: yes
Data set 3: no

易错点

一开始没读懂题,以为a和b在s中就行,最后发现是要两个字符串按顺序拼接起来。
枝剪的艺术!!!

代码

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

char a[205],b[205],s[1005];
int len,lena,lenb;
int flag;
int vis[205][205];
int DFS(int x,int y,int step)
{
    if(flag)
        return 0;
    if(step > len-1 && x<=lena-1)
    {
        return 0;
    }
    if(step > len-1 && x<=lenb-1)
    {
        return 0;
    }
    if(step == len-1)
    {
        flag = 1;
        return 1;
    }
    if(a[x] == s[step])
    {
        DFS(x+1,y,step+1);
    }
    if(b[y] == s[step])
    {
        DFS(x,y+1,step+1);
    }
}
int main()
{
    std::ios::sync_with_stdio(false);
    int t;
    cin >> t;
    int k = 1;
    while(t--)
    {
        memset(vis,0,sizeof vis);
        flag = 0;
        cin >> a >> b >> s;
        len = strlen(s);
        lena = strlen(a);
        lenb = strlen(b);
        cout << "Data set " <<k<<": ";
        if(len == lena + lenb)
        {
        if(a[lena-1]==s[len-1]||b[lenb-1]==s[len-1])
        DFS(0,0,0);
        if(flag)
            cout << "yes" << endl;
        else
            cout << "no" << endl;
        k++;
        continue;
        }
        cout << "no" << endl;
        k++;
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值