BCHESS - Bàn cờ tướng

Tác giả: RR

Ngôn ngữ: C++

#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;
}

Download