Editorial for VO 12 Bài 2 - Mã đi tuần


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

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 <string>
using namespace std;
const int dx[]={-2,-1,1,2,2,1,-1,-2};
const int dy[]={1,2,2,1,-1,-2,-2,-1};

int main()
{
    int n,k,x=0,y=0,maxx=0,maxy=0,minx=0,miny=0;
    string s;
    cin >> n >> k >> s;
    for (int i=0;i<k;i++)
    {
        x+=dx[s[i]-'1']; y+=dy[s[i]-'1'];
        maxx=max(maxx,x); minx=min(minx,x);
        maxy=max(maxy,y); miny=min(miny,y);
    }
    cout << (n-maxx+minx)*(n-maxy+miny) << endl;
}

Code mẫu của happyboy99x

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

int d[8][2] = {{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2},{1,-2},{2,-1}};
int n, k;
char s[1001];

int main() {
    scanf("%d%d%s", &n, &k, s);
    int x = 0, y = 0, minRow = 0, maxRow = 0, minCol = 0, maxCol = 0;
    for(int i = 0; i < k; ++i) {
        x += d[s[i] - '1'][0]; y += d[s[i] - '1'][1];
        minRow = min(minRow, x);
        minCol = min(minCol, y);
        maxRow = max(maxRow, x);
        maxCol = max(maxCol, y);
    }
#ifdef __WATASHI
    printf("%d %d %d %d\n", minRow, maxRow, minCol, maxCol);
#endif
    printf("%d\n", max(0, (n - maxRow + minRow) * (n - maxCol + minCol)));
    return 0;
}

Code mẫu của ladpro98

program lknight;
uses    math;
const   fi='';
        dx:array[1..8] of longint = (-2,-1,1,2,2,1,-1,-2);
        dy:array[1..8] of longint = (1,2,2,1,-1,-2,-2,-1);
var     n,k,c,x,y,maxx,maxy,minx,miny,res,i,j:longint;
        cc:char;
        inp:text;
function inBound(i,j:longint):boolean;
begin
        exit((1<=i) and (i<=n) and (1<=j) and (j<=n));
end;

begin
        assign(inp,fi);reset(inp);
        readln(inp,n,k);
        for i:=1 to k do
        begin
                read(inp,cc);
                c:=ord(cc)-48;
                inc(x,dx[c]);
                inc(y,dy[c]);
                maxX:=max(maxX,x);
                minX:=min(minX,x);
                maxY:=max(maxY,y);
                minY:=min(minY,y);
        end;
        for i:=1 to n do
        for j:=1 to n do
        if inBound(i+maxX,j+maxY) and inBound(i+minX,j+minY) then
        inc(res);
        write(res);
end.

Code mẫu của RR

#include <iostream>
#include <algorithm>
#include <fstream>

using namespace std;

const int di[] = {-2, -1, 1, 2,  2,  1, -1, -2};
const int dj[] = { 1,  2, 2, 1, -1, -2, -2, -1};

int n;

void maintain(int &x) {
    if (x < 1) x = 1;
    if (x > n) x = n;
}

int main() {
    int k;
    cin >> n >> k;
    int left = 1, right = n, up = 1, down = n;

    string s;
    cin >> s;
    for(int i = 0; i < k; i++) {
        int dir = s[i] - '1';
        left += dj[dir];
        right += dj[dir];
        up += di[dir];
        down += di[dir];
        maintain(left);
        maintain(right);
        maintain(up);
        maintain(down);
    }

    cout << (right - left + 1) * (down - up + 1);
    return 0;
}

Code mẫu của hieult

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <algorithm>

using namespace std;

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

#define Rep(i,n) for(__typeof(n) i = 0; i < (n); ++i)
#define Repd(i,n) for(__typeof(n) i = (n)-1; i >= 0; --i)
#define For(i,a,b) for(__typeof(b) i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(__typeof(a) 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))
#define nl puts("")
#define sp printf(" ")
#define ok puts("ok")
//#include <conio.h>

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> void db(T a, int p = -1) { if (p >= 0) cout << fixed << setprecision(p); cout << a << " "; }
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> struct Triple { T x, y, z; Triple() {} Triple(T _x, T _y, T _z) : x(_x), y(_y), z(_z) {} };
template<class T> Triple<T> euclid(T a, T b) { if (b == 0) return Triple<T>(1, 0, a); Triple<T> r = euclid(b, a % b); return Triple<T>(r.y, r.x - a / b * r.y, r.z); }
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 = 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;
}

const double PI = 2 * acos(0);
const string months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
//const int dr[] = {0, 0, -1, +1};
//const int dc[] = {-1, +1, 0, 0};
const int dr[] = {-2, -1, +1, +2, +2, +1, -1, -2};
const int dc[] = {+1, +2, +2, +1, -1, -2, -2, -1};

const int inf = (int)1e9 + 5;
const ll linf = (ll)1e16 + 5;
const double eps = ld(1e-12);
const ll mod = 1000000000;
typedef pair<int, int> II;

#define maxn 100005

using namespace std;

int n, k;
string s;

int main(){
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
    int maxr = 0, minr = 0, maxc = 0, minc = 0;
    scanf("%d %d", &n, &k);
    cin >> s;
    int X = 0, Y = 0, t;
    Rep(i, s.length()){
        t = s[i] - '1';
        X += dr[t]; Y += dc[t];
        maxr = max(maxr, X); minr = min(minr, X);
        maxc = max(maxc, Y); minc = min(minc, Y);
    }

    int r = maxr - minr, c = maxc - minc;
    if(r > n || c > n) {
        printf("0"); return 0;
    }
    printf("%d\n", (n - r) * (n - c));

}

Comments

Please read the guidelines before commenting.


There are no comments at the moment.