连连看核心代码

写连连看的时候的  用于判断消除的代码

枚举判断


int data[13][8];

class Point2D
{
public:
	int x;
	int y;
	Point2D(int xx, int yy):x(xx),y(yy){}
	Point2D() :x(0), y(0){}
}


class searchAlg
{
public:

	int search_0(Point2D pre, Point2D las, queue<Point2D> &tmp)
	{
		/*
		二维坐标0代表 空
		搜索0拐点
		return 0 代表未找到
		return N N代表路径数(两点差值)
		相邻返回1 隔一个返回2  以此类推
		*/
		int x_pre = pre.x;
		int y_pre = pre.y;
		int x_las = las.x;
		int y_las = las.y;


		//x变量
		if (y_pre == y_las)
		{
			int min = x_pre > x_las ? x_las : x_pre;
			int max = x_pre < x_las ? x_las : x_pre;

			tmp.push(Point2D(x_pre, y_pre));
			if (x_pre>x_las)
			{
				for (int x = max - 1; x > min; x--)
				{

					if (data[x][y_pre] == 0)
					{
						tmp.push(Point2D(x, y_pre));

					}
					else
					{
						return 0;
					}
				}

			}


			if (x_pre <= x_las) //等价
				//else
			{
				for (int x = min + 1; x < max; x++)
				{

					if (data[x][y_pre] == 0)
					{
						tmp.push(Point2D(x, y_pre));

					}
					else
					{
						return 0;
					}
				}

			}

			return (max - min); //代表队列多少个数据
		}






		///y变量
		if (x_pre == x_las)
		{

			int min = y_pre > y_las ? y_las : y_pre;
			int max = y_pre < y_las ? y_las : y_pre;

			tmp.push(Point2D(x_pre, y_pre));

			if (y_pre>y_las)
			{
				for (int y = max - 1; y > min; y--)
				{

					if (data[x_pre][y] == 0)
					{
						tmp.push(Point2D(x_pre, y));
					}
					else
					{
						return 0;
					}
				}
			}

			if (y_pre <= y_las)
			{
				for (int y = min + 1; y < max; y++)
				{

					if (data[x_pre][y] == 0)
					{
						tmp.push(Point2D(x_pre, y));
					}
					else
					{
						return 0;
					}
				}

			}

			return (max - min); //代表队列多少个数据

		}




		return 0;
	}



	int search_1(Point2D pre, Point2D las, queue<Point2D> &tmp)
		//1个拐点
	{
		if (data[las.x][pre.y] == 0)//右下
		{
			if (search_0(pre, Point2D(las.x, pre.y), tmp) != 0)
			{
				if (search_0(Point2D(las.x, pre.y), las, tmp) != 0)
				{
					return  tmp.size();
				}
			}
		}

		for (; !tmp.empty();)//清空
		{
			tmp.pop();
		}


		if (data[pre.x][las.y] == 0)//左上
		{
			if (search_0(pre, Point2D(pre.x, las.y), tmp) != 0)
			{
				if (search_0(Point2D(pre.x, las.y), las, tmp) != 0)
				{

					return tmp.size();
				}
			}
		}

		return 0;
	}



	int search_2(Point2D pre, Point2D las, queue<Point2D> &tmp)
	{//2拐点

		//凹型y+
		for (int i = 0; i < y; i++)
		{

			for (; !tmp.empty();)//清空
			{
				tmp.pop();
			}


			if (data[las.x][las.y + i] == 0)//凹型
			{
				if (search_1(pre, Point2D(las.x, las.y + i), tmp) != 0)
				{
					if (search_0(Point2D(las.x, las.y + i), las, tmp) != 0)
					{

						return tmp.size();
					}
				}
			}
		}




		for (; tmp.empty() == false;)
		{
			tmp.pop();
		}



		//凹型y-
		for (int i = 0; i < y; i++)
		{

			for (; !tmp.empty();)//清空
			{
				tmp.pop();
			}


			if (data[las.x][las.y - i] == 0)//凹型
			{
				if (search_1(pre, Point2D(las.x, las.y - i), tmp) != 0)
				{
					if (search_0(Point2D(las.x, las.y - i), las, tmp) != 0)
					{

						return tmp.size();
					}
				}
			}
		}




		for (; tmp.empty() == false;)
		{
			tmp.pop();
		}






		//凹型x+
		for (int i = 0; i < x; i++)
		{

			for (; !tmp.empty();)//清空
			{
				tmp.pop();
			}


			if (data[las.x + i][las.y] == 0)//凹型
			{
				if (search_1(pre, Point2D(las.x + i, las.y), tmp) != 0)
				{
					if (search_0(Point2D(las.x + i, las.y), las, tmp) != 0)
					{

						return tmp.size();
					}
				}
			}
		}



		for (; tmp.empty() == false;)
		{
			tmp.pop();
		}



		//凹型x-
		for (int i = 0; i < x; i++)
		{

			for (; !tmp.empty();)//清空
			{
				tmp.pop();
			}


			if (data[las.x - i][las.y] == 0)//凹型
			{
				if (search_1(pre, Point2D(las.x - i, las.y), tmp) != 0)
				{
					if (search_0(Point2D(las.x - i, las.y), las, tmp) != 0)
					{

						return tmp.size();
					}
				}
			}
		}







		return 0;

	}


	int x;
	int y;

	searchAlg(int l, int w) :y(l), x(w)
	{

	}
}


转载于:https://my.oschina.net/kkkkkkkkkkkkk/blog/423899

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值