问题:
答案:20312088
代码:
/*
bfs
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 10005,P = 3000; //偏移量
int map[N][N];
struct Node{
int x,y;
int step;
};
const int dir[4][2] = {
{-1,0},{0,1},{1,0},{0,-1},
};
//相当于起始的时候队列中已经有了4个坐标
void bfs() {
queue<Node> que;
//初始坐标都+3000的偏移量
map[0+P][0+P] = 1;
map[2020+P][11+P] = 1;
map[11+P][14+P] = 1;
map[2000+P][2000+P] = 1;
que.push({0+P,0+P,0});
que.push({2020+P,11+P,0});
que.push({11+P,14+P,0});
que.push({2000+P,2000+P,0});
while(que.size()) {
Node cur = que.front();
que.pop();
if(cur.step == 2020) return;
//往四个方向扩散
for(int i = 0; i < 4; i++) {
int tx = cur.x + dir[i][0];
int ty = cur.y + dir[i][1];
int ts = cur.step + 1;
if(tx<0||tx>=N-5||ty<0||ty>=N-5) continue;
if(map[tx][ty]==1) continue;
map[tx][ty] = 1;
que.push({tx,ty,ts});
}
}
}
int main() {
cout << "开始了" << endl;
bfs();
cout << "输出了" << endl;
int ans = 0;
for(int i = 0; i < N-5; i++)
for(int j = 0; j < N-5; j++)
if(map[i][j]) ans++;
cout << ans << endl;
return 0;
}
运行结果: