求助!!为什么这里全挂了?就是按照递归分治的方法做的
查看原帖
求助!!为什么这里全挂了?就是按照递归分治的方法做的
352285
Titan_Hope楼主2020/8/4 16:32
#include<iostream>
#include<string.h>
using namespace std;
int n;
int a[1025][1025];

struct place
{
	int x, y;
};
void solution(place start, place end);
int main(void)
{
	int k;
	cin >> k;
	//算出整个位置:
	n = 1 << k;
	//输入公主坐标:
	int x, y;
	cin >> x >> y;
	memset(a, 0, sizeof(a));
	//处理整个方格 (用分治法,其实就是递归)
	a[x][y] = 1;
	place start, end;
	start.x = 1;
	start.y = 1;
	end.x = n;
	end.y = n;
	solution(start, end);
/*	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			cout << a[i][j] << " ";
		}
		cout << endl;
	}*/
	return 0;
}
void solution(place start, place end)
{
/*	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
	cout << "start" << start.x << " " << start.y << " " << endl;
	cout << "end" << end.x << " " << end.y << " " << endl; */

	//找到中间值:
	place mid;
	mid.x = (start.x + end.x) / 2;
	mid.y = (start.y + end.y) / 2;
	//看xy的位置  x代表的是纵轴,y代表的是横方向的变化 
	int p = 0, q = 0;
	for (int i = start.x; i <= end.x; i++)
	{
		for (int j = start.y; j <= end.y; j++)
		{
			if (a[i][j] != 0)
			{
				//找到该点 
				p = i;
				q = j;
				//cout << p << "pq " << q << " " << endl;
				break;
			}

		}if (p)break;

	}
	//判断start和end
	if (start.x + 1 == end.x)
	{
		//判断pq位置:
		if (p == end.x)
		{
			if (q == end.y)
			{
				a[p][q - 1] = 4;
				a[p - 1][q] = 4;
				a[p - 1][q - 1] = 4;
				cout << p << " " << q - 1 << " " << 4 << endl;
				cout << p - 1 << " " << q << " " << 4 << endl;
				cout << p - 1 << " " << q - 1 << " " << 4 << endl;
				return;
			}
			else if (q == start.y)
			{
				a[p][q + 1] = 3;
				a[p - 1][q] = 3;
				a[p - 1][q + 1] = 3;
				cout << p << " " << q + 1 << " " << 3 << endl;
				cout << p - 1 << " " << q << " " << 3 << endl;
				cout << p - 1 << " " << q - 1 << " " << 3 << endl;
				return;
			}
		}
		else if (p == start.x)
		{
			if (q == end.y)
			{
				a[p][q - 1] = 2;
				a[p + 1][q] = 2;
				a[p + 1][q - 1] = 2;
				cout << p << " " << q - 1 << " " << 2 << endl;
				cout << p + 1 << " " << q << " " << 2 << endl;
				cout << p + 1 << " " << q - 1 << " " << 2 << endl;
				return;
			}
			else if (q == start.y)
			{
				a[p][q + 1] = 1;
				a[p + 1][q] = 1;
				a[p + 1][q + 1] = 1;
				cout << p << " " << q - 1 << " " << 1 << endl;
				cout << p + 1 << " " << q << " " << 1 << endl;
				cout << p + 1 << " " << q - 1 << " " << 1 << endl;
				return;
			}
		}

	}
	if (p <= mid.x)//上
	{
		if (q <= mid.y)//左
		{
			//左上
			//将其他位置覆盖:
			a[mid.x][mid.y + 1] = 1;
			a[mid.x + 1][mid.y] = 1;
			a[mid.x + 1][mid.y + 1] = 1;
			cout << mid.x << " " << mid.y + 1 << " " << 1 << endl;
			cout << mid.x + 1 << " " << mid.y << " " << 1 << endl;
			cout << mid.x + 1 << " " << mid.y + 1 << " " << 1 << endl;

			place lust, luend, rust, ruend, ldst, ldend, rdst, rdend;
			//左上角 
			lust.x = start.x;
			lust.y = start.y;
			luend.x = mid.x;
			luend.y = mid.y;
			//右上角 
			rust.x = start.x;
			rust.y = mid.x + 1;
			ruend.x = mid.x;
			ruend.y = end.y;
			//左下角 
			ldst.x = mid.x + 1;
			ldst.y = start.y;
			ldend.x = end.x;
			ldend.y = mid.y;
			//右下角:
			rdst.x = mid.x + 1;
			rdst.y = mid.y + 1;
			rdend.x = end.x;
			rdend.y = end.y;

			//solution(start,mid); 
			solution(lust, luend);
			solution(rust, ruend);
			solution(ldst, ldend);
			solution(rdst, rdend);
		}
		else if (q>mid.y)//右
		{
			place s, e;
			s.x = start.x;
			s.y = mid.y;
			e.x = mid.x;
			e.y = end.y;
			a[mid.x][mid.y] = 2;
			a[mid.x + 1][mid.y] = 2;
			a[mid.x + 1][mid.y + 1] = 2;
			cout << mid.x << " " << mid.y << " " << 2 << endl;
			cout << mid.x + 1 << " " << mid.y << " " << 2 << endl;
			cout << mid.x + 1 << " " << mid.y + 1 << " " << 2 << endl;

			place lust, luend, rust, ruend, ldst, ldend, rdst, rdend;
			//左上角 
			lust.x = start.x;
			lust.y = start.y;
			luend.x = mid.x;
			luend.y = mid.y;
			//右上角 
			rust.x = start.x;
			rust.y = mid.y + 1;
			ruend.x = mid.x;
			ruend.y = end.y;
			//左下角 
			ldst.x = mid.x + 1;
			ldst.y = start.y;
			ldend.x = end.x;
			ldend.y = mid.y;
			//右下角:
			rdst.x = mid.x + 1;
			rdst.y = mid.y + 1;
			rdend.x = end.x;
			rdend.y = end.y;

			//solution(start,mid); 
			solution(lust, luend);
			solution(rust, ruend);
			solution(ldst, ldend);
			solution(rdst, rdend);
		}
	}
	else if (p>mid.x)//下
	{
		if (q <= mid.y)//左 
		{  //左下 
			place s, e;//算出该区域的开始和结束坐标 
			s.x = mid.x;
			s.y = start.y;
			e.x = mid.x;
			e.y = mid.y;
			a[mid.x][mid.y] = 3;//将其他位置覆盖 
			a[mid.x][mid.y + 1] = 3;
			a[mid.x + 1][mid.y + 1] = 3;
			cout << mid.x << " " << mid.y << " " << 3 << endl;
			cout << mid.x << " " << mid.y + 1 << " " << 3 << endl;
			cout << mid.x + 1 << " " << mid.y + 1 << " " << 3 << endl;

			place lust, luend, rust, ruend, ldst, ldend, rdst, rdend;
			//左上角 
			lust.x = start.x;
			lust.y = start.y;
			luend.x = mid.x;
			luend.y = mid.y;
			//右上角 
			rust.x = start.x;
			rust.y = mid.y + 1;
			ruend.x = mid.x;
			ruend.y = end.y;
			//左下角 
			ldst.x = mid.x + 1;
			ldst.y = start.y;
			ldend.x = end.x;
			ldend.y = mid.y;
			//右下角:
			rdst.x = mid.x + 1;
			rdst.y = mid.y + 1;
			rdend.x = end.x;
			rdend.y = end.y;

			//solution(start,mid); 
			solution(lust, luend);
			solution(rust, ruend);
			solution(ldst, ldend);
			solution(rdst, rdend);
		}
		else if (q>mid.y)
		{
			place s, e;
			s.x = mid.x + 1;
			s.y = start.y + 1;
			e.x = end.x;
			e.y = end.y;
			a[mid.x][mid.y] = 4;
			a[mid.x][mid.y + 1] = 4;
			a[mid.x + 1][mid.y] = 4;
			cout << mid.x << " " << mid.y << " " << 4 << endl;
			cout << mid.x << " " << mid.y + 1 << " " << 4 << endl;
			cout << mid.x + 1 << " " << mid.y << " " << 4 << endl;

			place lust, luend, rust, ruend, ldst, ldend, rdst, rdend;
			//左上角 
			lust.x = start.x;
			lust.y = start.y;
			luend.x = mid.x;
			luend.y = mid.y;
			//右上角 
			rust.x = start.x;
			rust.y = mid.x + 1;
			ruend.x = mid.x;
			ruend.y = end.y;
			//左下角 
			ldst.x = mid.x + 1;
			ldst.y = start.y;
			ldend.x = end.x;
			ldend.y = mid.y;
			//右下角:
			rdst.x = mid.x + 1;
			rdst.y = mid.y + 1;
			rdend.x = end.x;
			rdend.y = end.y;

			//solution(start,mid); 
			solution(lust, luend);
			solution(rust, ruend);
			solution(ldst, ldend);
			solution(rdst, rdend);
		}
	}




}

2020/8/4 16:32
加载中...