2013-10-16 42 views
0

我正在爲學校做一項任務,並且有一些愚蠢的事情讓我無法取得進展。我搜索了SO和網絡的其他部分,無法找到適合我的答案。C++中的類之間的對象聲明

問題是使用堆棧和隊列的Rat-In-A-Maze算法設計問題。我設計了Stack(基於數組)和Queue(鏈表)類,現在我正在設計searchStack()和searchQueue()中的算法。

我的問題是:

1)如何我在ratInMaze類中聲明堆棧或隊列?我包括Stack.h和Queue.h文件,但在嘗試實例化時仍然出現錯誤

2)如何參考我的教授在Main.cpp中爲我的方法中的操作提供的迷宮?我的代碼中的IE我希望它被稱爲地圖[X] [Y],並能夠基於此執行操作。

這裏是我的ratInMaze.cpp:(尚未完成,但足以給上下文)

#include <stdio.h> 
#include <iostream> 
#include <sstream> 
#include <string> 
#include "RatInMaze.h" 
#include "Queue.h" 
#include "Stack.h" 


using namespace std; 



bool RatInMaze::isValidIndex(int x, int y) 
{ 
    // 12 x 14 array 
    if (x < 0 || y < 0 || x > 13 || y >15) 
     return false; 
    return true; 
} 

bool RatInMaze::searchStack(int fromX, int fromY, int toX, int toY) 
{ 

    int backX; 
    int backY; 
    int traveled; 
    int thePath; 

    //Matrix is 12x14, check dimensions 
    if (!isValidIndex(fromX,fromY)) 
    { 
     cout<<"Not valid starting point"<<endl; 
     return false; 
    } 

    else if (!isValidIndex(toX, toY)) 
    { 
     cout<<"Not a valid ending point"; 
      return false; 
    } 

    Stack<int> ratStack(20); //throws error 
    //Create stack? //ATTENTION 
    // Load Map? //ATTENTION 

    //Set map variables 
    int x = fromX; 
    int y = fromY; 
    map[x][y]='S'; //Mark starting point 
    ratStack->push(y); //First coordinates in 
    ratStack->push(x); //the stack are starting points 

    //Load map? Attention 
    while (!(x==toX || y==toY)) 
    { 
     //Try to move right 
     if(isValidIndex(map[x][y+1]) && map[x][y+1] < 1) 
     { 
      //stack->Push coordinates onto stack 
      ratStack->push(y); 
      ratStack->push(x); 
      y=y+1; 
      map[x][y]='R'; 
      traveled++; 
     } 
     //Try to move down 
     else if(isValidIndex(map[x+1][y]) && map[x+1][y] < 1) 
     { 
      ratStack->push(y); 
      ratStack->push(x); 
      x=x+1; 
      map[x][y]='D'; 
      traveled++; 
     } 
     //Try move left 
     else if(isValidIndex(map[x][y-1]) && map[x][y-1] < 1) 
     { 
      ratStack->push(y); 
      ratStack->push(x); 
      y=y-1; 
      map[x][y]='L'; 
      traveled++; 
     } 

     //Try move up 
     else if(isValidIndex(map[x+1][y]) && map[x+1][y] < 1) 
     { 
      ratStack->push(y); 
      ratStack->push(x); 
      x=x-1; 
      map[x][y]='U'; 
      traveled++; 
     } 

     //Else you can't make any move, must retrace steps 
     else 
     { 
      //Pop off last coordinates on stack to get the previous square 
      backX = ratStack->pop(); 
      backY= ratStack->pop(); 

      //If we came from right, go back left, block off this square from revisiting 
      if(map[backX][backY]=='R') 
      { 
       map[backX][backY]=2; 
       y=y-1; 
       traveled--; 
      } 
      //If we came from above, go back up, block off this square from revisiting 
      else if(map[backX][backY]=='D') 
      { 
       map[backX][backY]=2; 
       x=x+1; 
       traveled--; 
      } 
      //If we came from left, go back right if we can, block off this square from revisiting 
      else if(map[backX][backY]=='L') 
      { 
       map[backX][backY]=2; 
       y=y+1; 
       traveled--; 
      } 
      //If we came from below, go back up, block off this square from revisiting 
      else if(map[backX][backY]=='U') 
      { 
       map[backX][backY]=2; 
       x=x+1; 
       traveled--; 
      } 

      //return to top of loop and we are now at the spot we came from before running into jam 
      //try to move right, down, left, up again as usual 
      //if not, we pop off another set of coordinates and repeat until we can move 
     } 


    } //End while loop 

    if (x==toX && y==toY) 
    { 
     cout<<"We found the cheese! "<<"It took us "<<ratStack->size()<<"squares and we traveled through" << traveled<<endl; 
     return true; 
    } 
    else 
     return false; 

} //End method 




/* 
SearchQueue 
"Sprawling rats" 
start at the point, stack->push coordinates into queue stack->push(x) stack->push(y) 
checke every direction possible and change the letter RLUD to indicate where you came from 
and stack->push each one of those pairs to the queue 
When at the end, trace back based on the RLUD until you get to where you started ('4') 
*/ 

bool searchQueue(int fromX, int fromY, int toX, int toY) 
{ 
    int currentX; 
    int currentY; 

    //Matrix is 12x14, check dimensions 
    if (fromX < 0 || fromX >13 || fromY < 0 || fromY >15) 
    { 
     cout<<"Not valid starting point"<<endl; 
     return false; 
    } 

    else if (toX < 0 || toX > 13 || toY < 0 || toY > 15) 
    { 
     cout<<"Not a valid ending point"; 
      return false; 
    } 

    //Valid index, we can proceed 
    //Create Queue, starting points assigned 
    Queue<int> ratQueue; 
    int x = fromX; 
    int y = fromY; 
    myQueue->push(x); 
    myQueue->push(y); 

    bool found = false; 

    traveled=0; 
    while (!found) 
    { 
     currentX=myQueue->pop(); 
     currentY=myQueue->pop(); 

     if (currentX==toX && currentY==toY) 
      { 
       found = true; 
       break; 
      } 

     //Check right move 
     if (map[currentX][currentY+1] < 1) 
     { 
      myQueue->push(x); 
      myQueue->push(y+1); 
      map[x][y+1]='R'; 
      traveled++; 
     } 

     //Check down move 
     if (map[currentX+1][currentY] < 1) 
     { 
      myQueue->push(x+1); 
      myQueue->push(y); 
      map[x+1][y]='D'; 
      traveled++; 
     } 

     //Check left move 
     if (map[currentX][currentY-1] < 1) 
     { 
      myQueue->push(x); 
      myQueue->push(y-1); 
      map[x][y-1]='L'; 
      traveled++; 

     } 

     //Check up move 
     if (map[currentX-1][currentY] < 1) 
     { 
      myQueue->push(x-1); 
      myQueue->push(y); 
      map[x-1][y] = 'U'; 
      traveled++; 
     } 
    } //End while loop 



    //Cheese is now found 

    x=currentX; 
    y=currentY; 
    thePath = 0; 

    while(x == fromX || y == fromY) 
    { 

     //Check if we came from left 
     if (map[x][y]=='R') 
     { 
      map[x][y]=2; 
      y=y-1; 
      thePath++; 
     } 
     //Check if we came from above 
     else if (map[x][y]=='D') 
     { 
      map[x][y]=2; 
      x=x-1; 
      thePath++; 
     } 
     //Check if we came from right 
     else if (map[x][y]=='L') 
     { 
      map[x][y]=2; 
      y=y+1; 
      thePath++; 
     } 
     //Check if we came from below 
     else if (map[x][y]=='U') 
     { 
      map[x][y]=2; 
      x=x+1; 
      thePath++; 
     } 

    } 

    //Clean up array, leaving only 1s for the walls and 2 to show our path 
    for(int i=0; i <13; i++) 
    { 
     for(int j=0; i<15; j++) 
     { 
      if (map[i][j]!=1 && map[i][j]!=2) 
       map[i][j]=0; 
     } 
    } 

    cout<<"Found the cheese! We traveled "<<traveled<<" squares and the path took "<<thePath<<" squares"<<endl; 
    return true; 





} //End searchQueue method 

這裏是我的Stack.cpp:

#include "Stack.h" 
#include <stdio.h> 
#include <iostream> 
#include <sstream> 
#include <string> 

using namespace std; 


template<class T> 
Stack<T>::Stack(int initialCapacity) 
{ 
    stackSize = initialCapacity; 
    emptyStack=-1; 
    stackTop=emptyStack; 
    stack = new int[initialCapacity]; 

} 

template<class T> 
Stack<T>::~Stack() { delete [] stack;} 

template<class T> 
void Stack<T>::push(const int& theElement) 
{ 
    stackTop++; 
    stack[stackTop]=theElement; 
} 

template<class T> 
bool Stack<T>::empty() const 
{ 
    return stackTop==emptyStack; 
} 
template<class T> 
int Stack<T>::size() const 
{ 
    return stackTop+1; 
} 
template<class T> 
int Stack<T>::top() 
{ 
    return stack[stackTop]; 
} 
template<class T> 
void Stack<T>::pop() 
{ 
    stack[stackTop]=NULL; 
    stackTop--; 
} 

這裏是我的Queue.cpp:

#include <stdio.h> 
#include <ostream> 
#include <sstream> 
#include "Queue.h" 
#include <cstddef> 


using namespace std; 

template <class T> 
Queue<T>::Queue() 
{ 
    first,last=NULL; 
} 

template <class T> 
Queue<T>::~Queue() 
{ 

} 

template <class T> 
bool Queue<T>::empty() 
{ 
    return (first==NULL && last==NULL); 
} 

template <class T> 
int Queue<T>::size() 
{ 
    int count=0; 
    tempNode = first; 
    while (tempNode->next !=NULL) 
    { 
     count++; 
     tempNode = tempNode->next; 
    } 

    return count; 
} 

template <class T> 
int& Queue<T>::back() 
{ 
    return first; 
} 

template <class T> 
int& Queue<T>::front() 
{ 
    return last; 
} 

template <class T> 
void Queue<T>::pop() 
{ 
    if (first==NULL) 
     return; 
    else if (last==NULL) 
     return; 
    else 
    { 
    last = last->prev; 
    last->next=NULL; 
    } 
} 

template <class T> 
void Queue<T>::push(T element) 
{ 
    if(first==NULL) 
    { 
     first=new Node<T>(element); 
     last=first; 
     first->next=NULL; 
     first->prev=NULL; 
    } 

    else 
    { 
     tempNode = first; 
     first = new Node<T>(element); 
     first->next = tempNode; 
     tempNode->prev = first; 
    } 

} 

這裏是教授的Main.cpp的:

#include <iostream> 
#include "RatinMaze.h" 
using namespace std; 

void print_header (string h, int fromX,int fromY,int toX,int toY) { 
    cout << h << "from " << "(" << fromY << "," << fromX << ") to (" 
     << toY << "," << toX << "):" << endl; 
} 

int main(){ 

     RatInMaze* rim = new RatInMaze(); 
    char maze[13][15]={ 
     '0','0','0','1','0','0','0','0','0','0','1','0','0','0','0', 
     '0','0','0','1','0','0','1','0','0','0','0','0','0','0','0', 
     '0','0','0','0','0','0','0','1','1','1','1','1','1','1','1', 
     '0','0','0','1','1','1','0','0','1','0','0','1','0','0','0', 
     '0','0','0','0','0','1','1','0','0','1','0','0','1','0','0', 
     '1','1','0','0','0','1','1','0','0','1','0','0','0','0','0', 
     '0','1','1','0','0','1','1','0','0','1','0','0','0','0','0', 
     '0','0','1','0','0','1','1','0','0','1','0','0','0','0','0', 
     '0','1','1','0','0','1','0','0','0','0','0','0','0','0','0', 
     '0','0','1','0','0','0','1','1','1','1','1','1','1','1','1', 
     '0','0','1','0','1','0','0','0','0','0','0','0','0','0','0', 
     '0','0','1','0','1','0','0','0','1','0','0','0','0','0','0', 
     '0','0','0','0','1','0','0','1','0','0','0','0','0','0','0' }; 

    rim->load(maze,13,15); 
    print_header("Queue search ", -1,1,10,10); 
    rim->print(rim->searchQueue(-1,1,10,10)); 

    rim->load(maze,13,15); 
    print_header("Stack search ", 0,0,41,1); 
    rim->print(rim->searchStack(0,0,41,1)); 

    int fromX = 0; 
    int fromY = 7; 
    int toX = 14; 
    int toY = 6; 

    rim->load(maze,13,15); 
    print_header("Rat (stack) searching ",fromX,fromY,toX,toY); 
    rim->print(rim->searchStack(fromX,fromY,toX,toY)); 

    rim->load(maze,13,15); 
    print_header("Multiple rats searching ",fromX,fromY,toX,toY); 
    rim->print(rim->searchQueue(fromX,fromY,toX,toY)); 

    rim->load(maze,13,15); 
    print_header("Smart rat searching ",fromX,fromY,toX,toY); 
    rim->print(rim->searchStackSmart(fromX,fromY,toX,toY)); 
} 

我是新來的語言,我敢肯定有很多錯誤的東西不是我所問的;我感謝任何建設性的反饋,並會努力解決您真正需要的任何內容。提前致謝!

編輯:添加Stack.h和Queue.h澄清

Queue.h:

#ifndef QUEUE_H 
#define QUEUE_H 

#include <cstddef> 
#include <stdio.h> 
#include <iostream> 
#include <sstream> 
#include <string> 


//Create node struct to implement from linked list 

template<class T> 
struct Node 
{ 
    T data; 
    Node* next; 
    Node* prev; 
    Node(T d, Node* n=NULL): data(d), next(n){} 
}; 


template<class T> 
class Queue 
{ 
    private: 
     Node<T> *first; 
     Node<T> *last; 
     Node<T> *tempNode; 
     Node<T> *deleteNode; 
    public: 
     Queue(); 
     ~Queue(); 
     bool empty(); 
     int size(); 
     int& front(); 
     int& back(); 
     void pop(); 
     void show(); 
     void push (T data); 



}; //End of class queue 

#endif 

Stack.h:

#ifndef STACK_H 
#define STACK_H 

#include <stdio.h> 
#include <iostream> 
#include <sstream> 
#include <string> 

template<class T> 
class Stack 
{ 
    int stackTop;  //Top of stack 
    int stackSize;  //Total stack size 
    int emptyStack;  //emptyStack 
    T* ratStack;   //A stack 


    public: 
       Stack(int initialCapacity);  //Constructor 
       ~Stack();       //Destructor 
     bool empty() const;      //Returns true if stack is empty 
     int size() const;      //Returns size of stack 
     int top();       //Returns top of stack 
     void pop();       //Deletes top element 
     void push(const int& theElement); //Puts theElement at the top 





}; //End stack class 

#endif 
+0

你沒有在'ratInMaze.cpp'中包含'Stack.h' – Kunal

+0

@Kunal我在ratInMaze.h中包含了Stack.h .....不應該這樣做嗎? – Ladybro

+0

是的,應該這樣做。沒有注意到這一點。 – Kunal

回答

0

1)他們是模板類,因此編譯器在使用特定模板參數實例化新類時看不到定義,因爲它們位於cpp文件中。您應該在頭文件中定義堆棧和隊列的方法。 //還有其他方法,但這是最簡單的解決方案;)

2)您應該提供加載迷宮,稱爲加載和打印的方法,就是這樣。

0

快速觀察:堆棧和隊列的pop()成員函數不應該返回一個元素嗎?您的searchQueue和searchQueue似乎期待它。