字节跳动游戏开发岗题目

好久不打代码,有点菜

昨天有两个题xjb写,不分析直接跑火车,现在回忆下写一下

请实现一个函数用来匹配包括'.'和'*'的正则表达式。模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但是与"aa.a"和"ab*a"均不匹配。大概就是这样一个剑指offer的题目
这个必须要dfs遍历所有情况,也就是分一下'*'和'.'然后匹配的问题,而且还有'*'之后还是'.',然后其中有一项成立就满足,如果不可选有一项不成立就是不成立

代码没有经过测试

#include <bits/stdc++.h>
using namespace std;
string s, t;
int l1, l2;
bool dfs(int cur1, int cur2)
{
    if (s[cur1] == 0 && t[cur2] == 0)
        return true;
    if (s[cur1] && t[cur2] == 0)
        return false;
    if (t[cur2 + 1] == '*')
    {

        if (s[cur1])
            return dfs(cur1, cur2 + 2);
        else
        {
            if (s[cur1] == t[cur2] || s[cur1] && t[cur2] == '.')
                return dfs(cur1 + 1, cur2 + 2) ||
                       dfs(cur1 + 1, cur2) ||
                       dfs(cur1, cur2 + 2);
            else
                dfs(cur1, cur2 + 2);
        }
    }
    if (s[cur1] == t[cur2] || s[cur1] && t[cur2] == '.')
        return dfs(cur1 + 1, cur2 + 1);
    return false;
}
int main()
{
    cin >> s >> t;
    if (dfs(0, 0))
        cout << "YES\n";
    else
        cout << "NO\n";
}

还有一个连连看消除的

代码依旧没有经过测试

这个就是直连,拐个弯连,拐两个弯连接,笔试时依旧觉得写起来比较复杂,模拟题一般都给队友了,我对细节处理有时候挺糟的吧

现在想想,我不一定非要写3个枚举去判断,我可以bfs记录走到哪个位置,拐了几个弯和现在方向,初始自己搞一下就行

 

#include <bits/stdc++.h>
using namespace std;
struct T
{
    int x, y, turn, step;
} tmp;

int n, m, a[55][55];
int dx[4] = {1, 0, 0, -1};
int dy[4] = {0, 1, -1, 0};
int ax, ay, ex, ey;
int bfs()
{
    queue<T> Q;
    Q.push({ax, ay, -1, -1});
    while (!Q.empty())
    {
        tmp = Q.front();
        Q.pop();
        //cout << "debug: " << tmp.x << " " << tmp.y << " " <<tmp.step<<"\n";
        if (tmp.step > 2)
            continue;
        if (tmp.x == ex && tmp.y == ey && tmp.step >= 0 && tmp.step <= 2)
            return 1;
        for (int i = 0; i < 4; i++)
        {
            int tx = tmp.x + dx[i], ty = tmp.y + dy[i];
            //cout << "debug2: " << i << " " << tx << " " << ty << "\n";
            if (tx == ex && ty == ey)
            {
                //cout<<"End!!!\n";
            }
            else if (tx == 0 || ty == 0 || tx > n || ty > m || a[tx][ty] 
            || i + tmp.turn == 3)
                continue;
            Q.push({tx, ty, i, tmp.step + (i != tmp.turn)});
        }
    }
    return 0;
}
int main()
{
    while (cin >> n >> m)
    {
        if (n == 0 && m == 0)
            break;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
            {
                cin >> a[i][j];
            }
        int q;
        cin >> q;
        while (q--)
        {
            cin >> ax >> ay >> ex >> ey;
            if (a[ax][ay] != a[ex][ey] || a[ax][ay] == 0 || a[ex][ey] == 0)
                printf("NO\n");
            else
            {
                if (bfs())
                    printf("YES\n"), a[ax][ay] = a[ex][ey] = 0;
                else
                    printf("NO\n");
            }
        }
    }
}

 

posted @ 2019-07-01 19:46  暴力都不会的蒟蒻  阅读(949)  评论(0编辑  收藏  举报