六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

拼圖游戲的算法

[摘要]相信大家都玩過"滑塊拼圖"游戲! 大概說一下 :假如一副圖是由幾個部分拼湊成的,現在要你把這些散塊拼湊成一副完整的圖片 也可以是幾個數字來拼湊 比如 3*3的格子 1 2 3 4 5 6 7 8 ...
相信大家都玩過"滑塊拼圖"游戲!
  大概說一下 :假如一副圖是由幾個部分拼湊成的,現在要你把這些散塊拼湊成一副完整的圖片
              也可以是幾個數字來拼湊
  比如 3*3的格子
        1 2 3
        4 5 6
        7 8     (相當于原始矩陣)
有一個格子是空的現在要你組合成
     1 2 7
     3 6 4
     5 8          (目標矩陣)
問題是編寫一種算法 能根據輸入的原始圖形(矩陣)拼湊成目標圖形(目標矩陣) 要求是步數最少(耗時間最少)
  #include"iostream.h"
struct node{
int nodesun[4][4];

int x,y;
}path[1000];
int zx[4]={-1,0,1,0};
int zy[4]={0,-1,0,1};
int top;
int desti[4][4];//目標狀態
int detect(struct node *p)
{int i,j;
  for(i=1;i<4;i++)
     for(j=1;j<4;j++)
   if(p->nodesun[i][j]!=desti[i][j])
    return 0;
  return 1;
}

void printlj()
{int tempt;
int i,j;
tempt=1;
while(tempt<=top)
{    for(i=1;i<4;i++)
     for(j=1;j<4;j++)
  {cout<<path[tempt].nodesun[i][j];
   if(j==3)
    cout<<" "<<endl;
  }
  tempt++;
}
}

   
   

void main()
{ //初始化
  int i,j,m,n,f;
  int temp,find=0;
  top=1;
for(i=1;i<4;i++)
     for(j=1;j<4;j++)
  {cout<<"請輸入第"<<i<<"行"<<"第"<<j<<"列的值"<<endl;
   cin>>temp;
   path[1].nodesun[i][j]=temp;
  }
  cout<<"請輸入初始狀態的空格的位置(行)"<<endl;
  cin>>temp;
  path[1].x=temp;
cout<<"請輸入初始狀態的空格的位置(列)"<<endl;
  cin>>temp;
  path[1].y=temp;
//目標狀態
for(i=1;i<4;i++)
     for(j=1;j<4;j++)
  {cout<<"請輸入目標狀態第"<<i<<"行"<<"第"<<j<<"列的值"<<endl;
   cin>>temp;
   desti[i][j]=temp;
  }
  //深度優先搜索
while(!find)
{ m=path[top].x;
  n=path[top].y ;
  for(f=0;f<4;f++)
  {i=m+zx[f];
   j=n+zy[f];
   if(i>=1&&i<=3&&j>=1&&j<=3)
   {top++;
    path[top]=path[top-1];
path[top].nodesun[m][n]=path[top-1].nodesun[i][j];
path[top].nodesun[i][j]=0;

path[top].x=i;
path[top].y=j;

   if(detect(&path[top]))
   {printlj();
   find=1;
   break;
   }

   break;
   }//if
  
  }//for
   
   }//while
}



#include"iostream.h"
struct node{
int nodesun[4][4];
int pre;
int x,y;
}path[400];
int zx[4]={-1,0,1,0};
int zy[4]={0,-1,0,1};
int front,rear;
int desti[4][4];//目標狀態
int detect(struct node *p)
{int i,j;
  for(i=1;i<4;i++)
     for(j=1;j<4;j++)
   if(p->nodesun[i][j]!=desti[i][j])
    return 0;
  return 1;
}

void printlj()
{int tempt;
int i,j;
tempt=rear;
while(tempt!=0)
{    for(i=1;i<4;i++)
     for(j=1;j<4;j++)
  {cout<<path[tempt].nodesun[i][j];
   if(j==3)
    cout<<" "<<endl;
  }
  tempt=path[tempt].pre;
}
}

   
   

void main()
{ //初始化
  int i,j,m,n,f;
  int temp,find=0;
  front=rear=1;
  path[1].pre=0;
for(i=1;i<4;i++)
     for(j=1;j<4;j++)
  {cout<<"請輸入第"<<i<<"行"<<"第"<<j<<"列的值"<<endl;
   cin>>temp;
   path[1].nodesun[i][j]=temp;
  }
  cout<<"請輸入初始狀態的空格的位置(行)"<<endl;
  cin>>temp;
  path[1].x=temp;
cout<<"請輸入初始狀態的空格的位置(列)"<<endl;
  cin>>temp;
  path[1].y=temp;
//目標狀態
for(i=1;i<4;i++)
     for(j=1;j<4;j++)
  {cout<<"請輸入目標狀態第"<<i<<"行"<<"第"<<j<<"列的值"<<endl;
   cin>>temp;
   desti[i][j]=temp;
  }
  //廣度優先搜索
while(front<=rear&&!find)
{ m=path[front].x;
  n=path[front].y ;
  for(f=0;f<4;f++)
  {i=m+zx[f];
   j=n+zy[f];
   if(i>=1&&i<=3&&j>=1&&j<=3)
   {rear++;
    path[rear]=path[front];
path[rear].nodesun[m][n]=path[front].nodesun[i][j];
path[rear].nodesun[i][j]=0;
path[rear].pre=front;
path[rear].x=i;
path[rear].y=j;
   if(detect(&path[rear]))
   {printlj();
   find=1;
   break;
   }
   }
  }
   front++;
   }
}

上面是用最簡單的深度優先,廣度優先直接搜索得來得,毫無AI可言,這并不說明我不能寫出其更好得算法,這里最簡單得的估價函數f(x)=g(x)+h(x)
g(x)用初始化的節點到當前的節點的路徑長度來表示h(x)啟發函數用當前狀態和目標狀態的位置不同的個數來表
到156.com可以看我的根多文章 


主站蜘蛛池模板: 欧美又黄又嫩大片a级 | 亚洲图片综合区另类图片 | 日韩色天使综合色视频 | 色婷婷久久综合中文久久一本 | 婷婷六月久久综合丁香可观看 | 日本精品不卡 | 一级做性色a爰片久久毛片免费 | 亚洲国产一 | 在线观看欧美三级 | 一级人做人a爰免费视频 | 综合色久 | 中文字幕亚洲一区婷婷 | 污污在线观看视频 | 小草社区在线观看播放 | 日产精品久久久一区二区 | 最新天堂网| 天天草天天干天天 | 日本视频一区二区 | 伊人免费视频网 | 亚洲第一网站快活影院 | 天啪天天久久天天综合啪 | 青青青手机在线观看 | 四虎影视ww5234hucomww网站 | 日韩欧美一区黑人vs日本人 | 涩涩五月天| 色秋霞| 热久久最新网址 | 色噜噜狠狠色综合中文字幕 | 视频在线观看40分钟免费 | 人人射人人舔 | 青青草原在线播放 | 婷婷综合缴情亚洲五月伊 | 日韩亚州 | 亚洲成a人片在线观看中文 亚洲成a人片在线观看精品 | 日本欧美一区二区三区视频 | 午夜毛片不卡高清免费 | 污污小视频在线观看 | 色噜噜在线 | 亚洲大片免费看 | 亚洲 欧美 日韩中文字幕一区二区 | 小小小小视频高清日本 |