第十一届蓝桥杯B组国赛C/C++

题意

注意

  1. 首先我们要明确的一点就是:数组里面是无法存负数下标的,可以存负数。所以题目中给的(0,0)点,如果我们从该点bfs的话,会造成数组越界的情况

  2. 针对第一点的问题,有两个解决办法。第一办法是:给出的四个坐标每一个都去加上2020;第二个办法就是用map去进行存储

思路

除去上面需要注意的地方,剩下的就是一个bfs模板,直接写就行了

这是另外一种不用bfs的思路,比较巧妙:https://blog.csdn.net/weixin_46794749/article/details/111392848

答案是:20312088

AC代码

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;

//(0,0) +2020 (2020,2020)
//(2020,11) +2020 (4040,2031)
//(11,14) +2020 (2031,2034)
//(2000,2000) +2020 (4020,4020)
// max=4020,4020+2020=6040

const int N=6100;
bool book[N][N];
int ans=4,to[4][2]= {{-1,0},{1,0},{0,1},{0,-1}};
struct node
{
    int x,y,step;
} p,q;

queue<node>Q;

//void bfs(int x,int y)
void bfs()
{
    //book[x][y]=1;
    while(!Q.empty())
    {
        p=Q.front();
        Q.pop();
        for(int i=0; i<4; i++)
        {
            q.x=p.x+to[i][0];
            q.y=p.y+to[i][1];
            q.step=p.step+1;
            if(!book[q.x][q.y]&&q.step<=2020)
            {
                book[q.x][q.y]=1;
                ans++;
                Q.push(q);
            }
        }
    }
}

void init(int x,int y)
{
    book[x][y]=1;
    p.x=x,p.y=y,p.step=0;
    Q.push(p);
}

int main()
{
    init(2020,2020);
    init(4040,2031);
    init(2031,2034);
    init(4020,4020);
    bfs();
    cout<<ans<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/OFSHK/p/14663046.html