栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++连连看判定图形消除算法

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++连连看判定图形消除算法

我们玩过的连连看游戏,通过选定两个图形相同的元素,判定其是否可在三次转弯内连接起来,若能,则消去,若不能,则不可消去,直至最后全部消除。

本算法中不包括关于死锁状态判定。

// 连连看.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include 
#include 
#include 
 
 
using namespace std;
 
struct Point
{
  int x;
  int y;
};
 
int pushonelinespot(Point a,Point b,int c[6][6], queue &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 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; }  //第一次转弯
   while(!Y.empty())
    { temp=Y.front();Y.pop();
     i=pushonelinespot(temp,b,c,Z);
     if(i==1) return 1;}  //第二次转弯
 cout<<"转弯路径大于两次"<>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<<"不可输入相同坐标,请重新输入:"<

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/62226.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号