一个关于回溯的算法题

jieguo 2005-12-04 03:54:11
各位关于《计算机算法基础》回溯那章里有个跳牌的问题,请教一下,要能运行得出来才好
因为我的花时间太长,所以不能得到答案。原问题如下
有33格子的盘子,只有中心格空着,规定当一张牌跳过邻近一张牌到空格子时,就将这张邻近的牌从盘上拿掉,写个算法来找出一些列跳步,使出来最后一张在中心格的牌外其余的牌都被拿掉。
格子和牌如下(我用0,1来表示了,0表示无牌)
111
111
1111111
1110111
1111111
111
111
其余的空的位置为不合法位置。
...全文
842 38 打赏 收藏 转发到动态 举报
写回复
用AI写文章
38 条回复
切换为时间正序
请发表友善的回复…
发表回复
bohlee 2006-03-07
  • 打赏
  • 举报
回复
mark
ox_thedarkness 2006-01-07
  • 打赏
  • 举报
回复
正确移动过程保存在log.log中...
期待更高效的算法
ox_thedarkness 2006-01-07
  • 打赏
  • 举报
回复
orz...
对不起阿,偶的错.......你那个是算出来了,才非法关闭的...
因为刚才匆忙在末尾加了一句,但是没完整测试

for( int i = 0; paceStack[i]; i++ ) delete paceStack[i];

因为忘了初始化数组为0,(orz....) 所以一旦算出结果,运行到这里就会试图删除非法地址....

错误修正了. 加了简单的进度显示. 另外强烈建议在release模式发布,比debug快一倍(在我的机器上40秒...还是挺慢的.期待 bigpin(bigpin) 的代码):

//==========================================================
// jump_chess.h
//==========================================================
// last updata ox_thedarkness 2006,1,7
//
#ifndef JUMP_CHESS_H_
#define JUMP_CHESS_H_
#include <iostream>
#include <vector>

//==========================================================
struct Pace{
int _line, _col;
enum Dir{
d_Up, d_Down, d_Left, d_Right,
}_dir;
Pace( int line, int col, Dir dir ):_line(line), _col(col), _dir(dir){};
};

//==========================================================
// A 7x7 JumpChess, with emptyblock, cardblock,
// and some blocks were prohibit to place card.
//
class JumpChess{
public:
enum State{
s_Empty = 0,
s_Card = 1,
s_Prohibit = 10,
};

// Constructors
JumpChess(){};
JumpChess( const State* psInit );
JumpChess( const JumpChess& );

// Stream out
friend std::ostream& operator<<( std::ostream&, const JumpChess& );

// Control
std::vector< Pace >* getAviPaces();
void doPace( const Pace& );

// Test func
bool isEq( const JumpChess& ); //compare two chess state
private:
State _arChess[7][7];
};


#endif //JUMP_CHESS_H_

//==========================================================
// jump_chess.cpp
//==========================================================
// last updata ox_thedarkness 2006,1,7
//
//#include <jump_chess.h>
#include <string.h>
#include <iostream>
#include <assert.h>

//==========================================================
// Constructors
//------------------------------------------------
JumpChess::JumpChess( const JumpChess::State* sInit ){
memcpy( _arChess, sInit, sizeof(_arChess));
};

//------------------------------------------------
JumpChess::JumpChess( const JumpChess& jc ){
memcpy( _arChess, jc._arChess, sizeof(_arChess));
};

//==========================================================
// Test funcs
//------------------------------------------------
bool JumpChess::isEq( const JumpChess& jc ){
return memcmp( _arChess, jc._arChess, sizeof( _arChess ) )?false:true;
}

//==========================================================
// Overload operator<<
//------------------------------------------------
std::ostream& operator<<( std::ostream& out, const JumpChess& jc ){
for( int i = 0; i < 7; i++ ){
for( int j = 0; j < 7; j++ ){
if( jc._arChess[i][j] == JumpChess::s_Prohibit ) out<<' '<<' ';
else out<<jc._arChess[i][j]<<' ';
}
out<<std::endl;
}
return out;
}


//==========================================================
// Control func
//------------------------------------------------
// get all aviliable paces in a vector
std::vector< Pace >* JumpChess::getAviPaces(){
std::vector< Pace >* vPaces = new std::vector< Pace >;

for( int i = 0; i < 7; i++ ){
for( int j = 0; j < 7; j++ ){
// Find a card;
if( _arChess[i][j] != s_Card ) continue;

// Check 4 dirs:
// d_Up, d_Down, d_Left, d_Right
static int xInc[] = { 0, 0, -1, 1 };
static int yInc[] = { -1, 1, 0, 0 };
for( int dir = 0; dir < 4; dir ++ ){
// Must next to another card:
int x = j+ xInc[ dir ];
int y = i+ yInc[ dir ];
if( x < 0 || x >= 7 || y < 0 || y >=7 || _arChess[y][x] != s_Card ) continue;

// And can jump to a empty block:
x += xInc[ dir ];
y += yInc[ dir ];
if( x < 0 || x >= 7 || y < 0 || y >=7 || _arChess[y][x] != s_Empty ) continue;

// Push to vPaces
vPaces->push_back( Pace( i, j, (Pace::Dir) dir ) );
}
}
}
return vPaces;
};

// Apply a pace
void JumpChess::doPace( const Pace& p ){
int x = p._col;
int y = p._line;
int dir = p._dir;
static int xInc[] = { 0, 0, -1, 1 };
static int yInc[] = { -1, 1, 0, 0 };

assert( _arChess[y][x] == s_Card );
_arChess[y][x] = s_Empty;
x += xInc[dir];
y += yInc[dir];
assert( _arChess[y][x] == s_Card );
_arChess[y][x] = s_Empty;
x += xInc[dir];
y += yInc[dir];
assert( _arChess[y][x] == s_Empty );
_arChess[y][x] = s_Card;
};

//==========================================================
// JumpChessWorkout.cpp
//==========================================================
// last updata ox_thedarkness 2006,1,7
//
#include <iostream>
#include <fstream>
#include <vector>
//#include <jump_chess.h>

std::ofstream myLog( "log.log" );

int main(){
using namespace std;

//------------------------------------------------------
// define chess state shorthand
JumpChess::State O = JumpChess::State::s_Empty; // letter 'o' uppercase ( resembling 0 )
JumpChess::State I = JumpChess::State::s_Card; // letter 'I' ( resembling 1 )
JumpChess::State P = JumpChess::State::s_Prohibit;// letter 'P' ( well, Prohibit )

// init & end states
static JumpChess::State jcsStartInit [] =
{ P, P, I, I, I, P, P,
P, P, I, I, I, P, P,
I, I, I, I, I, I, I,
I, I, I, O, I, I, I,
I, I, I, I, I, I, I,
P, P, I, I, I, P, P,
P, P, I, I, I, P, P,};

static JumpChess::State jcsEndInit[] =
{ P, P, O, O, O, P, P,
P, P, O, O, O, P, P,
O, O, O, O, O, O, O,
O, O, O, I, O, O, O,
O, O, O, O, O, O, O,
P, P, O, O, O, P, P,
P, P, O, O, O, P, P,};

JumpChess jcStart( jcsStartInit );
JumpChess jcEnd( jcsEndInit );

// Current jc & pace
JumpChess jcStack[ 50 ]; // current jumpchess
vector<Pace>* paceStack[ 50 ]; // current aviliable paces
memset( paceStack, 0, sizeof(paceStack) );
int iPaceStack[ 50 ]; // current index of paces


// Push the first jumpchess
int level = 0;
jcStack[0] = jcStart;
jcStack[49] = jcEnd;
paceStack[0] = jcStack[ 0 ].getAviPaces();
iPaceStack[0] = 0;

cout<<"Start:"<<endl;
int topLevel = 0; // For output only
while( level>=0 && ! jcStack[ level ].isEq( jcEnd ) ){
// First get next pacelist & paceId
vector<Pace>& paceVecNow = *(paceStack[ level ]);
int paceIdNow = iPaceStack[ level ]++;

// If no pace left, backdate
if( paceIdNow >= paceVecNow.size() ){
static bottomLevel = 50;
delete paceStack[ level ];
paceStack[ level-- ] = 0;

if( topLevel == 30 && level == 17 )
cout<<"30["<<iPaceStack[ 16 ]
<<'.'<<iPaceStack[ 17 ]<<'/'
<< (*paceStack[ 16 ]).size()<<"]/31"<<endl;
continue;
}

// Get next pace & step in
Pace& PaceNow = paceVecNow[ paceIdNow ];
level++;
jcStack[ level ] = jcStack[ level-1 ];
jcStack[ level ].doPace( PaceNow );
// init pace list of current level
paceStack[ level ] = jcStack[ level ].getAviPaces();
iPaceStack[ level ] = 0;

if( level > topLevel ) {
topLevel = level;
cout<<topLevel<<"/31"<<endl;
}
}
// delete paceStack
for( int i = 0; paceStack[i]; i++ ) delete paceStack[i];

// Out put data
cout<<"Result: "<<endl;
cout<<jcStack[level]<<endl;
if( level >= 0 ){
for( int i = 0; i <= level; i ++ )
myLog<<jcStack[i]<<endl;
}
cout<<"All steps saved in \"log.log\""<<endl;

return 0;
}
jieguo 2006-01-07
  • 打赏
  • 举报
