请用c++实现以下题目有一片矩形的阵地,有a行b列。现在在第x行、第y列埋上了炸弹,如果从埋上炸弹的地方开始走(可以向上、向下、向左、向右4个方向走)n步之内(包括0步)都会被炸到,请问有多少点会被炸到。 输入格式: 共1行,为a,b,x,y,n。 输出格式: 共1行,问有多少点会被炸到。 限制: 10%的数据满足: 1<=x<=a<=100 1<=y<=b<=100 1<=n<=10 30%的数据满足: 1<=x<=a<=1000 1<=y<=b<=1000 1<=n<=100 100%的数据满足:1<=x<=a<=2.1*10^9 1<=y<=b<=2.1*10^9 1<=n<=10000 样例 1 : 输入: 6 7 3 2 4 输出: 27
时间: 2023-08-10 12:04:38 浏览: 59
以下是C++实现的代码,使用了BFS算法:
```c++
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int N = 2005;
int a, b, x, y, n;
bool g[N][N];
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int bfs()
{
queue<pair<int, int>> q;
q.push({x, y});
g[x][y] = true;
int res = 1;
while (n--)
{
int len = q.size();
while (len--)
{
auto t = q.front();
q.pop();
for (int i = 0; i < 4; i++)
{
int nx = t.first + dx[i], ny = t.second + dy[i];
if (nx >= 1 && nx <= a && ny >= 1 && ny <= b && !g[nx][ny])
{
g[nx][ny] = true;
q.push({nx, ny});
res++;
}
}
}
}
return res;
}
int main()
{
cin >> a >> b >> x >> y >> n;
cout << bfs() << endl;
return 0;
}
```