蓝桥杯2020年第十一届C/C++国赛B组第二题-扩散

Problem Description

在这里插入图片描述

AC Code

#include <iostream>
#include <queue>
#include <cmath>
using namespace std;

typedef long long LL;

struct node{
    int x;
    int y;
    int step;
};

int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};

int ans;
int a[8004][8004];

queue<node> map;

int main()
{
    a[0+3000][0+3000]=1;
    a[2020+3000][11+3000]=1;
    a[11+3000][14+3000]=1;
    a[2000+3000][2000+3000]=1;
    map.push(node{0+3000,0+3000,0});
    map.push(node{2020+3000,11+3000,0});
    map.push(node{11+3000,14+3000,0});
    map.push(node{2000+3000,2000+3000,0});
    while (!map.empty()){
        node cur=map.front();
        map.pop();
        if(cur.step==2020) continue;
        for (int i = 0; i <4 ; ++i) {
            int nx=cur.x+dx[i];
            int ny=cur.y+dy[i];
            if(!a[nx][ny]){
                a[nx][ny]=1;
                ans++;
                map.push(node{nx,ny,cur.step+1});
            }
        }
    }
    cout<<ans+4;
    return 0;
}

Correct Answer

20312088

版权声明:本文为weixin_45774972原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。