fork(2) download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 3, M = 3, L = 4;
  5. int S[4] = {1,3,4,6};
  6. int A[N+2][M+2] = {
  7. {0,0,0,0,0},
  8. {0,1,2,3,0},
  9. {0,3,4,5,0},
  10. {0,5,6,7,0},
  11. {0,0,0,0,0}
  12. };
  13. int V[N+2][M+2] = {0};
  14. int dx[4] = {1,-1,0,0};
  15. int dy[4] = {0,0,1,-1};
  16.  
  17. void DFS(int x, int y, int c){
  18. V[x][y] = 1;
  19.  
  20. if(c == L-1){
  21. cout << "FOUND" << endl;
  22. return;
  23. }
  24. for(int i=0; i<4;i++){
  25. int nx = x+dx[i], ny = y+dy[i];
  26. if(!V[nx][ny] && A[nx][ny] == S[c+1]) DFS(nx,ny, c+1);
  27. }
  28. }
  29.  
  30.  
  31. int main() {
  32. for(int i=1; i<=N;i++){
  33. for(int j=1; j<=M;j++){
  34. if(!V[i][j] && A[i][j] == S[0]) DFS(i,j,0);
  35. }
  36. }
  37. return 0;
  38. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
1 1 0
2 1 1
2 2 2
3 2 3
FOUND