国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++連連看判定圖形消除算法

2020-01-26 13:22:30
字體:
供稿:網(wǎng)友

我們玩過的連連看游戲,通過選定兩個圖形相同的元素,判定其是否可在三次轉(zhuǎn)彎內(nèi)連接起來,若能,則消去,若不能,則不可消去,直至最后全部消除。

本算法中不包括關于死鎖狀態(tài)判定。

// 連連看.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <iostream.h>#include <vector>#include <queue>  using namespace std; struct Point{  int x;  int y;}; int pushonelinespot(Point a,Point b,int c[6][6], queue<Point> &g)  //將a點可直線到達的點壓入隊列g中{  int i;  Point temp;  for(i=1;;i++)  //向下檢測   { if((a.x+i)<=5&&c[a.x+i][a.y]==0)      {     temp.x=a.x+i;temp.y=a.y;       g.push(temp);}       else if(c[a.x+i][a.y]!=0&&(a.x+i)<=5)      {if((a.x+i)==b.x&&a.y==b.y) return 1;       else break;}     else break;    }   for(i=-1;;i--)   //向上檢測   { if(c[a.x+i][a.y]==0&&(a.x+i)>=0)      { temp.x=a.x+i;temp.y=a.y;         g.push(temp);}     else if(c[a.x+i][a.y]!=0&&(a.x+i)>=0)      {if((a.x+i)==b.x&&a.y==b.y) return 1;       else break;}     else break;    }   for(i=1;;i++)   //向右檢測   { if(c[a.x][a.y+i]==0&&(a.y+i)<=5)      { temp.x=a.x;temp.y=a.y+i;         g.push(temp);}     else if(c[a.x][a.y+i]!=0&&(a.y+i)<=5)      {if(a.x==b.x&&(a.y+i)==b.y) return 1;       else break;}     else break;    }   for(i=-1;;i--)  //向左檢測   { if(c[a.x][a.y+i]==0&&(a.y+i)>=0)      { temp.x=a.x;temp.y=a.y+i;         g.push(temp);}     else if(c[a.x][a.y+i]!=0&&(a.y+i)>=0)      {if(a.x==b.x&&(a.y+i)==b.y) return 1;       else break;}     else break;    }   return 0;} bool shortestline(Point a,Point b,int c[6][6]){  if(c[a.x][a.y]==c[b.x][b.y])  {  Point temp;  queue<Point> X,Y,Z;  int i;  i=pushonelinespot(a,b,c,X);  if(i==1) return 1;  while(!X.empty())   { temp=X.front();X.pop();    i=pushonelinespot(temp,b,c,Y);    if(i==1) return 1; }  //第一次轉(zhuǎn)彎   while(!Y.empty())    { temp=Y.front();Y.pop();     i=pushonelinespot(temp,b,c,Z);     if(i==1) return 1;}  //第二次轉(zhuǎn)彎 cout<<"轉(zhuǎn)彎路徑大于兩次"<<endl;   return 0;  } else if(c[a.x][a.y]==0||c[b.x][b.y]==0) {cout<<"圖形已空,請重新輸入:"<<endl;}  else { cout<<"兩圖形不相同"<<endl;return 0;}} int main(){  int v,i,j;  int c[6][6]={{0,0,0,0,0,0},{0,1,3,3,4,0},{0,0,6,4,0,0},{0,4,0,2,1,0},{0,6,0,4,2,0},{0,0,0,0,0,0}};/*  for(i=0;i<6;i++)    {for(j=0;j<6;j++)  c[i][j]=0;}            //初始化二維數(shù)組  cout<<"請初始化數(shù)組矩陣:"<<endl;  while(cin>>value)           //輸入控制    {      for(i=0;i<6;i++)       {for(j=0;j<6;j++)         c[i][j]=value;}    } //初始化二維數(shù)組;//while*/  for(i=0;i<6;i++)    {   for(j=0;j<6;j++)   cout<<"    "<<c[i][j]<<"  ";cout<<endl;  }; Point a,b;      for(i=0;;i++)  {  int sum=0;  cout<<"請輸入坐標:"<<endl;    cin>>a.x>>a.y>>b.x>>b.y;   if(a.x>0&&a.x<5&&a.y>0&&a.y<5&&b.x>0&&b.x<5&&b.y>0&&b.y<5)   {  if(a.x==b.x&&a.y==b.y) {cout<<"不可輸入相同坐標,請重新輸入:"<<endl;continue;} v=shortestline(a,b,c);    if(v==1) {  c[a.x][a.y]=0;c[b.x][b.y]=0;         cout<<"成功消除"<<endl; }   }    else cout<<"坐標輸入有誤,請重新輸入:"<<endl;   for(i=0;i<6;i++)    {   for(j=0;j<6;j++)       sum+=c[i][j];  };  if(sum==0) break;   for(i=0;i<6;i++)    {   for(j=0;j<6;j++)   cout<<"    "<<c[i][j]<<"  ";cout<<endl;  }; }; cout<<"恭喜過關。"<<endl; getchar(); return 0;}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 仙游县| 长武县| 察雅县| 河津市| 沾益县| 济宁市| 梅州市| 宝山区| 莱阳市| 通榆县| 沈阳市| 岳普湖县| 兴城市| 灵宝市| 安义县| 吕梁市| 德惠市| 资源县| 洮南市| 剑河县| 滦平县| 瑞金市| 麻栗坡县| 友谊县| 淮滨县| 梁平县| 密云县| 化德县| 逊克县| 图片| 汝城县| 北海市| 龙井市| 宿州市| 宿松县| 晋州市| 百色市| 峨眉山市| 项城市| 台江县| 柏乡县|