过河卒问题的动态规划求解(分支限界或者回溯过于耗时)
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;
}
}
#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;
}
}
相关文章推荐
- 0/1背包问题 回溯 分支界限 动态规划
- 五大算法比较-分治、动态规划、回溯、分支限界、贪心算法
- 【算法复习二】传统基本算法(贪心、动态规划、回溯和分支限界)
- 【算法复习二】传统基本算法(贪心、动态规划、回溯和分支限界)
- 分支限界求解货郎担/旅行商问题
- 五大常用经典算法(分治、动态规划、贪心、回溯、分支限界)
- 贪心算法-分治算法-动态规划-回溯-分支限界的简单介绍
- 分支限界求解TSP问题
- 五大常用算法:分治、动态规划、贪心、回溯、分支限界
- 五大常用算法:分治、动态规划、贪心、回溯、分支限界
- 分支限界求解货郎担/旅行商问题
- 动态规划求解合唱队问题的思路
- 物件捆绑 背包问题 动态规划 求解
- 最长公共子序列问题(动态规划求解)
- 0-1背包问题的两种解法(回溯法和动态规划)
- 一个分支限界的旅行商求解
- 回溯法——求解0-1背包问题
- N皇后问题回溯法求解
- 动态规划求解硬币找零问题——Java实现
- 百分求解一个可能涉及比较多的一个有关问题,求熟悉javascript或者ASP无组件上传或者AJAX等等的高手帮忙解答,非常感谢!