MAGCARI

SegiTiga

Jan 4th, 2023
870
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. /*
  2.     Task    : Segi Tiga
  3.     Author  : Phumipat C. [MAGCARI]
  4.     Language: C++
  5.     Created : 04 January 2023 [20:46]
  6. */
  7. #include<bits/stdc++.h>
  8. using namespace std;
  9. char a[260];
  10. int dp[260][260][3];    //0 1 2
  11. int res[3][3] = {2,1,0,2,1,1,1,2,1};
  12. int recur(int i,int j,int target){
  13.     if(i == j){
  14.         if(a[i]-'0' == target)  return 1;
  15.         else                    return -1;
  16.     }
  17.     if(dp[i][j][target] != 0)   return dp[i][j][target];
  18.     for(int k=i;k<j;k++){
  19.         for(int x=0;x<=2;x++){
  20.             for(int y=0;y<=2;y++){
  21.                 // อยากให้ทางซ้ายได้ค่าเป็น x
  22.                 // อยากให้ทางขวาได้ค่าเป็น y
  23.                 if(res[x][y] != target) continue;
  24.                 if(recur(i,k,x) == 1 && recur(k+1,j,y) == 1){
  25.                     return dp[i][j][target] = 1;
  26.                 }
  27.             }
  28.         }
  29.     }
  30.     return dp[i][j][target] = -1;
  31. }
  32. int main(){
  33.     cin.tie(0)->sync_with_stdio(0);
  34.     cin.exceptions(cin.failbit);
  35.     int q=20;
  36.     while(q--){
  37.         int n;
  38.         cin >> n >> a+1;
  39.         memset(dp,0,sizeof dp);
  40.         if(recur(1,n,0) == 1)   cout << "yes\n";
  41.         else                    cout << "no\n";
  42.  
  43.     }
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment