fork download
  1. #include<stdio.h>
  2. void dfs(int mat[100][100], int n, int x, int y, int list[2][100], int *size)
  3. {
  4. int i;
  5. if( mat[x][y] == -1)
  6. {
  7. return;
  8. }
  9. if (x== n-1 && y== n-1)
  10. {
  11. for(i=0;i<*size;i++)
  12. {
  13. printf("( %d , %d ) - ", list[0][i], list[1][i]);
  14. }
  15. printf("( 2 , 2 )");
  16. printf("\n");
  17. return;
  18. }
  19. list[0][*size]=x;
  20. list[1][*size]=y;
  21. (*size)++;
  22. if((x+1)<n)
  23. {
  24. dfs(mat,n,x+1,y,list,size);
  25. }
  26. if((y+1)<n)
  27. {
  28. dfs(mat,n,x,y+1,list,size);
  29. }
  30. if((y+1)<n && (x+1)<n )
  31. {
  32. dfs(mat,n,x+1,y+1,list,size);
  33. }
  34. (*size)--;
  35. }
  36. int main()
  37. {
  38. int n,o1,o2,i,j;
  39. printf("Enter the size of the grid\n");
  40. scanf("%d",&n);
  41. if(n<0)
  42. {
  43. printf("Invalid Input");
  44. return 0;
  45. }
  46. int mat[100][100], list[2][100];
  47. int size = 0;
  48. for (i=0;i<n;i++)
  49. {
  50. for(j=0;i<n;i++)
  51. mat[i][j]=0;
  52. }
  53. printf("Enter the grid points that are offsets\n");
  54. while(o1!=-1 && o2!=-1)
  55. {
  56. scanf(" %d %d",&o1,&o2);
  57. mat[o1][o2]=-1;
  58. if(o1<-1 || o2<-1 || o1>=n || o2>=n || (o1==0 && o2==0) || (o1==(n-1) && o2==(n-1)) )
  59. {
  60. printf("Invalid Input");
  61. return 0;
  62. }
  63. }
  64. printf("The paths for the robot are\n");
  65. dfs(mat,n,0,0,list,&size);
  66. return 0;
  67. }
Success #stdin #stdout 0s 10320KB
stdin
3
0 1
-1 -1
stdout
Enter the size of the grid
Enter the grid points that are offsets
The paths for the robot are
( 0 , 0 ) - ( 1 , 0 ) - ( 2 , 0 ) - ( 2 , 1 ) - ( 2 , 2 )
( 0 , 0 ) - ( 1 , 0 ) - ( 1 , 1 ) - ( 2 , 1 ) - ( 2 , 2 )
( 0 , 0 ) - ( 1 , 0 ) - ( 1 , 1 ) - ( 1 , 2 ) - ( 2 , 2 )
( 0 , 0 ) - ( 1 , 0 ) - ( 1 , 1 ) - ( 2 , 2 )
( 0 , 0 ) - ( 1 , 0 ) - ( 2 , 1 ) - ( 2 , 2 )
( 0 , 0 ) - ( 1 , 1 ) - ( 2 , 1 ) - ( 2 , 2 )
( 0 , 0 ) - ( 1 , 1 ) - ( 1 , 2 ) - ( 2 , 2 )
( 0 , 0 ) - ( 1 , 1 ) - ( 2 , 2 )