您的位置:首页 > 其它

zoj3513 Human or Pig----P/N分析 博弈

2012-09-15 15:22 99 查看
HumanorPig

TimeLimit:2Seconds
MemoryLimit:65536KB

Amanislostinastrangeworld.Inthisworld,heisahumaninthedaytime,andhewillbecomeapigatnight.Thestrangeworldisrectanglewhichisseperatedintomany1*1grids,from(0,0)to(X,Y).Everygridhasacoordinate(
x,y).Ifheisinthegrid(x,y),hecanjumptogrid(
x-k*y,y)or(x,y-k*x).kisapositiveinteger.Forexample,ifheisinthegrid(4,9),hecanonlyjumptothegrid(4,1)or(4,5).Atnight,hejumpstoanothergridat1:00AMaspig.Inthedaytime,
hejumpstoanothergridat1:00PMasahuman.Sohewilljumpexactlytwiceeveryday.



Asthefigureshow,thegrids(x,0),(0,y)(0<=x<=X,0<=
y<=Y)aretheriver.WhenheJumpintotheriver,hewillchangefrompigtohumanorchangefromhumantopigimmediately.Andhispropertywillneverchangefromthenon.Itmeansthatifhejumpintotheriverinthedaytime,hewillbeapig
forever.Hewanttojumpintotheriveratnight,sothathechangefrompigtohumanimmediately,andcanbeahumanforever,neverbecomeapigagain.

Whenhebecomeapigatnight,hewilljumptoagridwhosecoordinatesatisfies(
x-k*y,y)or(x,y-k*x)arbitrarily.Hewillnotjumpoutofthestrangeworldeitherinthedaytimeoratnight.Atthebeginning,heisatthegrid(
x0,y0).Toensurethathecanjumpintotheriverasapigatlast,atthebeginning,hecanchoosetostartasapigatnightorasahumaninthedaytime.Youneedtodeterminewhattime(dayornight)tostartineverygridofthestrange
worldexcecpttheriver.Useamatrixtodisplayit.

Input

Therearemultiplecases(nomorethan100).

EachcasecontaintwointegersXandY(1<=X*
Y<=40000)indicatingthesizeofthestrangeworld.

Output

Foreachtestcasei,printcasenumberintheform"Case#i"inonesingleline.Andthereisa
X*Ymatrix.Thejthcharateroftheithlineindicatingwhattime(dayornight)tostartinthegrid(
i,j).'H'meansthattoensurethathecanjumpintotheriverasahuman,heneedstostartasahumaninthedaytime.'P'meansthattoensurethathecanjumpintotheriverasahuman,heneedstostartasapigatnight.

SampleInput

12
23

SampleOutput

Case#1:
PH
Case#2:
PHH
HPP

[code]#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>
usingnamespacestd;

constintkMAX=1<<16;
chargrid_[kMAX];
intn,m;

intgrid(intx,inty)
{
	returnx*m+y;
}

charGet(intx,inty)
{
	intret=grid(x,y);
	charc='P';
for(intk=1;x>k*y;++k)
{
intyy=grid(x-k*y,y);
if(grid_[yy]=='P')
{
				c='H';
				break;
}
}
for(intk=1;y>k*x;++k)
{
intyy=grid(x,y-k*x);
if(grid_[yy]=='P')
{
				c='H';
				break;
}
}
	
	returngrid_[ret]=c;
}

intmain()
{

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

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

return0;
}

[/code]

[code]#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<memory.h>
usingnamespacestd;
intn,m;
intmat[100000];
intget(intx,inty)
{
returnx*m+y;
}
voidsolve(intx,inty)
{
intid=get(x,y);
//if(mat[id]!=-1)return;
boolflag=true;
for(inti=1;;i++)
{
intpx=x-i*y;
if(px<=0)break;
intiid=get(px,y);
if(mat[iid]==0)
{
flag=false;
break;
}
}
if(flag)
{
for(inti=1;;i++)
{
intpy=y-i*x;
if(py<=0)break;//悲催,写成<0wrong了
intiid=get(x,py);
if(mat[iid]==0)
{
flag=false;
break;
}
}
}

if(flag)
mat[id]=0;
else
mat[id]=1;
return;
}
intmain()
{
intcount=1;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(mat,-1,sizeof(mat));
printf("Case#%d:\n",count++);
for(inti=1;i<=n;i++)
for(intj=1;j<=m;j++)
solve(i,j);
intid=1*m+1;
intc=0;
for(inti=id;i<=n*m+m;i++)
{
if(mat[i]==0)cout<<'P';
elsecout<<'H';
c++;
if(c==m)
{
c=0;
cout<<endl;
}
}
}
}

[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: