少种走法,打印每一种走法。
/*==========================================================*\ 给二维数组,存(0,0) -> (m, n)个点,就是一个m * n 的网格, 从左上角(0,0)走到右下角(m,n)只能向右走或者向下走,有多 少种走法,打印每一种走法。\*==========================================================*/#include#include using namespace std; struct position{ int x; int y;};position *stepX(position *pCurrent){ position *newStep = new position(); newStep->x = pCurrent->x + 1; newStep->y = pCurrent->y; return newStep;}position *stepY(position *pCurrent){ position *newStep = new position(); newStep->x = pCurrent->x; newStep->y = pCurrent->y + 1; return newStep;}void path(int widthM,int heightN,deque &dq){ int endM = widthM - 1; int endN = heightN - 1; if(dq.back()->x == endM && dq.back()->y == endN){ deque ::iterator it = dq.begin(); while (it != dq.end()){ cout << "[" << (*it)->x << "," << (*it)->y << "]" << " "; it++; } cout << endl; return ; }else if(dq.back()->y > endN){ return ; }else if(dq.back()->x > endM){ return ; }else{ position *newStep = stepX(dq.back()); dq.push_back(newStep); path(widthM,heightN,dq); dq.pop_back(); newStep = stepY(dq.back()); dq.push_back(newStep); path(widthM,heightN,dq); dq.pop_back(); }}int main(){ deque dq; position *newStep = new position; newStep->x = 0; newStep->y = 0; dq.push_back(newStep); path(2,3,dq); system("pause"); return 1;}