fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. // valid transition from x to n[x-'1'][0 or 1]
  5.  
  6. int n[9][2] =
  7. {
  8. {'6','8'},{'7','9'},{'4','8'},
  9. {'3','9'},{ 0 , 0 },{'1','7'},
  10. {'2','6'},{'1','3'},{'2','4'}
  11. };
  12.  
  13. // i is a pointer to where to start on a string
  14.  
  15. bool L(char * i)
  16. {
  17. char c = 0;
  18.  
  19. // move if not \0 and (not-first-char or is a valid move)
  20.  
  21. while(*i && (!c || *i==n[c-'1'][0] || *i==n[c-'1'][1]))
  22. {
  23. c=*i++;
  24. }
  25.  
  26. return !*i; // success if it's \0
  27. }
  28.  
  29. int main()
  30. {
  31. char *falsy = "183492760";
  32. printf("%s ~ %s \n", falsy, L(falsy)?"true":"false");
  33.  
  34. char *truthy = "618349276";
  35. printf("%s ~ %s \n", truthy, L(truthy)?"true":"false");
  36.  
  37. return 0;
  38. }
  39.  
  40.  
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
183492760 ~ false 
618349276 ~ true