题解:P13829【MX-X18-T1】「FAOI-R6」520

· · 题解

考虑枚举所有四个方向中连续三个字符能组成的字符串,然后看是否有一个串与 s 相等即可。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

void solve()
{
    string s; cin >> s;

    if(s[0] == s[1] || s[0] == s[2] || s[1] == s[2]){ // 不能有相同字符
        cout << "No"; return ;
    }

    int n = 5, m = 4;
    vector<vector<char>> mp = {
        {' ', '/', '*', '-'},
        {'7', '8', '9', '+'},
        {'4', '5', '6', '+'},
        {'1', '2', '3', ' '},
        {'0', '0', '.', ' '}
    };

    // 从左至右
    for(int i = 0; i < n; i ++){
        for(int j = 0; j <= m - 3; j ++){
            string t; 
            t += mp[i][j]; t += mp[i][j + 1]; t += mp[i][j + 2];
            if(t == s){
                cout << "Yes"; return ;
            }
        }
    }
    // 从右至左
    for(int i = 0; i < n; i ++){
        for(int j = m - 1; j >= 2; j --){
            string t;
            t += mp[i][j]; t += mp[i][j - 1]; t += mp[i][j - 2];
            if(t == s){
                cout << "Yes"; return ;
            }
        }
    }
    // 从上至下
    for(int j = 0; j < m; j ++){
        for(int i = 0; i <= n - 3; i ++){
            string t; 
            t += mp[i][j]; t += mp[i + 1][j]; t += mp[i + 2][j];
            if(t == s){
                cout << "Yes"; return ;
            }
        }
    }
    // 从下至上
    for(int j = 0; j < m; j ++){
        for(int i = n - 1; i >= 2; i --){
            string t; 
            t += mp[i][j]; t += mp[i - 1][j]; t += mp[i - 2][j];
            if(t == s){
                cout << "Yes"; return ;
            }
        }
    }

    cout << "No";
}

signed main()
{
    ios :: sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    solve();
    return 0;
}