⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 poj2111.cpp

📁 本人最近在acm.pku.edu.cn上通过的程序
💻 CPP
字号:
#include <algorithm>
using namespace std;

const int dx[8] = {-2, -2, -1, 1, 2, 2, 1, -1};
const int dy[8] = {-1, 1, 2, 2, 1, -1, -2, -2};
int n;
int a[400][400];
int f[400][400];
int b[400][400];
pair<int, int> p[400][400];

void init()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            scanf("%d", &a[i][j]);
    memset(f, -1, sizeof(f));
}

int calc(int x, int y)
{
    if (f[x][y] >= 0) return f[x][y];
    for (int d = 0; d < 8; d++)
    {
        int tx = x + dx[d], ty = y + dy[d];
        if (!(tx > 0 && tx <= n && ty > 0 && ty <= n)) continue;
        if (a[tx][ty] < a[x][y]) continue;
        int t = calc(tx, ty);
        if (t + 1 > f[x][y] || t + 1 == f[x][y] && a[tx][ty] < b[x][y]) 
        {
            f[x][y] = t + 1;
            b[x][y] = a[tx][ty];
            p[x][y]. first = tx, p[x][y]. second = ty;
        }
    }
    if (f[x][y] < 0) f[x][y] = 0;
    return f[x][y];
}

void printans(int x, int y)
{
    printf("%d\n", a[x][y]);
    if (f[x][y]) printans(p[x][y]. first, p[x][y]. second);
}

void solve()
{
    int ans = -1, x = 0, ax = 0, ay = 0;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
        {
            int t = calc(i, j);
            if (t > ans || t == ans && a[i][j] < x) 
            {
                ans = t, x = a[i][j];
                ax = i, ay = j;
            }
        }
    printf("%d\n", ans + 1);
    printans(ax, ay);
}

int main()
{
    init();
    solve();
    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -