3 条题解

  • 0
    @ 2024-4-9 12:00:07
    #include<bits/stdc++.h>
    using namespace std;
    char Map[1010][1010];
    int n, m, ans;
    int d[4][2] =
    {
        {-1,0},
        {1,0},
        {0,-1},
        {0,1}
    };
    struct point
    {
        int x;
        int y;
        int cnt;
        string str;
    };
    
    void bfs(int x, int y)
    {
        queue<point> que;//重新构造队列
    
        point start;
        start.x = x, start.y = y, start.cnt = 0;
        start.str = Map[x][y];
        que.push(start);
        while (!que.empty())
        {
            if (que.front().cnt == 6 && que.front().str == "tencent")
            {
                ans++;
             
            }
            else if (que.front().cnt > 6) break;
            for (int i = 0; i < 4; i++)
            {
                int tx = que.front().x + d[i][0], ty = que.front().y + d[i][1];
                if (tx >= 1 && tx <= n && ty >= 1 && ty <= m)
                {
                    point temp;
                    temp.x = tx;
                    temp.y = ty;
                    temp.cnt = que.front().cnt + 1;
                    temp.str = que.front().str + Map[tx][ty];
                    que.push(temp);
                }
            }
            que.pop();
        }
    }
    
    int main()
    {
        cin >> n >> m;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                cin >> Map[i][j];
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                bfs(i, j);            
            }
        }
        cout << ans;
        return 0;
    }
    

    信息

    ID
    81
    时间
    1000ms
    内存
    256MiB
    难度
    7
    标签
    递交数
    100
    已通过
    21
    上传者