ZOJ3513 Human or Pig,博弈论

这题是博弈论。

变成猪的时候,它的移动是没有意识的,所以我们定义一个点为P点,当且仅当猪处于这个点时,不管怎么移动,都能够达到胜利条件(即跳到水里)。

而人是有意识,所以如果一个点能够到达一个P点,则这个点可以放置人,因为人可以有意识的直接去P点。

如果一个点不是P点,也不能放人,它仍是P点,因为当一头猪在这种点时,它随便移动到一个P点或者人点,变回人时,他都可以主动移动到P点再变回猪。

我们知道,在(1,1)这个点的时候,猪是必定跳到水里的,所以(1, 1)是个必胜点,余下就是爆搜了。

/*******************************************************************************
 # Author : Neo Fung
 # Email : neosfung@gmail.com
 # Last modified: 2012-07-12 19:26
 # Filename: ZOJ3513 Human or Pig.cpp
 # Description : 
 ******************************************************************************/
#ifdef _MSC_VER
#define DEBUG
#define _CRT_SECURE_NO_DEPRECATE
#endif

#include <fstream>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <string>
#include <limits.h>
#include <algorithm>
#include <math.h>
#include <numeric>
#include <functional>
#include <ctype.h>
using namespace std;

const int kMAX=1<<16;
char grid_[kMAX];
int n,m;

char& grid(const int &x,const int &y)
{
	return grid_[x*m+y];
}

char Get(const int &x,const int &y)
{
	char &ret = grid(x,y);
	if(ret!='?')
		return ret;
	else
	{
		ret='P';
		for(int k=1;x>k*y;++k)
			if(Get(x-k*y,y) == 'P')
			{
				ret='H';
				break;
			}
		for(int k=1;y>k*x;++k)
			if(Get(x,y-k*x) == 'P')
			{
				ret='H';
				break;
			}
	}
	return ret;
}

int main(void)
{
#ifdef DEBUG  
  freopen("../stdin.txt","r",stdin);
  freopen("../stdout.txt","w",stdout); 
#endif  

	int cases=1;

  while(~scanf("%d%d",&n,&m) )
  {
    ++n;
    ++m;
    printf("Case #%d:\n",cases++);
    memset(grid_,'?',sizeof(grid_));

		for(int i=1;i<n;++i)
		{
			for(int j=1;j<m;++j)
				putchar(Get(i,j));
			puts("");
		}
  }

  return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值