CodeForces 803E: Roma and Poker memory search

Portal

Title description

Give you a string of n length, the difference between the number of'W' and the number of'L' is less than k in the middle, and the difference is exactly k at the end,'D' means a tie,'? 'Is not sure. Ask if such a string exists, output if it exists, and NO if it does not exist.

analysis

Somewhat similar to the writing of digital DP?
Just a memoize search

Code

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <cstring>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define _CRT_SECURE_NO_WARNINGS
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
const int INF = 0x3f3f3f3f;
const int N = 1e4 + 10;
char str[N + 1];
map<int,int> M[N];
int n,m;

bool dfs(int x,int y){
    
    
    if(x == (n + 1) && abs(y - (n + 1)) == m) return true;
    if(x == (n + 1) || abs(y - (n + 1)) >= m) return false;
    if(M[x][y]) return false;
    M[x][y] = 1;
    if(str[x] == 'W') return dfs(x + 1,y + 1);
    if(str[x] == 'L') return dfs(x + 1,y - 1);
    if(str[x] == 'D') return dfs(x + 1,y);
    if(dfs(x + 1,y + 1)) {
    
    
        str[x] = 'W';
        return true;
    }
    if(dfs(x + 1,y - 1)) {
    
    
        str[x] = 'L';
        return true;
    }
    if(dfs(x + 1,y)) {
    
    
        str[x] = 'D';
        return true;
    }
}

int main(){
    
    
    scanf("%d%d",&n,&m);
    scanf("%s",str + 1);
    if(dfs(1,n + 1)) printf("%s",str + 1);
    else puts("NO");
    return 0;
}

/**
*  ┏┓   ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃       ┃
* ┃   ━   ┃ ++ + + +
*  ████━████+
*  ◥██◤ ◥██◤ +
* ┃   ┻   ┃
* ┃       ┃ + +
* ┗━┓   ┏━┛
*   ┃   ┃ + + + +Code is far away from  
*   ┃   ┃ + bug with the animal protecting
*   ┃    ┗━━━┓ 神兽保佑,代码无bug 
*   ┃        ┣┓
*    ┃        ┏┛
*     ┗┓┓┏━┳┓┏┛ + + + +
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛+ + + +
*/



Guess you like

Origin blog.csdn.net/tlyzxc/article/details/112893777