拿错了?求!!!
2024-08-30 20:21:55
发布于:福建
62阅读
0回复
0点赞
#include<bits/stdc++.h>
using namespace std;
int a[105][105],b[105][105]={ };
int m,n,x=1,y=1;
int s=0;
void chuan1()
{
if(a[x+1][y]>=a[x][y+1] && b[x][y]!=1)
{
x++;
s+=a[x][y];
b[x][y]=1;
}else
{
y++;
s+=a[x][y];
b[x][y]=1;
}
}
void chuan2()
{
if(a[x-1][y]>=a[x][y-1] && b[x][y]!=1)
{
x--;
s+=a[x][y];
b[x][y]=1;
}else
{
y--;
s+=a[x][y];
b[x][y]=1;
}
}
int main()
{
cin >> m >> n;
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
cin >> a[i][j];
}
}
while(x!=m && y!=n)
{
chuan1();
}
while(x!=1 && y!=1)
{
chuan2();
}
cout << s;
return 0;
}
//我看你们为啥搞得那么复杂,连三维数组都搞出来了
全部评论 1
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;int main() {
int m, n;
cin >> m >> n;
vector<vector<int>> grid(m + 1, vector<int>(n + 1)); // 1-based索引
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
cin >> grid[i][j];
}
}int max_k = (m + n - 2); // 最大步数:从(1,1)到(m,n)需要m+n-2步 // dp[k][x1][x2]表示两条路径走了k步后,分别到达(x1,y1)和(x2,y2),其中y1=k+2-x1, y2=k+2-x2 vector<vector<vector<int>>> dp(max_k + 1, vector<vector<int>>(m + 1, vector<int>(m + 1, -1))); dp[0][1][1] = 0; // 初始状态:0步,都在(1,1),好感度和为0 for (int k = 0; k < max_k; ++k) { // 当前步数k,下一步到k+1步 for (int x1 = 1; x1 <= m; ++x1) { int y1 = (k + 2) - x1; if (y1 < 1 || y1 > n) continue; // (x1,y1)越界,跳过 for (int x2 = 1; x2 <= m; ++x2) { int y2 = (k + 2) - x2; if (y2 < 1 || y2 > n) continue; // (x2,y2)越界,跳过 if (dp[k][x1][x2] == -1) continue; // 该状态不可达,跳过 // 四个方向组合:(上,上), (上,左), (左,上), (左,左) vector<pair<int, int>> dirs = {{-1, -1}, {-1, 0}, {0, -1}, {0, 0}}; for (auto& dir : dirs) { int dx1 = dir.first, dx2 = dir.second; int nx1 = x1 + dx1; // 下一个x1坐标 int nx2 = x2 + dx2; // 下一个x2坐标 int ny1 = (k + 3) - nx1; // 下一个y1坐标(k+1步时,x+y = (k+1)+2 = k+3) int ny2 = (k + 3) - nx2; // 下一个y2坐标 // 检查新坐标是否越界 if (nx1 < 1 || nx1 > m || ny1 < 1 || ny1 > n) continue; if (nx2 < 1 || nx2 > m || ny2 < 1 || ny2 > n) continue; // 计算新增好感度:若两点相同,只加一次;否则加两点之和 int add = 0; if (nx1 == nx2 && ny1 == ny2) { add = grid[nx1][ny1]; } else { add = grid[nx1][ny1] + grid[nx2][ny2]; } //18小时前 来自 浙江
0AI
18小时前 来自 浙江
0



有帮助,赞一个