字節跳動游戲開發崗題目


好久不打代碼,有點菜

昨天有兩個題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");
            }
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM