Hướng dẫn giải của Bàn cờ tướ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 flashmt

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

char s[2222];
int n, f[2222][2222], g[2222][2222], ans[3], cnt[3];

void update(int val, int &ans, int &cnt)
{
    if (val > ans) ans = val, cnt = 1;
    else cnt += val == ans;
}

int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++) 
    {
        scanf("%s", s + 1);
        for (int j = 1; j <= n; j++)
            if (s[j] == '0')
            {
                f[i][j] = min(f[i - 1][j - 1], min(g[i - 1][j], g[i][j - 1])) + 1;
                g[i][j] = 0;
                update(f[i][j] - 1 + f[i][j] % 2, ans[2], cnt[2]);
                update(f[i][j] - f[i][j] % 2, ans[0], cnt[0]);
            }
            else
            {
                g[i][j] = min(g[i - 1][j - 1], min(f[i - 1][j], f[i][j - 1])) + 1;
                f[i][j] = 0;
                update(g[i][j] - 1 + g[i][j] % 2, ans[1], cnt[1]);
                update(g[i][j] - g[i][j] % 2, ans[0], cnt[0]);
            }
    }

    for (int i = 0; i < 3; i++) cout << ans[i] << ' ' << cnt[i] << endl;
}

Code mẫu của happyboy99x

#include<cstdio>
#include<algorithm>
using namespace std;

const int N = 2000 + 5;
char a[N][N];
int f[N][N], s[3], t[3], n;

void update(int type, int edge) {
    if(edge > s[type]) s[type] = edge, t[type] = 1;
    else if(edge == s[type]) ++t[type];
}

int main() {
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i)
        scanf("%s", a[i] + 1);
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= n; ++j) {
            f[i][j] = 1;
            if(a[i][j] == a[i-1][j-1] && a[i][j-1] == a[i-1][j] && a[i][j] != a[i][j-1])
                f[i][j] = min(f[i-1][j], min(f[i][j-1], f[i-1][j-1])) + 1;
            update(0, f[i][j] - f[i][j] % 2);
            update(0x32 - a[i][j], f[i][j] - (f[i][j] - 1) % 2);
        }
    for(int i = 0; i < 3; ++i)
        printf("%d %d\n", s[i], t[i]);
    return 0;
}

Code mẫu của ladpro98

program bchess;
uses    math;
const   fi='';
        maxn=2222;
var     n:longint;
        a:array[1..maxn] of ansistring;
        f1a,f1b,f2,f3:array[1..maxn,1..maxn] of int64;
        res1,res2,res3,c1,c2,c3:int64;

procedure input;
var     inp:text;
        i,j:longint;
begin
        assign(inp,fi);
        reset(inp);
        readln(inp,n);
        for i:=1 to n do
        readln(inp,a[i]);
        close(inp);
end;

function min3(a,b,c:int64):int64;
begin
        exit(min(min(a,b),c));
end;

procedure process;
var     i,j:longint;
begin
        for j:=1 to n do
        begin
                if a[1][j]='0' then
                begin
                        f2[1,j]:=0;
                        f3[1,j]:=1;
                        res3:=1;
                end
                else
                begin
                        f2[1,j]:=1;
                        f3[1,j]:=0;
                        res2:=1
                end;
                f1a[1,j]:=0;
                f1b[1,j]:=0;

        end;
        for i:=1 to n do
        begin
                if a[i][1]='0' then
                begin
                        f2[i,1]:=0;
                        f3[i,1]:=1;
                        res3:=1;

                end
                else
                begin
                        f2[i,1]:=1;
                        f3[i,1]:=0;
                        res2:=1;

                end;
                f1a[i,1]:=0;
                f1b[i,1]:=0;
        end;

        for i:=2 to n do
        for j:=2 to n do
        begin
                if (a[i][j]=a[i-1][j-1]) and (a[i-1][j]=a[i][j-1]) then
                begin
                        if a[i][j]='0' then
                        begin
                                f1a[i,j]:=min3(f2[i,j-1],f2[i-1,j],f3[i-1,j-1])+1;
                                f1b[i,j]:=0;
                                f2[i,j]:=0;
                                f3[i,j]:=min3(f1b[i,j-1],f1b[i-1,j],f1a[i-1,j-1])+1;
                        end
                        else
                        begin
                                f1a[i,j]:=0;
                                f1b[i,j]:=min3(f3[i,j-1],f3[i-1,j],f2[i-1,j-1])+1;
                                f2[i,j]:=min3(f1a[i,j-1],f1a[i-1,j],f1b[i-1,j-1])+1;
                                f3[i,j]:=0;
                        end;
                end
                else
                if a[i][j]='0' then
                        f3[i,j]:=1
                else
                        f2[i,j]:=1;
        end;
end;

procedure output;
var     i,j:longint;
begin
        for i:=1 to n do
        for j:=1 to n do
        begin
                res1:=max(res1,max(f1a[i,j],f1b[i,j]));
                res2:=max(res2,f2[i,j]);
                res3:=max(res3,f3[i,j]);
        end;
        for i:=1 to n do
        for j:=1 to n do
        begin
                if a[i][j]='0' then
                begin
                        if (res1>0) and (f1a[i,j]=res1) then inc(c1);
                        if (res3>0) and (f3[i,j]=res3) then inc(c3);
                end
                else
                begin
                        if (res1>0) and (f1b[i,j]=res1) then inc(c1);
                        if (res2>0) and (f2[i,j]=res2) then inc(c2);
                end;
        end;
        writeln(res1,' ',c1);
        writeln(res2,' ',c2);
        writeln(res3,' ',c3);
end;

begin
        input;
        process;
        output;
end.

Code mẫu của RR

#include <sstream>
#include <iomanip>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <string>
#include <deque>
#include <complex>

#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define FORN(i,a,b) for(int i=(a),_b=(b);i<_b;i++)
#define DOWN(i,a,b) for(int i=a,_b=(b);i>=_b;i--)
#define SET(a,v) memset(a,v,sizeof(a))
#define sqr(x) ((x)*(x))
#define ll long long
#define F first
#define S second
#define PB push_back
#define MP make_pair

#define DEBUG(x) cout << #x << " = "; cout << x << endl;
#define PR(a,n) cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl;
#define PR0(a,n) cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl;
using namespace std;

//Buffer reading
int INP,AM,REACHEOF;
const int BUFSIZE = (1<<12) + 17;
char BUF[BUFSIZE+1], *inp=BUF;
#define GETCHAR(INP) { \
    if(!*inp && !REACHEOF) { \
        memset(BUF,0,sizeof BUF);\
        int inpzzz = fread(BUF,1,BUFSIZE,stdin);\
        if (inpzzz != BUFSIZE) REACHEOF = true;\
        inp=BUF; \
    } \
    INP=*inp++; \
}
#define DIG(a) (((a)>='0')&&((a)<='9'))
#define GN(j) { \
    AM=0;\
    GETCHAR(INP); while(!DIG(INP) && INP!='-') GETCHAR(INP);\
    if (INP=='-') {AM=1;GETCHAR(INP);} \
    j=INP-'0'; GETCHAR(INP); \
    while(DIG(INP)){j=10*j+(INP-'0');GETCHAR(INP);} \
    if (AM) j=-j;\
}
//End of buffer reading

const long double PI = acos((long double) -1.0);
const int MN = 2011;

char a[MN][MN];
int f[MN][MN], g[MN][MN], n, res[5], cnt[5];

#define min3(x,y,z) min(min(x,y),z)

void update(int x, int k) {
    if (x > res[k]) {
        res[k] = x;
        cnt[k] = 1;
    }
    else if (x == res[k]) {
        cnt[k]++;
    }
}

int main() {
    scanf("%d\n", &n);
    FOR(i,1,n) scanf("%s\n", &a[i][1]);

    FOR(i,1,n) FOR(j,1,n) {
        if (a[i][j] == '0') {
            g[i][j] = 0;
            f[i][j] = min3(f[i-1][j-1], g[i-1][j], g[i][j-1]) + 1;
        }
        else {
            f[i][j] = 0;
            g[i][j] = min3(g[i-1][j-1], f[i-1][j], f[i][j-1]) + 1;
        }

        if (f[i][j] % 2 == 0) {
            update(f[i][j], 1);
            update(f[i][j]-1, 2);
        }
        else {
            update(f[i][j], 2);
            update(f[i][j]-1, 1);
        }

        if (g[i][j] % 2 == 0) {
            update(g[i][j], 1);
            update(g[i][j]-1, 3);
        }
        else {
            update(g[i][j], 3);
            update(g[i][j]-1, 1);
        }
    }

    swap(res[2], res[3]);
    swap(cnt[2], cnt[3]);
    FOR(i,1,3) printf("%d %d\n", res[i], cnt[i]);
    return 0;
}

Code mẫu của hieult

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <algorithm>

#include <ctime>
#include <deque>
#include <bitset>
#include <cctype>
#include <utility>
#include <cassert>

using namespace std;

typedef long long ll;
typedef double ld;
typedef unsigned int ui;
typedef unsigned long long ull;

#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)
#define Fit(i,v) for(__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
//#define Fitd(i,v) for(__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define ms(a,x) memset(a, x, sizeof(a))

template<class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; }
template<class T> T gcd(T a, T b) { T r; while (b != 0) { r = a % b; a = b; b = r; } return a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> T sqr(T x) { return x * x; }
template<class T> T cube(T x) { return x * x * x; }
template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); }

const int bfsz = 1 << 16;
char bf[bfsz + 5];
int rsz = 0;
int ptr = 0;
char gc() {
    if (rsz <= 0) {
        ptr = 0;
        rsz = (int) fread(bf, 1, bfsz, stdin);
        if (rsz <= 0)
            return EOF;
    }
    --rsz;
    return bf[ptr++];
}
void ga(char &c) {
    c = EOF;
    while (!isalpha(c))
        c = gc();
}
int gs(char s[]) {
    int l = 0;
    char c = gc();
    while (isspace(c))
        c = gc();
    while (c != EOF && !isspace(c)) {
        s[l++] = c;
        c = gc();
    }
    s[l] = '\0';
    return l;
}
template<class T> bool gi(T &v) {
    v = 0;
    char c = gc();
    while (c != EOF && c != '-' && !isdigit(c))
        c = gc();
    if (c == EOF)
        return false;
    bool neg = c == '-';
    if (neg)
        c = gc();
    while (isdigit(c)) {
        v = v * 10 + c - '0';
        c = gc();
    }
    if (neg)
        v = -v;
    return true;
}

typedef pair<int, int> II;

const ld PI = acos(-1.0);
const ld eps = 1e-9;

const int dr[] = {0, +1, 0, -1};
const int dc[] = {+1, 0, -1, 0};

const int inf = (int)1e9 + 5;
const ll linf = (ll)1e16 + 5;
//const ll mod = (ll)1e9 + 7;

#define maxn 2005

char s[maxn][maxn];
int f[maxn][maxn];
int n;

int  main()
{
//  freopen("in.txt", "r", stdin);
    cin >> n;
    Rep(i, n) scanf("%s", s[i]);
    ms(f, 0);
    int MAX[3], num[3];
    ms(MAX, 0); ms(num, 0);
    For(i, 1, n) For(j, 1, n){
        int c = '2' - s[i - 1][j - 1];
        if(i == 1 || j == 1){
            f[i][j] = 1;
        }
        else if(s[i - 2][j - 2] == s[i - 1][j - 1] && s[i - 2][j - 1] != s[i - 1 ][j - 1] && s[i - 1][j - 2] != s[i - 1][j - 1]){
            f[i][j] = min(f[i - 1][j], min(f[i][j - 1], f[i - 1][j - 1])) + 1;
        }
        else{
            f[i][j] = 1;
        }

        if(f[i][j] % 2 == 0){
            if(MAX[0] < f[i][j]){
                MAX[0] = f[i][j];
                num[0] = 1;
            }
            else if(MAX[0] == f[i][j]) num[0]++;
            if(MAX[c] < f[i][j] - 1){
                MAX[c] = f[i][j] - 1;
                num[c] = 1;
            }
            else if(MAX[c] == f[i][j] - 1) num[c]++;
        }

        else{
            if(MAX[c] < f[i][j]){
                MAX[c] = f[i][j];
                num[c] = 1;
            }
            else if(MAX[c] == f[i][j]) num[c]++;
            if(MAX[0] < f[i][j] - 1){
                MAX[0] = f[i][j] - 1;
                num[0] = 1;
            }
            else if(MAX[0] == f[i][j] - 1) num[0]++;
        }
    }

    Rep(i, 3) if(MAX[i] == 0) num[i] = 0;
    Rep(i, 3) cout << MAX[i] << " " << num[i] << endl;
}

Code mẫu của skyvn97

#include<stdio.h>
#define MAX   2001
int a[MAX][MAX];
int f[MAX][MAX];
int n;
int m1,m2,m3;
int c1,c2,c3;
int min(int x,int y,int z)
{
    int m;
    m=x;
    if (y<m) m=y;
    if (z<m) m=z;
    return (m);
}
void init(void)
{
    char s[MAX];
    int i,j;
    scanf("%d",&n);
    for (i=1;i<=n;i=i+1)
        {
         scanf("%s",&s);
         for (j=1;j<=n;j=j+1)
             a[i][j]=s[j-1]-48;
        }   
    m1=1;m2=1;m3=1;
    c1=0;c2=0;c3=0;
}
void optimize(void)
{
     int i,j;
     for (i=1;i<=n;i=i+1)
         {
          f[i][1]=1;
          f[1][i]=1;
         }
     for (i=2;i<=n;i=i+1)
         for (j=2;j<=n;j=j+1)
             {
              if ((a[i][j]==a[i-1][j-1]) && (a[i][j]!=a[i-1][j]) && (a[i][j]!=a[i][j-1]))
                 f[i][j]=min(f[i-1][j],f[i-1][j-1],f[i][j-1])+1;
              else f[i][j]=1;              
              if ((a[i][j]==1) && (m2<f[i][j]-1+f[i][j]%2)) m2=f[i][j]-1+f[i][j]%2;
              if ((a[i][j]==0) && (m3<f[i][j]-1+f[i][j]%2)) m3=f[i][j]-1+f[i][j]%2;
              if (m1<f[i][j]-f[i][j]%2) m1=f[i][j]-f[i][j]%2;
             }    
     for (i=1;i<=n;i=i+1)
         for (j=1;j<=n;j=j+1)
             {
              if (f[i][j]>=m1) c1++;
              if ((a[i][j]==1) && (f[i][j]>=m2)) c2++;
              if ((a[i][j]==0) && (f[i][j]>=m3)) c3++;
             }
     printf("%d %d\n",m1,c1);
     printf("%d %d\n",m2,c2);
     printf("%d %d\n",m3,c3);
}
int main(void)
{
    init();
    optimize();
}

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.