# 挑战程序竞赛系列（48）：4.2 推理与动态规划算法（1）

CSDN博客 (源链)

## POJ 1082: Calendar Game

```dp[year][month][day]: 表示在某天的输赢态

dp[year][month][day] = {选择同月下一天} || {选择下月同一天}

```import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main{

String INPUT = "./data/judge/201709/P1082.txt";

public static void main(String[] args) throws IOException {
new Main().run();
}

static int[] leap_month    = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
static int[] nonleap_month = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

boolean[][][] dp = new boolean[200][13][32];

void solve() {
int T = ni();

DP();
while (T --> 0) {
int year  = ni();
int month = ni();
int day   = ni();
out.println(dp[year - 1900][month][day] ? "YES" : "NO");
}
}

void DP() {
dp[2001 - 1900][11][3] = true;
dp[2001 - 1900][11][2] = false;
dp[2001 - 1900][11][1] = true;
for (int j = 4; j = 1; --mon) {
for (int j = nonleap_month[mon]; j >= 1; --j) {
// 当前月是否为最后一天
if (j == nonleap_month[mon]) {
dp[101][mon][j] |= !dp[101][mon + 1][1];
}
else {
dp[101][mon][j] |= !dp[101][mon][j + 1];
}

if (j = 0; --year) {
int[] month = leap(year) ? leap_month : nonleap_month;
for (int mon = 12; mon >= 1; --mon) {
for (int day = month[mon]; day >= 1; --day) {
if (mon == 12 && day == month[mon]) { // 1 年中的最后一天
dp[year][mon][day] = !dp[year + 1][1][1] || !dp[year + 1][1][day];
}
else if (day == month[mon]) { // 1个月中的最后一天
dp[year][mon][day] |= !dp[year][mon + 1][1];
if (day <= 0="" 4="=" 12="" 100="" 400="=" month[mon="" +="" 1])="" {="" dp[year][mon][day]="" |="!dp[year][mon" 1][day];="" }="" else="" 其他="" 1];="" if="" (day="" <="month[mon" %="" boolean="" leap(int="" year){="" return="" (year="" &&="" year="" !="0" ||="" 0);="" fastscanner="" in;="" printwriter="" out;="" void="" run()="" throws="" ioexception="" oj;="" try="" oj="!" system.getproperty("user.dir").equals("f:\java_workspace\leetcode");="" catch="" (exception="" e)="" inputstream="" is="oj" ?="" system.in="" :="" new="" fileinputstream(new="" file(input));="" in="new" fastscanner(is);="" out="new" printwriter(system.out);="" long="" s="System.currentTimeMillis();" solve();="" out.flush();="" (!oj){="" system.out.println("["="" (system.currenttimemillis()="" -="" s)="" "ms]");="" public="" more(){="" in.hasnext();="" int="" ni(){="" in.nextint();="" nl(){="" in.nextlong();="" double="" nd(){="" in.nextdouble();="" string="" ns(){="" in.nextstring();="" char="" nc(){="" in.nextchar();="" class="" bufferedreader="" br;="" stringtokenizer="" st;="" hasnext;="" fastscanner(inputstream="" is)="" br="new" bufferedreader(new="" inputstreamreader(is));="" hasnext="true;" nexttoken()="" while="" (st="=" null="" !st.hasmoretokens())="" st="new" stringtokenizer(br.readline());="" "##";="" st.nexttoken();="" next="null;" hasnext(){="" nextint()="" (next="=" null){="" hasnext();="" more="next;" integer.parseint(more);="" nextlong()="" long.parselong(more);="" nextdouble()="" double.parsedouble(more);="" nextstring(){="" more;="" nextchar(){="" more.charat(0);="" }
```

|