服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++连连看判定图形消除算法

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

2021-08-08 16:53yyhejun C/C++

这篇文章主要为大家详细介绍了C++连连看判定图形消除算法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// 连连看.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; }  //第一次转弯
   while(!Y.empty())
    { temp=Y.front();Y.pop();
     i=pushonelinespot(temp,b,c,Z);
     if(i==1) return 1;}  //第二次转弯
 cout<<"转弯路径大于两次"<<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;}            //初始化二维数组
  cout<<"请初始化数组矩阵:"<<endl;
  while(cin>>value)           //输入控制
    {
      for(i=0;i<6;i++)
       {for(j=0;j<6;j++)
         c[i][j]=value;}
    } //初始化二维数组;//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;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/yyhejun/article/details/16360649

延伸 · 阅读

精彩推荐