地址:http://acm.hdu.edu.cn/showproblem.php?pid=1404
Digital Deletions
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1764 Accepted Submission(s): 630
Problem Description
Digital deletions is a two-player game. The rule of the game is as following.
Begin by writing down a string of digits (numbers) that's as long or as short as you like. The digits can be 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 and appear in any combinations that you like. You don't have to use them all. Here is an example:
On a turn a player may either:
Change any one of the digits to a value less than the number that it is. (No negative numbers are allowed.) For example, you could change a 5 into a 4, 3, 2, 1, or 0.
Erase a zero and all the digits to the right of it.
The player who removes the last digit wins.
The game that begins with the string of numbers above could proceed like this:
Now, given a initial string, try to determine can the first player win if the two players play optimally both.
Begin by writing down a string of digits (numbers) that's as long or as short as you like. The digits can be 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 and appear in any combinations that you like. You don't have to use them all. Here is an example:

On a turn a player may either:
Change any one of the digits to a value less than the number that it is. (No negative numbers are allowed.) For example, you could change a 5 into a 4, 3, 2, 1, or 0.
Erase a zero and all the digits to the right of it.
The player who removes the last digit wins.
The game that begins with the string of numbers above could proceed like this:

Now, given a initial string, try to determine can the first player win if the two players play optimally both.
Input
The input consists of several test cases. For each case, there is a string in one line.
The length of string will be in the range of [1,6]. The string contains only digit characters.
Proceed to the end of file.
The length of string will be in the range of [1,6]. The string contains only digit characters.
Proceed to the end of file.
Output
Output Yes in a line if the first player can win the game, otherwise output No.
Sample Input
0 00 1 20
Sample Output
Yes Yes No No
思路:因为只有长度为6的字符串,所以上来搜索,结果超时。然后建个表,将搜索的结果表存在表中避免第二次搜,A了。(有点状态压缩的感觉)
代码:
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define LL __int64
int ans[1000010];
int dfs(char ma[])
{
if(ma[0]=='0') return 1;
int len=strlen(ma);
if(!len) return 0;
int num=0;
for(int i=0;i<len;i++) num*=10,num+=(ma[i]-'0');
if(~ans[num]) return ans[num];
for(int i=0;i<len;i++)
{
char mb[10];
if(ma[i]=='0')
{
for(int j=0;j<i;j++)
mb[j]=ma[j];
mb[i]='\0';
if(!dfs(mb)) return ans[num]=1;
}
else
{
strcpy(mb,ma);
for(int j=0;j<ma[i]-'0';j++)
{
mb[i]=j+'0';
if(!dfs(mb)) return ans[num]=1;
}
}
}
return ans[num]=0;
}
int main()
{
memset(ans,-1,sizeof(ans));
char a[10];
while(scanf("%s",&a)>0)
puts(dfs(a)?"Yes":"No");
return 0;
}