第二个点emmm

P1434 [SHOI2002] 滑雪

chanper @ 2019-12-03 09:21:32

改用scanf+开启O2优化就过了。。。但是还是不知道正常的写法哪里超了。。。

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

int r, c;
int mat[105][105];
int maxx[105][105];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
struct Node{
    int x, y, step;
    Node(int x, int y, int step):x(x), y(y), step(step){}
};

void bfs(int i, int j);
bool isLegal(int x, int y);

int ans = 0;

int main(void)
{
    scanf("%d%d", &r, &c);
    for(int i = 1; i <= r; i++)
        for(int j = 1; j <= c; j++)
            scanf("%d", &mat[i][j]);

    memset(maxx, -1, sizeof(maxx));
    for(int i = 1; i <= r; i++){
        for(int j = 1; j <= c; j++){
            bfs(i, j);
        }
    }

    for(int i = 1; i <= r; i++)
        for(int j = 1; j <= c; j++)
            if(maxx[i][j] > ans)
                ans = maxx[i][j];
    printf("%d\n", ans);
    return 0;
}

void bfs(int i, int j)
{
    if(maxx[i][j] >= 1)
        return;

    queue<Node> q;
    q.push(Node(i, j, 1));

    while(!q.empty())
    {
        Node cur = q.front();
        q.pop();

        if(maxx[cur.x][cur.y] >= cur.step)
            continue;
        else
            maxx[cur.x][cur.y] = cur.step;

        for(int i = 0; i < 4; i++)
        {
            int xx = cur.x + dx[i];
            int yy = cur.y + dy[i];

            if(isLegal(xx, yy) && mat[xx][yy] > mat[cur.x][cur.y])
            {
                q.push(Node(xx, yy, cur.step+1));
            }
        }
    }
}

bool isLegal(int x, int y)
{
    if(x < 1 || y < 1 || x > r || y > c)
        return false;
    return true;
}

by fresh_boy @ 2019-12-04 17:39:39

我也想知道__


|