回复
反而非正常关闭了
jieguo 2006-01-07
  • 打赏
  • 举报
回复
我怎么运行了一分多还没运行出来
jieguo 2006-01-07
  • 打赏
  • 举报
回复
弟兄们试出来了么
ox_thedarkness 2006-01-07
  • 打赏
  • 举报
回复
汗。。出了点小问题。。
上面的找到这一段,把!=换成 ==
assert( _arChess[y][x] != s_Card );
_arChess[y][x] = s_Empty;
x += xInc[dir];
y += yInc[dir];
assert( _arChess[y][x] != s_Empty );

另外,原本是3个文件贴一块了。如果要在一个文件中编译, 请吧两处
#include "jump_chess.h"
注释掉
ox_thedarkness 2006-01-07
  • 打赏
  • 举报
回复
//=============================================================
// jump_chess.h
//=============================================================
// last updata ox_thedarkness 2006,1,7
//
#ifndef JUMP_CHESS_H_
#define JUMP_CHESS_H_
#include <iostream>
#include <vector>

//=============================================================
struct Pace{
int _line, _col;
enum Dir{
d_Up, d_Down, d_Left, d_Right,
}_dir;
Pace( int line, int col, Dir dir ):_line(line), _col(col), _dir(dir){};
};

//=============================================================
// A 7x7 JumpChess, with emptyblock, cardblock,
// and some blocks were prohibit to place card.
//
class JumpChess{
public:
enum State{
s_Empty = 0,
s_Card = 1,
s_Prohibit = 10,
};

// Constructors
JumpChess(){};
JumpChess( const State* psInit );
JumpChess( const JumpChess& );

// Stream out
friend std::ostream& operator<<( std::ostream&, const JumpChess& );

// Control
std::vector< Pace >* getAviPaces();
void doPace( const Pace& );

// Test func
bool isEq( const JumpChess& ); //compare two chess state
private:
State _arChess[7][7];
};


#endif //JUMP_CHESS_H_

//=============================================================
// jump_chess.cpp
//=============================================================
// last updata ox_thedarkness 2006,1,7
//
#include "jump_chess.h"
#include <string.h>
#include <iostream>
#include <assert.h>

//=============================================================
// Constructors
//------------------------------------------------
JumpChess::JumpChess( const JumpChess::State* sInit ){
memcpy( _arChess, sInit, sizeof(_arChess));
};

