您的位置:首页 > 其它

过河卒问题的动态规划求解(分支限界或者回溯过于耗时)

2013-12-25 11:50 543 查看
如图,A 点有一个过河卒,需要走到目标 B 点。卒行走规则:可以向下、或者向右。同时在棋盘上的任一点有一个对方的马(如上图的C点),该马所在的点和所有跳跃一步可达的点称为对方马的控制点。例如上图 C 点上的马可以控制 9 个点(图中的P1,P2 … P8 和 C)。卒不能通过对方马的控制点。

#include <memory.h>#include "OJ.h"

#define MAX_AXIS 21

#define HORSEMAX 9

/*

功能:计算出卒从 A 点(0,0)能够到达 B 点的路径的条数。

输入参数:

int B_axis[2]: B点所在位置的坐标,B_axis[0]:横坐标,B_axis[1]:纵坐标

int C_axis[2]:马所在位置的坐标,C_axis[0]:横坐标,C_axis[1]:纵坐标

输出参数:

long long result: 返回从A点(绕过C点所控制区域)到B点的路径条数

返回值:



*/

void GetResult(int B_axis[2], int C_axis[2], long long & result)

{

long long pathCnt[MAX_AXIS][MAX_AXIS] = {0};

bool availPos[MAX_AXIS][MAX_AXIS];

int horseJump[HORSEMAX][2] = {{2,1},{2,-1},{-2,-1},{-2,1},{1,2},{-1,2},{1,-2},{-1,-2},{0,0}};//马可以到达的范围

for (int i = 0 ; i < HORSEMAX ; i++)

{

if ((horseJump[i][0] + C_axis[0]) < 0 || (horseJump[i][1] + C_axis[1]) < 0)

{

continue;

}

availPos[horseJump[i][0] + C_axis[0]][horseJump[i][1] + C_axis[1]] = false;

}

pathCnt[0][0] = 1;

int i, j;

for (i = 0 ; i <= B_axis[0] ; i++)

{

for (j = 0 ; j <= B_axis[1]; j++)

{

if ((i - 1) < 0 && (j - 1) >= 0 && availPos[i][j]) //沿着水平边线走的话路径只有一条

{

pathCnt[i][j] = pathCnt[i][j - 1];

}

if ((i - 1) >= 0 && (j - 1) < 0 && availPos[i][j])//沿着竖直边线走的话路径只有一条

{

pathCnt[i][j] = pathCnt[i - 1][j];

}

if((i - 1) >= 0 && (j - 1) >= 0 && availPos[i][j]) //一旦离开了水平和竖直边线,路径的条数就是横着过来和竖着过来的和

{

pathCnt[i][j] = pathCnt[i - 1][j] + pathCnt[i][j - 1];

}

}

}

result = pathCnt[B_axis[0]][B_axis[1]];

}

回溯法求解:

public class Demo {

/**

*

* 功能:计算出卒从 A 点(0,0)能够到达 B 点的路径的条数。

* @param B_axis B点所在位置的坐标,B_axis[0]:横坐标,B_axis[1]:纵坐标

* @param C_axis 马所在位置的坐标,C_axis[0]:横坐标,C_axis[1]:纵坐标

* @return 返回从A点(绕过C点所控制区域)到B点的路径条数

*/

class Point

{

Point(int a,int b){x=a;y=b;}

int x;

int y;

}

int traceCount=0;

Point curr;

Point []path;

Point des;

Point horse;

Boolean checkVacancy(Point p)

{

if((p.x==horse.x)&&(p.y==horse.y)) return false;

if((Math.abs(p.x-horse.x)==2)&&(Math.abs(p.y-horse.y)==1)) return false;

if((Math.abs(p.x-horse.x)==1)&&(Math.abs(p.y-horse.y)==2)) return false;

if(p.x>des.x||p.y>des.y) return false;

return true;

}

void stepBack(String dir)

{

if(dir=="left") curr.y--;

if(dir=="up") curr.x--;

}

Boolean Exceed()

{

if(curr.x>des.x)

{

curr.x--;

return true;

}

if(curr.y>des.y)

{

curr.y--;

return true;

}

return false;

}

Boolean stepRight()

{

if(checkVacancy(new Point(curr.x,curr.y+1)))

{

curr.y+=1;

return true;

}

return false;

}

Boolean stepDown()

{

if(checkVacancy(new Point(curr.x+1,curr.y)))

{

curr.x+=1;

return true;

}

return false;

}

void debug()

{

System.out.println("the curr x:"+curr.x+" the y:"+curr.y);

}

void backTrace(int t,int h)

{

//debug();

if((curr.x==des.x)&&(curr.y==des.y))

{

traceCount++;

return; //递归出口

}

if(stepRight())

{

backTrace(t+1,h);

stepBack("left");

}

if(stepDown())

{

backTrace(t,h+1);

stepBack("up");

}

}

public long getResult(int[] B_axis, int[] C_axis) {

// coding

if(B_axis==null||C_axis==null) return 0;

if(B_axis.length!=2||C_axis.length!=2)return 0;

des=new Point(B_axis[0],B_axis[1]);

horse=new Point(C_axis[0],C_axis[1]);

if(des.x<0||des.y<0) return 0;

if((des.x==horse.x)||(des.y==horse.y)) return 0;

curr=new Point(0,0);

traceCount=0;

backTrace(0,0);

return traceCount;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: