MAGCARI

Segi Tiga

Jan 2nd, 2023
934
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. /*
  2.     Task    : Segi Tiga
  3.     Author  : Phumipat C. [MAGCARI]
  4.     Language: C++
  5.     Created : 02 January 2023 [21:45]
  6. */
  7. #include<bits/stdc++.h>
  8. using namespace std;
  9. int res[3][3] = {2,1,0,2,1,1,1,2,1};
  10. int dp[260][260][3];
  11. char a[260];
  12. bool recur(int l,int r,int target){
  13.     if(l == r)                  return (a[l] - '0' == target);
  14.     if(dp[l][r][target] != -1)  return dp[l][r][target];
  15.     for(int k=l;k<r;k++){
  16.         for(int i=0;i<=2;i++){
  17.             for(int j=0;j<=2;j++){
  18.                 if(res[i][j] != target) continue;
  19.                 if(recur(l,k,i) && recur(k+1,r,j))
  20.                     return dp[l][r][target] = 1;
  21.             }
  22.         }
  23.     }
  24.     return dp[l][r][target] = 0;
  25. }
  26. int main(){
  27.     cin.tie(0)->sync_with_stdio(0);
  28.     cin.exceptions(cin.failbit);
  29.     int q = 20;
  30.     while(q--){
  31.         int n;
  32.         cin >> n >> a+1;
  33.         memset(dp,-1,sizeof dp);
  34.         if(recur(1,n,0))    cout << "yes\n";
  35.         else                cout << "no\n";
  36.     }
  37.     return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment