连连看,devc++下编译通过,代码如下:
下次再和MM一起用mfc做界面去。#include<iostream>
#include<vector> 
using namespace std;
class point
{
      public:
             point(){x=0;y=0;f=-1;} 
             point(int xx,int yy,int ff):x(xx),y(yy),f(ff){} 
             int x,y; //x为行号,y为列号,下标都是从0开始 
             int f;//为0表示该点为空,否则为第f种图片 
};
class link

      public:
             virtual ~link(){}
             link(){row=8;col=8;kind=8;num=row*col;judge_flag=true;} 
             link(int r,int c){row=r;col=c;kind=8;num=row*col;judge_flag=true;} 
             link(int r,int c,int n){row=r;col=c;kind=n;num=row*col;judge_flag=true;}
             bool judge_link(const point &a,const point &b)const; //判断两点是否相连(转角最多两个) 
             void produce_map(); //产生地图      
             int get_num()const{return num;} 
             point get_point(int x,int y)const{return map[x][y];} 
             void kill_picture(const point &a,const point &b){map[a.x][a.y].f=0;map[b.x][b.y].f=0;num-=2;} 
             void print_map()const; 
      private:
              bool link_zero_corner(const point &a,const point &b)const; //转角为0连接 
              bool link_one_corner(const point &a,const point &b)const; //转角为1连接
              bool link_two_corner(const point &a,const point &b)const; //转角为2连接
              void judge(int cnt);//递归搜索所有可能下法,判断是否有解  
              bool judge_practice(); //true有解,false无解       
      private:
              vector< vector<point> >map; //地图 
              vector< vector<point> > vt; //把相同种类的图片链接起来,vt[i].size()表示为第i种图片的数量 
              int row,col; //行与列,row*col必为偶数 
              int kind; //图片的种类数量  
              int num; //地图中图片的数量 
              bool judge_flag; //是否有解                                   
}; 
bool link::link_zero_corner(const point &a,const point &b)const
{
     if(a.x!=b.x && a.y!=b.y)
         return false;
     int i; 
     if(a.x==b.x)
     { 
         for(i=min(a.y,b.y)+1;i!=max(a.y,b.y);++i)
             if(map[a.x][i].f) 
                 return false; 
     } 
     else
     {
         for(i=min(a.x,b.x)+1;i!=max(a.x,b.x);++i)
             if(map[i][a.y].f)
                 return false; 
     } 
     return true; 

bool link::link_one_corner(const point &a,const point &b)const
{
     if(!map[a.x][b.y].f && link_zero_corner(a,map[a.x][b.y]) && link_zero_corner(b,map[a.x][b.y]))
         return true; 
     else if(!map[a.y][b.x].f && link_zero_corner(a,map[a.y][b.x]) && link_zero_corner(b,map[a.y][b.x]))
         return true;
     return false; 
}
bool link::link_two_corner(const point &a,const point &b)const
{
     //由a开始向4个方向遍历c,判断c和b是否一个转角相连 
     int i;
     for(i=0;i!=a.x && i<row;++i)//up,down 
     {
         if(map[i][a.y].f)
            break;
         else if(link_one_corner(map[i][a.y],b))
            return true; 
     } 
     for(i=0;i!=a.y && i<col;++i)//left,right 
     {
         if(map[a.x][i].f)
             break;
         else if(link_one_corner(map[a.x][i],b))
              return true; 
     }
     return false; 

bool link::judge_link(const point &a,const point &b)const
{
     if(a.x==b.x && a.y==b.y) //两点相同判定为不相连 
         return false; 
     if(a.f!=b.f) 
         return false;
     if(link_zero_corner(a,b) || link_one_corner(a,b) || link_two_corner(a,b))
         return true; 
     return false; 

void link::produce_map()
{
     do
     { 
         map.clear();
         vt.clear(); 
         int i,j,x,y; 
         for(i=0;i<row;++i) //构建二维数组map 
         {   
             vector<point>v;          
             for(j=0;j<col;++j)
             { 
                 point p;
                 p.x=i; p.y=j; p.f=0; 
                  v.push_back(p); 
             }  
             map.push_back(v); 
         } 
         srand(time(0)); 
         int t=static_cast<int>(row*col/kind);
         (t&1)==0?t:t-=1; //数据保证t为偶数  
         num=kind*t; //图片总数 
         for(i=1;i<=kind;++i) //kind种图片随机均分到地图中,kind*t!=row*col时不是均分,那么会产生空位,这个由用户决定 
         {
             vector<point>v2; 
             for(j=0;j<t;++j)
             {
                 int flag=1;
                 while(flag)
                 {
                      x=rand()%row;
                      y=rand()%col;
                      if(!map[x][y].f)
                      {
                          map[x][y].x=x;
                          map[x][y].y=y;
                          map[x][y].f=i; 
                          v2.push_back(map[x][y]); // 
                          flag=0; 
                      } 
                 } 
             } 
             vt.push_back(v2); 
         }     
     }while(!judge_practice()); //若地图无解,那么继续产生,当然实际上几乎总是有解  

bool link::judge_practice()
{
     judge_flag=false; 
     vector< vector<point> >v(map);// 
     judge(0);
     map.assign(v.begin(),v.end());// 
     return judge_flag; 

void link::judge(int cnt)
{
     if(cnt==num)
         judge_flag=true;
     if(judge_flag)
         return ;
     vector< vector<point> >::size_type i;
     vector<point>::size_type j,k;  
     for(i=0;i!=vt.size();++i)
     { 
         for(j=0;j!=vt[i].size();++j)
         { 
              for(k=j+1;k<vt[i].size();++k)// 
              { 
                   int f=map[vt[i][j].x][vt[i][j].y].f; 
                   if(judge_link(vt[i][j],vt[i][k])) 
                   { 
                       map[vt[i][j].x][vt[i][j].y].f=0;
                       map[vt[i][k].x][vt[i][k].y].f=0;     
                       judge(cnt+2);
                       map[vt[i][j].x][vt[i][j].y].f=f;
                       map[vt[i][k].x][vt[i][k].y].f=f; 
                   } 
              } 
         }           
     } 

void link::print_map()const
{
     int i,j,k;
     for(i=0;i<row;++i)
     {
         if(i==0)
         {
             cout<<"      ";
             for(k=0;k<col;++k)
                 cout<<k<<" ";
             cout<<endl;
             for(k=0;k<col;++k)
                 cout<<"----";
             cout<<endl;
         }
         for(j=0;j<col;++j)
         {
              if(j==0)
                  cout<<i<<"  |  ";
              cout<<map[i][j].f<<" ";        
         }
         cout<<endl;
     }   

int main()
{
    link p;
    p.produce_map();
    p.print_map();
    int x1,y1,x2,y2; 
    point a,b; 
    while(p.get_num())
    {
         cout<<"请分别输入你要选择的两个相连点的行号和列号,下标从0开始"<<endl;
         cin>>x1>>y1>>x2>>y2; 
         a=p.get_point(x1,y1);
         b=p.get_point(x2,y2); 
         if(p.judge_link(a,b))
         {
             p.kill_picture(a,b); 
             cout<<endl; 
             p.print_map(); 
             cout<<endl; 
         } 
         else
         {
             cout<<"两点不相连,请重新输入"<<endl; 
             cout<<endl; 
             p.print_map(); 
             cout<<endl; 
         }             
    } 
      
system("pause");
return 0;
}27号搬校区,嘎嘎,散个分