//------------------------------------------------
JumpChess::JumpChess( const JumpChess& jc ){
memcpy( _arChess, jc._arChess, sizeof(_arChess));
};

//=============================================================
// Test funcs
//------------------------------------------------
bool JumpChess::isEq( const JumpChess& jc ){
return memcmp( _arChess, jc._arChess, sizeof( _arChess ) )?false:true;
}

//=============================================================
// Overload operator<<
//------------------------------------------------
std::ostream& operator<<( std::ostream& out, const JumpChess& jc ){
for( int i = 0; i < 7; i++ ){
for( int j = 0; j < 7; j++ ){
if( jc._arChess[i][j] == JumpChess::s_Prohibit ) out<<' '<<' ';
else out<<jc._arChess[i][j]<<' ';
}
out<<std::endl;
}
return out;
}


//=============================================================
// Control func
//------------------------------------------------
// get all aviliable paces in a vector
std::vector< Pace >* JumpChess::getAviPaces(){
std::vector< Pace >* vPaces = new std::vector< Pace >;

for( int i = 0; i < 7; i++ ){
for( int j = 0; j < 7; j++ ){
// Find a card;
if( _arChess[i][j] != s_Card ) continue;

// Check 4 dirs:
// d_Up, d_Down, d_Left, d_Right
static int xInc[] = { 0, 0, -1, 1 };
static int yInc[] = { -1, 1, 0, 0 };
for( int dir = 0; dir < 4; dir ++ ){
// Must next to another card:
int x = j+ xInc[ dir ];
int y = i+ yInc[ dir ];
if( x < 0 || x >= 7 || y < 0 || y >=7 || _arChess[y][x] != s_Card ) continue;

// And can jump to a empty block:
x += xInc[ dir ];
y += yInc[ dir ];
if( x < 0 || x >= 7 || y < 0 || y >=7 || _arChess[y][x] != s_Empty ) continue;

// Push to vPaces
vPaces->push_back( Pace( i, j, (Pace::Dir) dir ) );
}
}
}
return vPaces;
};

// Apply a pace
void JumpChess::doPace( const Pace& p ){
int x = p._col;
int y = p._line;
int dir = p._dir;
static int xInc[] = { 0, 0, -1, 1 };
static int yInc[] = { -1, 1, 0, 0 };

assert( _arChess[y][x] == s_Card );
_arChess[y][x] = s_Empty;
x += xInc[dir];
y += yInc[dir];
assert( _arChess[y][x] != s_Card );
_arChess[y][x] = s_Empty;
x += xInc[dir];
y += yInc[dir];
assert( _arChess[y][x] != s_Empty );
_arChess[y][x] = s_Card;
};
//=============================================================
// JumpChessWorkout.cpp
//=============================================================
// last updata ox_thedarkness 2006,1,7
//
#include "jump_chess.h"
#include <iostream>
#include <fstream>
#include <vector>

std::ofstream myLog( "log.log" );

int main(){
using namespace std;

//------------------------------------------------------
// define chess state shorthand
JumpChess::State O = JumpChess::State::s_Empty; // letter 'o' uppercase ( resembling 0 )
JumpChess::State I = JumpChess::State::s_Card; // letter 'I' ( resembling 1 )
JumpChess::State P = JumpChess::State::s_Prohibit;// letter 'P' ( well, Prohibit )

// init & end states
static JumpChess::State jcsStartInit [] =
{ P, P, I, I, I, P, P,
P, P, I, I, I, P, P,
I, I, I, I, I, I, I,
I, I, I, O, I, I, I,
I, I, I, I, I, I, I,
P, P, I, I, I, P, P,
P, P, I, I, I, P, P,};

static JumpChess::State jcsEndInit[] =
{ P, P, O, O, O, P, P,
P, P, O, O, O, P, P,
O, O, O, O, O, O, O,
O, O, O, I, O, O, O,
O, O, O, O, O, O, O,
P, P, O, O, O, P, P,
P, P, O, O, O, P, P,};

JumpChess jcStart( jcsStartInit );
JumpChess jcEnd( jcsEndInit );

// Current jc & pace
JumpChess jcStack[ 50 ]; // current jumpchess
vector<Pace>* paceStack[ 50 ]; // current aviliable paces
int iPaceStack[ 50 ]; // current index of paces

// Push the first jumpchess
int level = 0;
jcStack[0] = jcStart;
jcStack[49] = jcEnd;
paceStack[0] = jcStack[ 0 ].getAviPaces();
iPaceStack[0] = 0;


while( level>=0 && ! jcStack[ level ].isEq( jcEnd ) ){
// First get next pacelist & paceId
vector<Pace>& paceVecNow = *(paceStack[ level ]);
int paceIdNow = iPaceStack[ level ]++;

// If no pace left, backdate
if( paceIdNow >= paceVecNow.size() ){
delete paceStack[ level ];
paceStack[ level-- ] = 0;
continue;
}

// Get next pace & step in
Pace& PaceNow = paceVecNow[ paceIdNow ];
level++;
jcStack[ level ] = jcStack[ level-1 ];
jcStack[ level ].doPace( PaceNow );
// init pace list of current level
paceStack[ level ] = jcStack[ level ].getAviPaces();
iPaceStack[ level ] = 0;
}
// delete paceStack
for( int i = 0; paceStack[i]; i++ ) delete paceStack[i];

// Out put data
cout<<level<<endl;
cout<<jcStack[level]<<endl;
if( level >= 0 ){
for( int i = 0; i <= level; i ++ )
myLog<<jcStack[i]<<endl;
}

return 0;
}
ox_thedarkness 2006-01-07
  • 打赏
  • 举报
回复
呼。。。搞定了。。。 CPU466, VC6, release模式, 41秒,深度优先暴力遍历解出来了
偶整理一下代码再贴
Dev 2006-01-07
  • 打赏
  • 举报
回复
我也写了一个,纯回溯的时间非常久,正在思考和寻找新思路

^_^
ox_thedarkness 2006-01-07
  • 打赏
  • 举报
回复
问一下,是这样的么?
◆是牌,中间那个◇是空格,其他地方不能走。
跳的时候,只能隔一个格子,并且把那跳过的提掉?

  ◆◆◆  
  ◆◆◆  
◆◆◆◆◆◆◆
◆◆◆◇◆◆◆
◆◆◆◆◆◆◆
  ◆◆◆  
  ◆◆◆  
最后要变成:

  ◇◇◇  
  ◇◇◇  
◇◇◇◇◇◇◇
◇◇◇◆◇◇◇
◇◇◇◇◇◇◇
  ◇◇◇  
  ◇◇◇  

呼。。好像很好玩又很麻烦。。。
jieguo 2006-01-01
  • 打赏
  • 举报
回复
对阿,说出来给大家参考参考阿,
sanhill 2005-12-30
  • 打赏
  • 举报
回复
能将代码贴出来看看么?
bigpin 2005-12-30
  • 打赏
  • 举报
回复
回朔是对的,得出一个答案来,时间并不长,关键算法的内部的顺序,我原来写过一个这个,如果顺利只需0。2秒,就算不好也就不到5秒
wzjall 2005-12-24
  • 打赏
  • 举报
回复
mark
等待新的思路
jieguo 2005-12-23
  • 打赏
  • 举报
回复
做标记我也想过阿,但是给哪些状态做标记呢,每一步都是到不能再进行的地方才知道下一步不能进行下去啊,难道能提前几个状态知道他是不可达的,如果不能,这个标记也就没法记阿
zcz0918 2005-12-23
  • 打赏
  • 举报
回复
mark
sanhill 2005-12-18
  • 打赏
  • 举报
回复
还是要好好研究算法!
你的代码太长了!
现在还是要先搞出结果才行。
jieguo 2005-12-18
  • 打赏
  • 举报
回复
// gridCards.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

