Advertisement
Diogo03

Davi

Nov 14th, 2024
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.80 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define x first
  4. #define y second
  5.  
  6. int main() {
  7.  
  8.     ios_base::sync_with_stdio(false);
  9.     cin.tie(nullptr);
  10.  
  11.     int n;
  12.     string s;
  13.     cin >> n >> s;
  14.     int teclado[5][4];
  15.     pair<int, int> vetor[10][10], pos[10];
  16.  
  17.     for (int i = 0; i < 5; i++)
  18.         for (int j = 0; j < 4; j++)
  19.             teclado[i][j] = -1;
  20.  
  21.     teclado[1][1] = 1;
  22.     teclado[1][2] = 2;
  23.     teclado[1][3] = 3;
  24.     teclado[2][1] = 4;
  25.     teclado[2][2] = 5;
  26.     teclado[2][3] = 6;
  27.     teclado[3][1] = 7;
  28.     teclado[3][2] = 8;
  29.     teclado[3][3] = 9;
  30.     teclado[4][2] = 0;
  31.  
  32.     for (int i = 0; i <= 9; i++) {
  33.         for (int j = 1; j < 5; j++) {
  34.             for (int k = 1; k < 4; k++) {
  35.                 if (teclado[j][k] == i) pos[i] = {j, k};
  36.             }
  37.         }
  38.     }
  39.  
  40.     for (int i = 0; i <= 9; i++) {
  41.         for (int j = 0; j <= 9; j++) {
  42.             vetor[i][j] = {pos[i].x - pos[j].x, pos[i].y - pos[j].y};
  43.         }
  44.     }
  45.  
  46.     for (int i = 0; i <= 9; i++) {
  47.         if (i != s[0] - '0') {
  48.             string c = "";
  49.             c += i + '0';
  50.             auto p = pos[i];
  51.  
  52.             bool valid = true;
  53.             for (int j = 0; j < n - 1; j++) {
  54.                 p.x += vetor[s[j + 1] - '0'][s[j] - '0'].x;
  55.                 p.y += vetor[s[j + 1] - '0'][s[j] - '0'].y;
  56.  
  57.                 if (1 <= p.x && p.x <= 4 && 1 <= p.y && p.y <= 3 && teclado[p.x][p.y] != -1) {
  58.                     c += teclado[p.x][p.y] + '0';
  59.                 } else {
  60.                     valid = false;
  61.                     break;
  62.                 }
  63.             }
  64.            
  65.             if (valid && c.length() == n) {
  66.                 cout << "NO" << endl;
  67.                 return 0;
  68.             }
  69.         }
  70.     }
  71.  
  72.     cout << "YES" << endl;
  73.     return 0;
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement