您的位置:首页 > 其它

HDOJ1564Play a game(找规律博弈)

2013-07-10 12:03 387 查看
原题--题目链接

题目大意:题目意思是输出里面的两个人玩走方格的游戏,谁走了最后一步就赢了

这道题一开始木有思路,后来想了一下搜索,就大概试了一下,代码如下:

#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <map>
#include <vector>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cmath>
#include <queue>
#include <set>
#include <stack>
using namespace std;
int min(int a, int b)
{
if(a<=b)
return a;
return b;
}
int max(int a, int b)
{
if(a>=b)
return a;
return b;
}
double min(double a, double b)
{
if(a<=b)
return a;
return b;
}
double max(double a, double b)
{
if(a>=b)
return a;
return b;
}
int n ;
int s[1010][1010] ;
int direct[4][2] = {-1,0,1,0,0,-1,0,1} ;
int DFS(int x, int y)
{
int x1, y1;
int i, ans ;
for (i = 0 ; i < 4 ; i++)
{
x1 = x + direct[i][0] ;
y1 = y + direct[i][1] ;
if (x1 < 0 || x1 >= n) continue ;
if (y1 < 0 || y1 >= n) continue ;//判定是否在图内
if (s[x1][y1]) continue ;
s[x1][y1] = 1 ;
ans = DFS(x1, y1) ;
s[x1][y1] = 0 ;
if (ans == 0)
return 1 ;   //如果下一点是必败点  那么该点一定是必胜点;
}
return 0;
}
int main ()
{
s[0][0] = 1 ;
for (n = 1 ; n <= 7; n++)
printf ("%d, %d\n", n, DFS(0,0)) ;
return 0;
}
通过搜索前几个数字,发现这个和n的奇偶性有关,在看了其他的一些讨论,都得出来奇偶性的特点

#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <map>
#include <vector>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cmath>
#include <queue>
#include <set>
#include <stack>
using namespace std;
int min(int a, int b)
{
if(a<=b)
return a;
return b;
}
int max(int a, int b)
{
if(a>=b)
return a;
return b;
}
double min(double a, double b)
{
if(a<=b)
return a;
return b;
}
double max(double a, double b)
{
if(a>=b)
return a;
return b;
}
int main()
{
int n;
while(scanf("%d",&n),n)
{
if(!(n %= 2))
printf("8600\n");
else printf("ailyanlu\n");
}
return 0;
}
还有什么办法呢?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  找规律 博弈 搜索