const int N=7;
struct node
{
int valid;//是否合法,如果是1表示合法位置
int yes;//是否有牌,1表示有牌,0表示没有
public:
node(int v=0 ,int y=1):valid(v),yes(y)
{}
};

struct ordinate
{
int x;
int y;
ordinate(int xv=0,int yv=0):x(xv),y(yv) {}
};


class GridCards
{
vector< vector<node> > grids; //邻接矩阵,其值为0和1,1表示格子上有牌

vector<ordinate> states; //记录跳格的过程

public:
GridCards();
virtual ~GridCards(){grids.clear(); states.clear();}

void printArray();
void printStates();

void skip(int direction ,int x,int y,int num);
void enumerate();

};


void GridCards::printStates()
{

cout<<states.size()<<endl;
for (int i=0;i<states.size();i++)
cout<<states[i].x<<":"<<states[i].y <<endl;
cout<<endl;
}

void GridCards::enumerate()
{
//只能从空格附近的某个位置开始跳
int i,j;
bool flag=false;
for (i=0;i<grids.size();i++)
{
for (j=0;j<grids.size();j++)
if ( (grids[i][j].valid) && (! grids[i][j].yes))
{
flag=true;
break;
}
if (flag)
break;
}
//找与这个空格相差一个格并且合法的位置上的子

int initX=N/2-2;
int initY=N/2;

grids[initX+1][initY].yes=0;
grids[initX][initY].yes=0;
grids[initX+2][initY].yes =1;
skip(3,initX,initY,1);//从1,3开始跳


}
void GridCards::skip(int direction,int x,int y,int num)
{
//direction表示跳格的方向,1,向上 2:向左,3,向下 4:向右
//x,y表示当前要跳格的牌的坐标

int midX,midY,destX,destY;
switch(direction)
{
case 1:
midX=x-1;
midY=y;
destX=x-2;
destY=y;
break;
case 2:
midX=x;
midY=y-1;
destX=x;
destY=y-2;
break;
case 3:
midX=x+1;
midY=y;
destX=x+2;
destY=y;
break;
case 4:
midX=x;
midY=y+1;
destX=x;
destY=y+2;

}
grids[midX][midY].yes=0;
grids[x][y].yes=0;
grids[destX][destY].yes =1;

ordinate state(x,y);
states.push_back(state);

cout<<states.size()<<endl; cout<<x<<"\t"<<y<<endl; printArray();


if ((num==31) && (grids[N/2][N/2].yes))
{
printStates();
return;
}


for (int i=0;i<grids.size();i++)
{
for (int j=0;j<grids.size();j++)
{
if ( grids[i][j].valid && (grids[i][j].yes ) ) //如果这一格合法,且有牌
{




if ( (i-2)>0 && (grids[i-2][j].valid) && (! grids[i-2][j].yes) && grids[i-1][j].yes )
//如果i-2的格子是合法的且无牌,而且i-1的格子上有牌
{

skip(1,i,j,num+1);

}
if ( (i+2)<N && grids[i+2][j].valid && (! grids[i+2][j].yes) && grids[i+1][j].yes )
{
skip(3,i,j,num+1);

}
if ( (j+2)<N && grids[i][j+2].valid && ! grids[i][j+2].yes && grids[i][j+1].yes)
{

skip(4,i,j,num+1);

}
if ( (j-2)<N && grids[i][j-2].valid && (! grids[i][j-2].yes) && grids[i][j-1].yes )// (j+2)!=states[states.size()-1].y )
{

skip(2,i,j,num+1);
}

}

}
}
grids[midX][midY].yes=1;
grids[x][y].yes=1;
grids[destX][destY].yes =0;
states.pop_back();

}
/*void GridCards::skip(int direction,int x,int y)
{
//direction表示跳格的方向,1,向上 2:向左,3,向下 4:向右
//x,y表示当前要跳格的牌的坐标

int midX,midY,destX,destY;
switch(direction)
{
case 1:
midX=x-1;
midY=y;
destX=x-2;
destY=y;
break;
case 2:
midX=x;
midY=y-1;
destX=x;
destY=y-2;
break;
case 3:
midX=x+1;
midY=y;
destX=x+2;
destY=y;
break;
case 4:
midX=x;
midY=y+1;
destX=x;
destY=y+2;

}
grids[midX][midY].yes=0;
grids[x][y].yes=0;
grids[destX][destY].yes =1;

ordinate state(x,y);
states.push_back(state);

cout<<states.size()<<endl;
cout<<x<<"\t"<<y<<endl;
printArray();

int count=0;

for (int i=0;i<grids.size();i++)
{
for (int j=0;j<grids.size();j++)
if ((grids[i][j].valid ) && (grids[i][j].yes) )
count++;
}
if ( (count==1) && (grids[N/2][N/2].yes) )
{
printStates();
cout<<"successful!"<<endl;
return ;
}


for (int i=0;i<grids.size();i++)
{
for (int j=0;j<grids.size();j++)
{
if ( grids[i][j].valid && (grids[i][j].yes ) ) //如果这一格合法,且有牌
{


bool flag=true;
for (int k1=0;k1<N;k1++)
{
if (grids[i][k1].yes || grids[k1][j].yes )
{
flag=false;
break;
}

}
for (int k1=0;k1<N;k1++)
for (int k2=0;k2<N;k2++)
{
if (abs(i-k1)==abs(j-k2) && grids[k1][k2].yes )
{
flag=false;
break;
}
}
if (flag) return;

if ( (i-2)>0 && grids[i-2][j].valid && (! grids[i-2][j].yes) && grids[i-1][j].yes )
//如果i-2的格子是合法的且无牌,而且i-1的格子上有牌
{

skip(1,i,j);
grids[midX][midY].yes=1;
grids[x][y].yes=1;
grids[destX][destY].yes =0;
states.pop_back();

}
if ( (i+2)<N && grids[i+2][j].valid && (! grids[i+2][j].yes) && grids[i+1][j].yes )
{
skip(3,i,j);

grids[midX][midY].yes=1;
grids[x][y].yes=1;
grids[destX][destY].yes =0;
states.pop_back();

}
if ( (j+2)<N && grids[i][j+2].valid && ! grids[i][j+2].yes && grids[i][j+1].yes)
{

skip(4,i,j);
grids[midX][midY].yes=1;
grids[x][y].yes=1;
grids[destX][destY].yes =0;
states.pop_back();

}
if ( (j-2)<N && grids[i][j-2].valid && (! grids[i][j-2].yes) && grids[i][j-1].yes )// (j+2)!=states[states.size()-1].y )
{

skip(2,i,j);
grids[midX][midY].yes=1;
grids[x][y].yes=1;
grids[destX][destY].yes =0;
states.pop_back();
}

}

}
}


}
*/
GridCards::GridCards()
{

for (int i=0;i<N;i++)
{
vector<node> tmp;
for (int j=0;j<N;j++)
{
node newNode(0,0);
tmp.push_back(newNode);
}

grids.push_back(tmp);
}
for (int i=2;i<=4;i++)
for (int j=0;j<N;j++)
{
grids[i][j].valid=1;
grids[i][j].yes =1;
}
for (int j=2;j<=4;j++)
for (int i=0;i<N;i++)
{
grids[i][j].valid=1;
grids[i][j].yes =1;

}
grids[N/2][N/2].yes=0;//中间位置无牌

printArray();
}

void GridCards::printArray()
{
for (int i=0;i<N;i++)
{
for (int j=0;j<N;j++)
{
cout<<grids[i][j].valid <<",";
cout<<grids[i][j].yes<<"\t";


}
cout<<endl;
}
}

int main()
{
GridCards gc;
gc.enumerate();

return 0;
}
这个程序是我的递归算法,我把前面的结果输出来看,转换过程是正确的,但是最后答案算不出来,因为时间太长了,大家看看
csucdl 2005-12-17
  • 打赏
  • 举报
回复
高手安在?
加载更多回复(18)

3,882

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