#include<iostream> using namespace std; struct treeNode{ int indexI,indexJ; treeNode *Right,*Down; }; treeNode*Try(int i,int j,int n,int x=0); void preorderPrint( treeNode *root ); int**Array; void read(int**A,int n){ for(int i=0;i<n;i++)> for(int j=0;j<n;j++){> cout<<"\nEnter the element["<<i<<"]["<<j<<"]: "; cin>>A[i][j]; } } void main(){ int size; cout<<"\nEnter the size of the array : "; cin>>size; Array=new int*[size]; for(int i=0;i<size;i++)> Array[i]=new int[size]; read(Array,size); treeNode*root=Try(0,0,size); preorderPrint( root ); } treeNode*Try(int i,int j,int n,int x){ treeNode*p=new treeNode; if(Array[i][j]!=-1){ bool b=false; if(x==0){ if((Array[i][j]+j)<n){> p->indexI=i; p->indexJ=j; b=true;} } else{ if((Array[i][j]+i)<n){> p->indexI=i; p->indexJ=j; b=true;} } if(b){ p->Right=Try(i,j+Array[i][j],n,0); p->Down=Try(i+Array[i][j],j,n,x=1); return p; } else return NULL; } } void preorderPrint( treeNode *root ) { // Print all the items in the tree to which root points. // The item in the root is printed first, followed by the // items in the left subtree and then the items in the // right subtree. if ( root != NULL ) { // (Otherwise, there's nothing to print.) cout << root->indexI << " "<<root->Right<<" "; // Print the root item. preorderPrint( root->Down ); // Print items in left subtree. preorderPrint( root->Right ); // Print items in right subtree. } } // end preorderPrint()
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)