如何将上面两个问题用C++实现
时间: 2024-03-24 10:40:01 浏览: 23
好的,下面是两个问题的C++实现代码:
问题一:正整数N的数字螺旋方阵主对角线上所有数字的乘积
```c++
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int a[n][n] = {0};
int num = 1, i = 0, j = 0, k = 0;
while (num <= n * n) {
while (j + k < n && a[i][j + k] == 0) a[i][j + k++] = num++;
k--; i++;
while (i + k < n && a[i + k][j] == 0) a[i + k++][j] = num++;
k--; j++;
while (j + k >= 0 && a[i][j + k] == 0) a[i][j + k--] = num++;
k++; i--;
while (i + k >= 0 && a[i + k][j] == 0) a[i + k--][j] = num++;
k++; j--;
}
int ans = 1;
for (int i = 0; i < n; i++) {
ans *= a[i][i];
}
cout << ans << endl;
return 0;
}
```
问题二:从入口到出口,小明和小亮的行动
```c++
#include <iostream>
#include <queue>
using namespace std;
const int MAXN = 50;
const int INF = 0x3f3f3f3f;
struct State {
int x1, y1, x2, y2, stone;
};
int n, m, x, y;
int maze[MAXN][MAXN];
bool vis[MAXN][MAXN][MAXN][MAXN];
int dx[] = {1, 0};
int dy[] = {0, 1};
int bfs() {
queue<State> q;
q.push({0, 0, 0, 0, 0});
vis[0][0][0][0] = true;
int ans = INF;
while (!q.empty()) {
State cur = q.front(); q.pop();
if (cur.x1 == n - 1 && cur.y1 == m - 1 && cur.x2 == n - 1 && cur.y2 == m - 1) {
ans = min(ans, cur.stone);
}
for (int d = 0; d < 2; d++) {
int nx1 = cur.x1 + dx[d], ny1 = cur.y1 + dy[d];
int nx2 = cur.x2 + dx[d], ny2 = cur.y2 + dy[d];
if (nx1 >= 0 && nx1 < n && ny1 >= 0 && ny1 < m && nx2 >= 0 && nx2 < n && ny2 >= 0 && ny2 < m) {
if (maze[nx1][ny1] == 0 && maze[nx2][ny2] == 0 && !vis[nx1][ny1][nx2][ny2]) {
vis[nx1][ny1][nx2][ny2] = true;
q.push({nx1, ny1, nx2, ny2, cur.stone});
} else if (maze[nx1][ny1] == 1 && maze[nx2][ny2] == 0 && cur.stone < x && !vis[nx1][ny1][nx2][ny2]) {
vis[nx1][ny1][nx2][ny2] = true;
q.push({nx1, ny1, nx2, ny2, cur.stone + 1});
} else if (maze[nx1][ny1] == 0 && maze[nx2][ny2] == 1 && cur.stone < y && !vis[nx1][ny1][nx2][ny2]) {
vis[nx1][ny1][nx2][ny2] = true;
q.push({nx1, ny1, nx2, ny2, cur.stone + 1});
}
}
}
}
return ans;
}
int main() {
cin >> n >> m >> x >> y;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> maze[i][j];
}
}
int ans = bfs();
cout << ans << endl;
return 0;
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)