SRM 550 - 500 CheckerExpansion

题目链接:

http://community.topcoder.com/stat?c=problem_statement&pm=11995


题意:

A、B对局,无限棋盘。

第一步A 在(0,0)除下一子。

之后两人交替。

在每一轮,所有满足以下条件的空格子(x, y)都要下子:

(x-1, y-1)为对方棋子,(x-2, y)为空

(x-2, y) 为对方棋子,(x-1, y-1)为空

最终要求输出t 轮后棋盘上一个边长不超过50的矩阵上的下子情况


算法:

这道题描述的是一种分形: Sierpinski_triangle

根据分形本身的特点分治即可。


对于当前大三角形,判断所求点在三角形的哪个子三角形里,如果不属于子三角形则是空点。


然后不断递归下去,每次把当前坐标调整为相对于子三角区域左下角的相对坐标,递归到只剩一个点的时候自然就是有棋子的。

最后如果发现这个点有棋子,那么(x + y)是偶数的是Alice的棋子,(x + y)是奇数的是Bob的棋子,这个观察条件就可以发现。

每次递归三角形高度减半,所以每次判断是log(t)级的。


图片来自TC 官方题解


代码:

#include <string>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <climits>
#include <cmath>
#include <queue>
#include <vector>
#include <stack>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define eps 1e-8
using namespace std;

#define PB push_back
#define MP make_pair

typedef vector <int> VI;
typedef vector <string> VS;
typedef vector <double> VD;
typedef long long LL;
typedef pair <int,int> PII;

class CheckerExpansion
{
	public:

		bool cal(long long x, long long y, long long lim)
		{
			if(lim == 1)
			{
				return true;
			}
			if(x >= 2 * lim - 1)
			{
				return false;
			}
			if(x < y)
			{
				return false;
			}
			if(y >= 2 * lim - x - 1)
			{
				return false;
			}
			if((y < lim / 2) && (x >= lim - y - 1 && x <= lim + y - 1))
			{
				return false;
			}
			if(y < lim / 2)
			{
				if(x < lim)
				{
					return cal(x, y, lim / 2);
				}
				else
				{
					return cal(x - lim, y, lim / 2);
				}
			}
			return cal(x - lim / 2, y - lim / 2, lim / 2);
		}

		vector <string> resultAfter(long long t, long long x0, long long y0, int w, int h)
		{
			VS ans;
			ans.clear();
			string str = "";
			for(int i = 0; i < w; i ++)
			{
				str += '.';
			}
			for(int i = 0; i < h; i ++)
			{
				ans.PB(str);
			}
			for(int i = 0; i < h; i ++)
			{
				for(int j = 0; j < w; j ++)
				{
					if(x0 + j + y0 + h - i - 1 > t * 2 - 2)
					{
						continue;
					}
					if(cal(x0 + j, y0 + h - i - 1, 1LL << 50))
					{
						if(((x0 + j + y0 + h - i - 1) >> 1) & 1)
						{
							ans[i][j] = 'B';
						}
						else		                                       
						{
							ans[i][j] = 'A';						
						}
					}
				}
			}
			return ans;
		}
};

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值