简单的连连看的算法

来源:互联网 发布:wacom mac 绘画软件 编辑:程序博客网 时间:2024/05/01 10:00

// stlusing.cpp : コンソール アプリケーションのエントリ ポイントを定義します。
//

#include "stdafx.h"
#include "stlusing.h"
#include <vector>
#include <functional>
#include <algorithm>


#ifdef _DEBUG
#define new DEBUG_NEW
#endif
using namespace std;
struct test
{
    CString m_strName;
    test():m_strName(L"0")
    {};
    test(CString str)
    {m_strName = str;};
    bool  display()
    {
        wcout<<(LPCTSTR)m_strName<<endl;
        return true;
    }
};
struct point
{
    int x;
    int y;
    bool operator ==(point& p)const
    {
        return (x==p.x) &&( y==p.y);
    }
};
struct node
{
    int m_idata;
    bool m_bChecked;
    node(int data)
    {
        m_idata = data;
        m_bChecked = false;

    }
};
bool comparePoint(vector<vector<node> >& vecPoints,point& from,point& to )
{
    
    cout<<from.x<<from.y<<endl;
   
    vecPoints[from.x][from.y].m_bChecked = true;
    point p;
    p.x = from.x+1;
    p.y = from.y;
    if((from.x+1+1)<=static_cast<int>(vecPoints.size())&&vecPoints[from.x+1][from.y].m_idata == 0 && !(vecPoints[from.x+1][from.y].m_bChecked) )
    {
        if(comparePoint(vecPoints,p,to))return true;
    }
    if(p == to) return true;
    
    
    p.x = from.x;
    p.y = from.y+1;
    if((from.y+1+1)<=static_cast<int>(vecPoints[0].size()) && vecPoints[from.x][from.y+1].m_idata == 0 && !(vecPoints[from.x][from.y+1].m_bChecked))
    {
        
        
        if(comparePoint(vecPoints,p,to))return true;
    }
    if(p == to) return true;


   p.x = from.x-1;
        p.y = from.y;
    if((from.x-1) >=0 && vecPoints[from.x-1][from.y].m_idata == 0 && !(vecPoints[from.x-1][from.y].m_bChecked))
    {
        
        
        if(comparePoint(vecPoints,p,to))return true;
    }

    if(p == to) return true;
    
        p.x = from.x;
        p.y = from.y-1;
    if((from.y-1)>=0 && vecPoints[from.x][from.y-1].m_idata == 0 && !(vecPoints[from.x][from.y-1].m_bChecked))
    {
    
        
        if(comparePoint(vecPoints,p,to))return true;
    }
    if(p == to) return true;
    return false;

}
//struct fun
//{
//    fun(){};
//    operator (test * t){
//        delete t;
//    }
//};

// 唯一のアプリケーション オブジェクトです。

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    int nRetCode = 0;

    // MFC を初期化して、エラーの場合は結果を印刷します。
    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
    {
        // TODO: 必要に応じてエラー コードを変更してください。
        _tprintf(_T("致命的なエラー: MFC の初期化ができませんでした。/n"));
        nRetCode = 1;
    }
    else
    {
        vector<vector<node> > vec;


    //vector<int> vec(in_iter, eof); //do the same work as following loop
      
    
      vector<node> vecInt1;
      vecInt1.push_back(node(1));
      vecInt1.push_back(node(1));
      vecInt1.push_back(node(0));
      vecInt1.push_back(node(0));
      
      vector<node> vecInt2;
      vecInt2.push_back(node(1));
     vecInt2.push_back(node(1));
     vecInt2.push_back(node(1));
     vecInt2.push_back(node(0));

      vector<node> vecInt3;
      vecInt3.push_back(node(1));
     vecInt3.push_back(node(1));
      vecInt3.push_back(node(1));
      vecInt3.push_back(node(0));

    
      vec.push_back(vecInt1);
      vec.push_back(vecInt2);
      vec.push_back(vecInt3);

      point from1;
      from1.x=1;
      from1.y=1;

      point from2;
      from2.x=2;
      from2.y=2;

      point to;
      to.x=0;
      to.y=1;

    /*  cout<<comparePoint( vec,from1,to )<<endl;*/
      cout<<comparePoint(vec,from2,to )<<endl;

    /*    vector <test*> v;
        test  t1;
        v.push_back(&t1);
        v.push_back(new test(L"1"));
        v.push_back(new test(L"2"));
        CString str =L"geegege";
        wcout<<(LPCTSTR)str<<endl;
        for_each(v.begin(),v.end(),mem_fun<bool,test>(&test::display));*/
    /*    for_each(v.begin(),v.end(),fun);*/
        system("pause");

        
    }

    return nRetCode;
}

 

原创粉丝点击