Ultraviolet A 1557-calendar game (game)

Source: Internet
Author: User

Link: Ultraviolet A 1557-calendar game

Given a date, you can add a month or a day each time. The premise is that the next month has a corresponding date, for example, it is illegal to change from 1.30 plus one month to 2.30, and the maximum date is. Two people take turns and cannot perform the operation as a failure.

Solution: DP [y] [m] [d] indicates whether the corresponding date is the first day. You can do this in advance. Pay attention to the details, including the leap year. Share code.

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int Y = 105;const int D = 15;const int M = 35;const int month[D] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};int dp[Y][D][M];bool is_year (int y) {    if (y % 100 == 0 && y % 400 == 0)        return true;    if (y % 4 == 0 && y % 100)        return true;    return false;}int get_day (int y, int m) {    if (y == 2001 && m == 11)        return 4;    if (m == 2 && is_year(y))        return 29;    return month[m];}bool judge_day (int y, int m, int d) {    if (y > 101)        return false;    if (y == 101 && m > 11)        return false;    if (y == 101 && m == 11 && d > 4)        return false;    return true;}bool getnext_day (int& yy, int& mm, int& dd, int y, int m, int d, int type) {    if (type) {        dd = d;        mm = m + 1;        if (mm > 12) {            yy = y + 1;            mm = 1;        } else            yy = y;        if (dd > get_day(yy + 1900, m))            return false;    } else {        dd = d + 1;        if (dd > get_day(y + 1900, m)) {            dd = 1;            mm = m + 1;        } else            mm = m;        if (mm > 12) {            yy = y + 1;            mm = 1;        } else            yy = y;    }    return judge_day(yy, mm, dd);}int SG (int y, int m, int d) {    if (dp[y][m][d] != -1)        return dp[y][m][d];    dp[y][m][d] = 0;    int yy, mm, dd;    if (getnext_day(yy, mm, dd, y, m, d, 0)) {        if (SG(yy, mm, dd) == false)            dp[y][m][d] = 1;    }    if (getnext_day(yy, mm, dd, y, m, d, 1)) {        if (SG(yy, mm, dd) == false)            dp[y][m][d] = 1;    }    return dp[y][m][d];}void init () {    memset(dp, -1, sizeof(dp));    dp[101][11][4] = 0;    for (int i = 0; i <= 101; i++) {        int limit_month = (i == 101 ? 11 : 12);        for (int j = 1; j <= limit_month; j++) {            int limit_day = get_day(1900+i, j);            for (int d = 1; d <= limit_day; d++)                SG(i, j, d);        }    }}int main () {    init();    int cas, y, m, d;    scanf("%d", &cas);    while (cas--) {        scanf("%d%d%d", &y, &m, &d);        printf("%s\n", dp[y-1900][m][d] ? "YES" : "NO");    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.