uva 1561 - Cycle Game(推理)

來源:互聯網
上載者:User

標籤:style   http   color   os   io   for   ar   line   

題目連結:uva 1561 - Cycle Game

題目大意:給出一個環,每次從起點開始,可以選擇一個權值非0的邊移動,移動後減掉權值至少1點。不能移動的為失敗。

解題思路:

  • 1:有0的情況,如果有方向離權值為0的邊的步數為奇數,則為必勝;否則必敗;
  • 2:無0的情況,奇數邊必勝;
  • 3:有1的情況,同0的判斷一樣;
  • 4:無1的情況,只剩偶數邊的情況,必敗;
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 30;int N, arr[maxn];bool check (int k) {    int p = -1;    while (arr[++p] != k);    int q = N;    while (arr[--q] != k);    q = N - 1 - q;    return (p&1) || (q&1);}bool judge () {    for (int i = 0; i < N; i++) {        if (arr[i] == 0)            return check(0);    }    if (N&1)        return true;    for (int i = 0; i < N; i++) {        if (arr[i] == 1)            return check(1);    }    return false;}int main () {    int cas;    scanf("%d", &cas);    while (cas--) {        scanf("%d", &N);        for (int i = 0; i < N; i++)            scanf("%d", &arr[i]);        printf("%s\n", judge() ? "YES" : "NO");    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.