China Petroleum Issue B: Kinky Word Search

Topic link

May not go~~~

Title:

Give you a map, and the number of turns, give you a path, and see if you can take the side of the path at the specified number of turns.

Ideas:

Because the amount of data is relatively small, we can think of directly using dfs to search, that is, walking along the path, recording the number of steps taken, the number of turns, and the original direction in the middle. Of course, we need to reduce the branches. You can directly use the data to reduce the branches
(10 10 100 8 100). The array can still be opened. If you walk through it, it will show the same state, so you don't need to consider it.
Note that when a 1*1 map is used, if the path is 1, and the number of turns is 0, it can also be achieved

There should be more than T80 if there is no reduction in branches, and the last one should be WA97 if it is not judged.

#include <set>
#include <map>
#include <queue>
#include <string>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
typedef pair<ll, ll> pii;
#define mem(a,x) memset(a,x,sizeof(a))
#define debug(x) cout << #x << ": " << x << endl;
#define rep(i,n) for(int i=0;i<(n);++i)
#define repi(i,a,b) for(int i=int(a);i<=(b);++i)
#define repr(i,b,a) for(int i=int(b);i>=(a);--i)
const int maxn = 2e5 + 1010;
#define inf 0x3f3f3f3f
#define x first
#define y second
#define sf scanf
#define pf printf
const int mod = 998244353;
const int MOD = 10007;
const int dx[] = {
    
    1, 1, 1, 0, 0, -1, -1, -1};
const int dy[] = {
    
    1, 0, -1, -1, 1, -1, 1, 0};
 
ll a[maxn], b[maxn];
ll ans, n, m, len, nn;
string s;
#define read read()
bool d[11][11][101][8][101];
char dp[200][200];
 
void dfs(ll x, ll y, ll sum, ll flag, ll num)
{
    
    
    if(num == len && sum == m)
    {
    
    
        ans = 1;
        return ;
    }
    if(num >= len) return ;
    if(sum > m) return ;
 
    if(sum >= 0 && flag >= 0 && d[x][y][sum][flag][num])
    {
    
    
        return ;
    }
    else if(sum >= 0 && flag >= 0 && !d[x][y][sum][flag][num])
    {
    
    
        d[x][y][sum][flag][num] = 1;
    }
 
    for(int i = 0; i < 8; i++)
    {
    
    
        ll fx = dx[i] + x, fy = dy[i] + y;
        if(fx <= n && fx >= 1 && fy <= nn && fy >= 1)
        {
    
    
            if(dp[fx][fy] == s[num])
            {
    
    
                if(i != flag)
                {
    
    
                    dfs(fx, fy, sum + 1, i, num + 1);
                }
                else
                {
    
    
                    dfs(fx, fy, sum, i, num + 1);
                }
            }
        }
    }
}
void solve()
{
    
    
    cin >> n >> nn;
    for(int i = 1; i <= n; i++)
    {
    
    
        for(int j = 1; j <= nn; j++)
        {
    
    
            cin >> dp[i][j];
        }
    }
    cin >> m;
    cin >> s;
    ans = 0;
    len = s.size();
    if(len == 1)
    {
    
    
        for(int i = 1; i <= n; i++)
        {
    
    
            for(int j = 1; j <= nn; j++)
            {
    
    
                if(dp[i][j]==s[0]&&m==0){
    
    
                    puts("YES");
                    return ;
                }
            }
        }
    }
    else
    {
    
    
        for(int i = 1; i <= n; i++)
        {
    
    
            for(int j = 1; j <= nn; j++)
            {
    
    
                if(dp[i][j] == s[0])
                {
    
    
                    dfs(i, j, -1, -1, 1);
                }
                if(ans == 1)
                {
    
    
                    puts("YES");
                    return ;
                }
            }
        }
    }
    puts("NO");
}
 
int main()
{
    
    
    solve();
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45911397/article/details/115032768