Hướng dẫn giải của Rocks Game


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 flashmt

const fi='';
      fo='';
      maxn=15;
var n,i:longint;
    p:array[0..maxn] of longint;
    d:array[0..1 shl maxn] of boolean;
    a:array[1..1 shl maxn+10] of word;

procedure att(i:longint);
var j,x,k,q:longint;
begin
     for j:=0 to n-1 do
     begin
          x:=a[i-1] xor p[j];
          if (i=p[n]+1) and (x=0) then
          begin
               a[i]:=0;
               for k:=1 to i do
               begin
                    for q:=0 to n-1 do
                         if a[k] shr q and 1=1 then write('X') else write('O');
                    writeln;
               end;
               close(output); halt;
          end;
          if not d[x] then
          begin
               a[i]:=x;
               d[x]:=true;
               att(i+1);
               d[x]:=false;
          end;
     end;
end;

begin
     assign(input,fi); reset(input);
     assign(output,fo); rewrite(output);
     read(n);
     close(input);
     for i:=0 to maxn do p[i]:=1 shl i;
     d[0]:=true; a[1]:=0;
     att(2);
end.

Code mẫu của happyboy99x

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long long LL;

#define sz(a) (int((a).size()))
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define tr(c,i) for(typeof((c).begin()) i = (c).begin(), _e = (c).end(); i != _e; ++i)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define rep(i,n) for(int i = 0, _n = (n); i < _n; ++i)
#define repd(i,n) for(int i = (n)-1; i >= 0; --i )
#define fo(i,a,b) for(int i = (a), _b = (b); i <= _b; ++i)
#define fod(i,a,b) for(int i = (a), _b = (b); i >= _b; --i)

#define INF 1000000000
#define N

int f[(1<<15)+1];

int main() {
    int n; scanf("%d",&n);
    rep(k,n) for(int i=0, j = (1<<k+1)-1; i < j; ++i, --j)
        f[j] = f[i] | (1 << k);
    rep(i,(1<<n)+1) {
        repd(j,n) putchar(f[i]&(1<<j) ? 'X' : 'O');
        putchar(10);
    }
    return 0;
}

Code mẫu của ladpro98

program rocks;
uses    math;
const   maxN=15;
        maxV=1 shl maxN;
        fi='';
var     f:array[1..maxN,0..maxV] of longint;
        n:longint;

procedure input;
var     f:text;
begin
        assign(f,fi);
        reset(f);
        readln(f,n);
        close(f);
end;

function getbit(n,i:longint):longint;
begin
        exit((n shr (i-1)) and 1);
end;

procedure graycode;
var     i,j:longint;
begin
        f[1,1]:=0;
        f[1,2]:=1;
        for i:=2 to n do
        for j:=1 to 1 shl i do
        begin
                if j<=(1 shl (i-1)) then
                f[i,j]:=f[i-1,j]
                else
                f[i,j]:=f[i-1,(1 shl i)-j+1]+(1 shl (i-1));
        end;
end;

procedure output;
var     i,j:longint;
begin
        for i:=1 to 1 shl n do
        begin
                for j:=1 to n do
                if getbit(f[n,i],j)=1 then write('X')
                else write('O');
                writeln;
        end;
        for i:=1 to n do
        write('O');
end;

begin
        input;
        graycode;
        output;
end.

Code mẫu của RR

#include <iostream>
#include <algorithm>
#define FOR(i,a,b) for(long i=a; i<=b; i++)
using namespace std;

long a[20],n;

void solve(long i) {
    if (i==n+1) {
        FOR(i,1,n)
            if (a[i]) printf("X");
            else printf("O");
        printf("\n");
        return ;
    }
    solve(i+1);
    a[i]=1-a[i];
    solve(i+1);
}

int main() {
//    freopen("input.txt","r",stdin);
//    freopen("output.txt","w",stdout);
    scanf("%ld",&n);
    solve(1);
    FOR(i,1,n) printf("O");
    printf("\n");
    return 0;
}

Code mẫu của hieult

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

main()

{
      int s[35000][17],C[17],n;
      scanf("%d",&n);
      C[0]=1;
      for(int i=1;i<=n;i++)
          C[i]=C[i-1]*2;
      for(int i=1;i<=n;i++)
      {
          for(int j=1;j<=C[i-1];j++)
              s[j][i]=0;
          for(int j=C[i-1]+1;j<=C[i];j++)
          {
              for(int k=1;k<=i-1;k++)
                  s[j][k]=s[C[i]+1-j][k];
              s[j][i]=1;
          }
      }
      for(int i=1;i<=C[n];i++)
      {
          for(int j=1;j<=n;j++)
          {
              if(s[i][j]==0)
                  printf("O");
              else printf("X");
          }
          printf("\n");
      }
      for(int i=1;i<=n;i++)
          printf("O");
      //getch();
}

Code mẫu của ll931110

{
ID: ll9311102
PROB: rocks
LANG: PASCAL
}

program rocks;
const
  input  = '';
  output = '';
  maxn = 16;
  maxk = 100000;
type
  pnode = ^tnode;
  tnode = record
    val: longint;
    link: pnode;
  end;
var
  a: array[0..maxk] of pnode;
  fin,list: array[0..maxk] of longint;
  free: array[0..maxk] of boolean;
  flag: boolean;
  n: longint;
  fi,fo: text;

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

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

procedure add(x,y: longint);
var
  p: pnode;
begin
  new(p);
  p^.val := y;
  p^.link := a[x];
  a[x] := p;
end;

procedure load;
var
  i,j,v: longint;
begin
  readln(fi, n);
  for i := 0 to 1 shl n do a[i] := nil;
  for i := 0 to 1 shl n - 1 do
      for j := 0 to n - 1 do
        begin
          if i and (1 shl j) = 0 then v := i + (1 shl j) else v := i - (1 shl j);
          add(i,v);
        end;
end;

procedure Ham(i: longint);
var
  p: pnode;
  u,v: longint;
begin
  if flag then exit;
  u := list[i - 1];
  free[u] := false;
  p := a[u];

  while p <> nil do
    begin
      v := p^.val;
      if (v = 0) and (i = 1 shl n) then
        begin
          flag := true;
          fin := list;
          exit;
        end;

      if free[v] then
        begin
          list[i] := v;
          if i < 1 shl n then Ham(i + 1);
        end;

      p := p^.link;
    end;
end;

procedure solve;
begin
  flag := false;
  fillchar(free, sizeof(free), true);
  Ham(1);
end;

procedure printresult;
var
  i,j: longint;
  u: longint;
begin
  for i := 0 to 1 shl n do
    begin
      u := list[i];
      for j := n - 1 downto 0 do
        if u and (1 shl j) = 0 then write(fo, 'O') else write(fo, 'X');
      writeln(fo);
    end;
end;

begin
  openfile;
  load;
  solve;
  printresult;
  closefile;
end.

Code mẫu của khuc_tuan

#include <string>
#include <iostream>
#include <vector>
using namespace std;

vector<string> gen(int n) {
    vector<string> res;
    if(n == 1) {
        res.push_back("O"); res.push_back("X");
    }
    else {
        vector<string> tmp = gen(n - 1);
        for(int i=0;i<tmp.size();++i) res.push_back("O" + tmp[i]);
        for(int i=tmp.size()-1;i>=0;--i) res.push_back("X" + tmp[i]);   
    }
    return res;
}

int main() {
    int n;
    cin >> n;
    vector<string> res = gen(n);
    for(int i=0;i<res.size();++i) cout << res[i] << endl;
    cout << string(n,'O') << endl;
    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.