Algorithm 对于A*算法,这是一个不错的启发式函数吗?为什么?
15-解释难题: 为了解决15个难题,我编写了一个启发式函数来决定哪种状态最适合访问。因此,我编写了一个名为update fgh的启发式函数,对于每个节点,它将g增加1,h相对于每个节点到目标状态的距离增加 15拼图问题是一个由15块瓷砖组成的随机数组,每个瓷砖编号为1-15,以随机顺序分散在一个4 x 4的盒子上,同时添加一块空白瓷砖Algorithm 对于A*算法,这是一个不错的启发式函数吗?为什么?,algorithm,graph-algorithm,Algorithm,Graph Algorithm,15-解释难题: 为了解决15个难题,我编写了一个启发式函数来决定哪种状态最适合访问。因此,我编写了一个名为update fgh的启发式函数,对于每个节点,它将g增加1,h相对于每个节点到目标状态的距离增加 15拼图问题是一个由15块瓷砖组成的随机数组,每个瓷砖编号为1-15,以随机顺序分散在一个4 x 4的盒子上,同时添加一块空白瓷砖 为了解决15个难题,你必须移动空白的瓦片来代替一个瓦片,向左,向右,底部或顶部。p> 当每一个数字从1-15和空白的瓦片结束时,15个难题都得到了解决。 我的代
为了解决15个难题,你必须移动空白的瓦片来代替一个瓦片,向左,向右,底部或顶部。p>
当每一个数字从1-15和空白的瓦片结束时,15个难题都得到了解决。 我的代码中的变量如下
//[pnode中的变量]// N-表示预编译器中的4个分片,以使代码更具可移植性,当然也是出于安全原因。update_fgh(struct node *pnode)
{
pnode->f = g + h;
for(i = 0; i < 16; i++)
g++;
for(j =0; j< 16; j++){
//Goal is a global variable state.
if(pnode->start[i] != goal[j])
pnode->h++;
else
continue;
}
pnode->f = pnode->g+->h;
}
NxN-表示预编译器中4个分片的4个数组,以使代码更具可移植性,当然也是出于安全原因。
update_fgh(struct node *pnode)
{
pnode->f = g + h;
for(i = 0; i < 16; i++)
g++;
for(j =0; j< 16; j++){
//Goal is a global variable state.
if(pnode->start[i] != goal[j])
pnode->h++;
else
continue;
}
pnode->f = pnode->g+->h;
}
pnode-包含拼图所在状态的拼图节点。
update_fgh(struct node *pnode)
{
pnode->f = g + h;
for(i = 0; i < 16; i++)
g++;
for(j =0; j< 16; j++){
//Goal is a global variable state.
if(pnode->start[i] != goal[j])
pnode->h++;
else
continue;
}
pnode->f = pnode->g+->h;
}
f-启发式的f变量。
update_fgh(struct node *pnode)
{
pnode->f = g + h;
for(i = 0; i < 16; i++)
g++;
for(j =0; j< 16; j++){
//Goal is a global variable state.
if(pnode->start[i] != goal[j])
pnode->h++;
else
continue;
}
pnode->f = pnode->g+->h;
}
g-启发式的g变量。
update_fgh(struct node *pnode)
{
pnode->f = g + h;
for(i = 0; i < 16; i++)
g++;
for(j =0; j< 16; j++){
//Goal is a global variable state.
if(pnode->start[i] != goal[j])
pnode->h++;
else
continue;
}
pnode->f = pnode->g+->h;
}
h-启发式的h变量。
update_fgh(struct node *pnode)
{
pnode->f = g + h;
for(i = 0; i < 16; i++)
g++;
for(j =0; j< 16; j++){
//Goal is a global variable state.
if(pnode->start[i] != goal[j])
pnode->h++;
else
continue;
}
pnode->f = pnode->g+->h;
}
zero\u列和&zero行-表示p节点中零分片的变量
*next和*parent-拼图节点的子节点和父节点,每个pnode都保存一个next,零列向上、向下、向左或向右移动,如果启发式算法最便宜,则选择向上、向下、向左或向右移动的节点
//[全局变量]//
int-goal_行[NxN];-包含16个节点的数组的目标行。
update_fgh(struct node *pnode)
{
pnode->f = g + h;
for(i = 0; i < 16; i++)
g++;
for(j =0; j< 16; j++){
//Goal is a global variable state.
if(pnode->start[i] != goal[j])
pnode->h++;
else
continue;
}
pnode->f = pnode->g+->h;
}
int-goal_列[NxN];-包含16个节点的数组的目标列
结构节点*开始,*目标;-程序用户使用argv[]以随机顺序选择数字1-15加上空白节点初始化和选择的起始节点。并创建目标节点以比较目标和初始节点
结构节点*open=NULL,*closed=NULL;-具有开放和可用节点的两个节点,以及无法添加或使用的节点,因此将其置于关闭状态,以避免进一步考虑它们。
update_fgh(struct node *pnode)
{
pnode->f = g + h;
for(i = 0; i < 16; i++)
g++;
for(j =0; j< 16; j++){
//Goal is a global variable state.
if(pnode->start[i] != goal[j])
pnode->h++;
else
continue;
}
pnode->f = pnode->g+->h;
}
结构节点*成功节点[4];-成功节点指向成功的4个节点的数组的指针。
update_fgh(struct node *pnode)
{
pnode->f = g + h;
for(i = 0; i < 16; i++)
g++;
for(j =0; j< 16; j++){
//Goal is a global variable state.
if(pnode->start[i] != goal[j])
pnode->h++;
else
continue;
}
pnode->f = pnode->g+->h;
}
update\u fgh(结构节点*pnode)
{
pnode->f=g+h;
对于(i=0;i<16;i++)
g++;
对于(j=0;j<16;j++){
//目标是一个全局变量状态。
如果(pnode->start[i]!=目标[j])
pnode->h++;
其他的
继续;
}
pnode->f=pnode->g+->h;
}
下面是使用启发式的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 4
#define NxN (N*N)
#define TRUE 1
#define FALSE 0
struct node {
int tiles[N][N];
int f, g, h;
short zero_row, zero_column; /* location (row and colum) of blank tile 0 */
struct node *next;
struct node *parent; /* used to trace back the solution */
};
int goal_rows[NxN];
int goal_columns[NxN];
struct node *start,*goal;
struct node *open = NULL, *closed = NULL;
struct node *succ_nodes[4];
void print_a_node(struct node *pnode) {
int i,j;
for (i=0;i<N;i++) {
for (j=0;j<N;j++)
printf("%2d ", pnode->tiles[i][j]);
printf("\n");
}
printf("\n");
}
struct node *initialize(char **argv){
int i,j,k,index, tile;
struct node *pnode;
pnode=(struct node *) malloc(sizeof(struct node));
index = 1;
for (j=0;j<N;j++)
for (k=0;k<N;k++) {
tile=atoi(argv[index++]);
pnode->tiles[j][k]=tile;
if(tile==0) {
pnode->zero_row=j;
pnode->zero_column=k;
}
}
pnode->f=0;
pnode->g=0;
pnode->h=0;
pnode->next=NULL;
pnode->parent=NULL;
start=pnode;
printf("initial state\n");
print_a_node(start);
pnode=(struct node *) malloc(sizeof(struct node));
goal_rows[0]=3;
goal_columns[0]=3;
for(index=1; index<NxN; index++){
j=(index-1)/N;
k=(index-1)%N;
goal_rows[index]=j;
goal_columns[index]=k;
pnode->tiles[j][k]=index;
}
pnode->tiles[N-1][N-1]=0; /* empty tile=0 */
pnode->f=0;
pnode->g=0;
pnode->h=0;
pnode->next=NULL;
goal=pnode;
printf("goal state\n");
print_a_node(goal);
return start;
}
/* merge unrepeated nodes into open list after filtering */
void merge_to_open() {
}
/*swap two tiles in a node*/
void swap(int row1,int column1,int row2,int column2, struct node * pnode){
int tile = pnode->tiles[row1][coulmn1];
pnode->tiles[row1][column1]=pnode->tiles[row2][column2];
pnode->tiles[row2][column2]=tile;
}
/*update the f,g,h function values for a node */
update_fgh(struct node *pnode)
{
pnode->f = g + h;
for(i = 0; i < 16; i++)
g++;
for(j =0; j< 16; j++){
//Goal is a global variable state.
if(pnode->start[i] != goal[j])
pnode->h++;
else
continue;
}
pnode->f = pnode->g+->h;
}
/* 0 goes down by a row */
void move_down(struct node * pnode){
swap(pnode->zero_row, pnode->zero_column, pnode->zero_row+1, pnode->zero_column, pnode);
pnode->zero_row++;
}
/* 0 goes right by a column */
void move_right(struct node * pnode){
swap(pnode->zero_row, pnode->zero_column, pnode->zero_row, pnode->zero_column+1, pnode);
pnode->zero_column++;
}
/* 0 goes up by a row */
void move_up(struct node * pnode){
swap(pnode->zero_row, pnode->zero_column, pnode->zero_row-1, pnode->zero_column, pnode);
pnode->zero_row--;
}
/* 0 goes left by a column */
void move_left(struct node * pnode){
swap(pnode->zero_row, pnode->zero_column, pnode->zero_row, pnode->zero_column-1, pnode);
pnode->zero_column--;
}
/* expand a node, get its children nodes, and organize the children nodes using
* array succ_nodes.
*/
void expand(struct node *selected) {
}
int nodes_same(struct node *a,struct node *b) {
int flg=FALSE;
if (memcmp(a->tiles, b->tiles, sizeof(int)*NxN) == 0)
flg=TRUE;
return flg;
}
/* Filtering. Some nodes in succ_nodes may already be included in either open
* or closed list. Remove them. It is important to reduce execution time.
* This function checks the (i)th node in succ_nodes array. You must call this
& function in a loop to check all the nodes in succ_nodes.
*/
void filter(int i, struct node *pnode_list){
if(nodes_same(*pnode_list, succ_nodes[i]) == TRUE)
pnode_list = pnode_list->next;
}
int main(int argc,char **argv) {
int iter,cnt;
struct node *copen, *cp, *solution_path;
int ret, i, pathlen=0, index[N-1];
solution_path=NULL;
start=initialize(argv); /* init initial and goal states */
open=start;
iter=0;
while (open!=NULL) { /* Termination cond with a solution is tested in expand. */
copen=open;
open=open->next; /* get the first node from open to expand */
if(nodes_same(copen,goal)){ /* goal is found */
do{ /* trace back and add the nodes on the path to a list */
copen->next=solution_path;
solution_path=copen;
copen=copen->parent;
pathlen++;
} while(copen!=NULL);
printf("Path (lengh=%d):\n", pathlen);
copen=solution_path;
... /* print out the nodes on the list */
break;
}
expand(copen); /* Find new successors */
for(i=0;i<4;i++){
filter(i,open);
filter(i,closed);
}
merge_to_open(); /* New open list */
copen->next=closed;
closed=copen; /* New closed */
/* print out something so that you know your
* program is still making progress
*/
iter++;
if(iter %1000 == 0)
printf("iter %d\n", iter);
}
return 0;
} /* end of main */
#包括
#包括
#包括
#定义n4
#定义NxN(N*N)
#定义真1
#定义FALSE 0
结构节点{
国际瓷砖[N][N];
int f,g,h;
短零行,零列;/*空白磁贴0的位置(行和列)*/
结构节点*下一步;
用于跟踪解决方案的结构节点*parent;/**/
};
整数目标_行[NxN];
int目标_列[NxN];
结构节点*开始,*目标;
结构节点*open=NULL,*closed=NULL;
结构节点*成功节点[4];
无效打印节点(结构节点*pnode){
int i,j;
对于(i=0;izero_列=k;
}
}
pnode->f=0;
pnode->g=0;
pnode->h=0;
pnode->next=NULL;
pnode->parent=NULL;
开始=pnode;
printf(“初始状态”);
打印一个节点(开始);
pnode=(结构节点*)malloc(sizeof(结构节点));
目标_行[0]=3;
目标_列[0]=3;
对于(索引=1;索引[j][k]=索引;
}
pnode->tile[N-1][N-1]=0;/*空tile=0*/
pnode->f=0;
pnode->g=0;
pnode->h=0;
pnode->next=NULL;
目标=pnode;
printf(“目标状态”);
打印一个节点(目标);
返回启动;
}
/*过滤后将未重复的节点合并到打开的列表中*/
作废合并到打开(){
}
/*交换节点中的两个平铺*/
无效交换(int-row1、int-column1、int-row2、int-column2、结构节点*pnode){
int tile=pnode->tiles[row1][coulmn1];
pnode->tiles[row1][column1]=pnode->tiles[row2][column2];
pnode->tiles[row2][column2]=tile;
}
/*更新节点的f、g、h函数值*/
更新_fgh(结构节点*pnode)
{
pnode->f=g+h;
对于(i=0;i<16;i++)
g++;
对于(j=0;j<16;j++){
//目标是一个全局变量状态。
如果(pnode->start[i]!=目标[j])
pnode->h++;
其他的
继续;
}
pnode->f=pnode->g+->h;
}
/*0向下移动一行*/
void下移(结构节点*pnode){
交换(pnode->zero\u行,pnode->zero\u列,pnode->zero\u行+1,pnode->zero\u列,pnode);
pnode->zero_row++;
}
/*0向右移动一列*/
void向右移动(结构节点*pnode){
交换(pnode->zero\u行,pnode->zero\u列,pnode->zero\u行,pnode->zero\u列+1,pnode);
pnode->zero_列++;
}
/*0上升一行*/
无效上移(结构节点*pnode){
交换(pnode->zero\u行,pnode->zero\u列,pnode->zero\u行-1,pnode->zero\u列,pnode);
pnode->zero_行--;
}
/*0向左移动一列*/
无效向左移动(结构节点*pnode){
交换(pnode->zero\u行,pnode->zero\u列,pnode->zero\u行,pnode->zero\u列-1,pnode);
pnode->zero_列--;
}
/*展开节点,获取其子节点,并使用
*数组成功节点。
*/
空心展开(结构节点*选定){
}
int nodes_same(结构节点*a,结构节点*b){
int flg=假;
如果(memcmp(a->tiles,b->tiles,sizeof(int)*NxN)==0)
flg=真;
返回flg;
}
/*筛选。成功节点中的某些节点可能已包含在任一打开节点中
*或已关闭列表。删除