Hướng dẫn giải của Trò chơi đen trắng


Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.

Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này

Code mẫu của RR

{$R+,Q+}
uses math;
const
  FINP='';
  FOUT='';
  MAXN=100001;
var
  f1,f2:text;
  g,n,test:longint;
  a:array[1..MAXN] of char;
procedure openF;
begin
  assign(f1,FINP); reset(f1);
  assign(f2,FOUT); rewrite(f2);
end;
procedure closeF;
begin
  close(f1); close(f2);
end;
procedure inp;
begin
  readln(f1,n);
  fillchar(a,sizeof(a),'0');
  for n:=1 to n do read(f1,a[n]);
  readln(f1);
end;
procedure solve;
var
  l,i,u:longint;
  last:char;
begin
  l:=0;
  for i:=1 to n do
    begin
      if a[i]<>'0' then
        begin
          last:=a[i];
          break;
        end;
      inc(l);
    end;
  if l=n then begin g:=2-n mod 2; exit; end;
  g:=l; l:=0;
  for u:=i+1 to n do
    begin
      if a[u]<>'0' then
        begin
          if (last=a[u]) and (l>0) then g:=g xor 1;
          if (last<>a[u]) and (l>0) then g:=g xor 0;
          last:=a[u]; l:=0;
        end
      else inc(l);
    end;
  g:=g xor l;
end;
procedure ans;
begin
  if g=0 then writeln(f2,2) else writeln(f2,1);
end;
begin
  openF;
  readln(f1,test);
  for test:=1 to test do
    begin
      inp;
      if n=0 then begin writeln(f2,2); continue; end;
      solve;
      ans;
    end;
  closeF;
end.

Code mẫu của hieult

#include <stdio.h>
//#include <conio.h>

int TTD(int a)
{
    if(a<0)
       return -a;
    else return a;
}

int main()
{
    int test,n;
    char s[100001];
    scanf("%d",&test);
    for(int ii = 1;ii<=test;ii++)
    {
        scanf("%d",&n);
        scanf("%s",s);
        int trai = 0,phai = 0,tong = 0,thutu=0,flag = 0;
        for(int i = 0;i<n;i++)
        {
            if(flag == 0)
            {
                if(s[i]=='0')
                    trai++;
                else 
                {
                    flag = 1;
                    thutu = i;
                }
            }
            else if(s[i]!='0')
            {
                if(i-thutu>1 && s[i]==s[thutu])
                    tong++;
                thutu=i;
            }
            else if(i==n-1)
                phai = n-1-thutu;
        }
        if(trai==phai)
        {
            if(tong%2==0) printf("2\n");
            else printf("1\n");
        }
        else if(TTD(trai-phai)==1)
        {
            if(trai%2==0)
            {
                if(trai<phai)
                {
                    if(tong%2==0) printf("1\n");
                    else printf("2\n");
                }
                else printf("1\n");
            }
            else
            {
                if(trai>phai)
                {
                    if(tong%2==0) printf("1\n");
                    else printf("2\n");
                }
                else printf("1\n");
            }
        }
        else printf("1\n");
    }
    //getch();
}

Code mẫu của ll931110

program BWGAME;
const
  input  = '';
  output = '';
  maxn = 100000;
var
  fi,fo: text;
  i,nTest: longint;
  a: array[1..maxn] of longint;
  gr: array[0..maxn,0..2,0..2] of longint;

procedure openfile;
begin
  assign(fi, input);  reset(fi);
  assign(fo, output);  rewrite(fo);
end;

procedure closefile;
begin
  close(fo);  close(fi);
end;

procedure gens;
var
  i: longint;
begin
  fillchar(gr, sizeof(gr), 0);
  for i := 1 to maxn do
    begin
     gr[i,0,1] := i;  gr[i,0,2] := i;  gr[i,1,0] := i;  gr[i,2,0] := i;
     gr[i,1,1] := 1;  gr[i,2,2] := 1;
     if odd(i) then gr[i,0,0] := 1;
    end;
end;

procedure solve;
var
  n,i: longint;
  cc,xa,xb,res: longint;
  ch: char;
begin
  readln(fi, n);
  for i := 1 to n do
    begin
      read(fi, ch);
      a[i] := ord(ch) - ord('0');
    end;

  readln(fi);

  res := 0;
  xa := 0;
  xb := 0;

  i := 1;
  while i <= n do
    begin
      cc := 0;
      while (i <= n) and (a[i] = 0) do
        begin
          inc(i);  inc(cc);
        end;

      if i <= n then xb := a[i];
      res := res xor gr[cc,xa,xb];

      xa := xb;
      xb := 0;
      cc := 0;
      inc(i);
    end;

  if res = 0 then writeln(fo, 2) else writeln(fo, 1);
end;

begin
  openfile;
  gens;

  readln(fi, nTest);
  for i := 1 to nTest do solve;

  closefile;
end.

Code mẫu của khuc_tuan

#include "stdio.h"
#include "string.h"
#include "queue"

using namespace std;

char s[100010];
int n;

int main() {
    int stest;
    scanf("%d",&stest);
    while(stest--) {
        scanf("%d",&n);
        gets(s); gets(s);
        int res = 0 ;
        for(int i=0;i<n;++i) if(s[i]=='0'){
            int j = i;
            while(j+1<n && s[j+1]=='0') ++j;
            int len = j-i+1;
            int a,b;
            if(i==0) a=0; else a='3'-s[i-1];
            if(j==n-1) b=0; else b='3'-s[j+1];

            if(a==b) {
                if(b!=0 || len%2==1) res ^= 1; 
            }
            else if(a==0 || b==0) res ^= len;

            i = j;
        }
        if(res==0) printf("2\n");
        else printf("1\n");
    }
    return 0;
}

Bình luận

Hãy đọc nội quy trước khi bình luận.


Không có bình luận tại thời điểm này.