這題我用遞迴寫
檢查好幾遍都找不到錯誤
求大神解答
#include<iostream>
using namespace std;
int n = 0, m = 0, arr[100 + 2][100 + 2];
void robot(int min, int x, int y, int ans)
{
if (arr[x + 1][y] < min && arr[x + 1][y] != -1 && x + 1 != n)
{
min = arr[x + 1][y];
arr[x + 1][y] = -1;
robot(min, x + 1, y, ans + min);
}
if (arr[x - 1][y] < min && arr[x - 1][y] != -1 && x - 1 > 0)
{
min = arr[x - 1][y];
arr[x - 1][y] = -1;
robot(min, x - 1, y, ans + min);
}
if (arr[x][y + 1] < min && arr[x][y + 1] != -1 && y + 1 != m)
{
min = arr[x][y + 1];
arr[x][y + 1] = -1;
robot(min, x, y + 1, ans + min);
}
if (arr[x][y - 1] < min && arr[x][y - 1] != -1 && y - 1>0)
{
min = arr[x][y - 1];
arr[x][y - 1] = -1;
robot(min, x, y - 1, ans + min);
}
cout << ans;
}
int main()
{
int min = 1e9, x = 0, y = 0, ans = 0;
cin >> n >> m;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> arr[i][j];
if (arr[i][j] < min)
{
min = arr[i][j];
x = i; y = j;
}
}
}
ans += min;
arr[x][y] = -1;
robot(min, x, y, ans);
return 0;
}
這題我用遞迴寫
檢查好幾遍都找不到錯誤
求大神解答
#include
using namespace std;
int n = 0, m = 0, arr[100 + 2][100 + 2];
void robot(int min, int x, int y, int ans)
{
if (arr[x + 1][y] < min && arr[x + 1][y] != -1 && x + 1 != n)
{
min = arr[x + 1][y];
arr[x + 1][y] = -1;
robot(min, x + 1, y, ans + min);
}
if (arr[x - 1][y] < min && arr[x - 1][y] != -1 && x - 1 > 0)
{
min = arr[x - 1][y];
arr[x - 1][y] = -1;
robot(min, x - 1, y, ans + min);
}
if (arr[x][y + 1] < min && arr[x][y + 1] != -1 && y + 1 != m)
{
min = arr[x][y + 1];
arr[x][y + 1] = -1;
robot(min, x, y + 1, ans + min);
}
if (arr[x][y - 1] < min && arr[x][y - 1] != -1 && y - 1>0)
{
min = arr[x][y - 1];
arr[x][y - 1] = -1;
robot(min, x, y - 1, ans + min);
}
cout << ans;
}
int main()
{
int min = 1e9, x = 0, y = 0, ans = 0;
cin >> n >> m;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> arr[i][j];
if (arr[i][j] < min)
{
min = arr[i][j];
x = i; y = j;
}
}
}
ans += min;
arr[x][y] = -1;
robot(min, x, y, ans);
return 0;
}
改了一下 加了bool判斷
#include<iostream>
using namespace std;
int n = 0, m = 0, arr[100 + 2][100 + 2], x = 0, y = 0;
bool Noway=0;
void robot(int min, int x, int y, int ans, bool Noway )
{
if(Noway)
{
cout << ans;
return;
}
Noway = 1;
if (arr[x + 1][y] < min && arr[x + 1][y] != -1 && x + 1 != n && Noway == 0)
{
min = arr[x + 1][y];
arr[x + 1][y] = -1;
x++;
Noway = 0;
}
if (arr[x - 1][y] < min && arr[x - 1][y] != -1 && x - 1 > 0 && Noway == 0)
{
min = arr[x - 1][y];
arr[x - 1][y] = -1;
x--;
Noway = 0;
}
if (arr[x][y + 1] < min && arr[x][y + 1] != -1 && y + 1 != m && Noway == 0)
{
min = arr[x][y + 1];
arr[x][y + 1] = -1;
y++;
Noway= 0;
}
if (arr[x][y - 1] < min && arr[x][y - 1] != -1 && y - 1>0 && Noway == 0)
{
min = arr[x][y - 1];
arr[x][y - 1] = -1;
y--;
Noway = 0;
}
ans += min;
robot(min, x, y, ans, Noway);
}
int main()
{
int min = 1e9, ans = 0;
cin >> n >> m;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> arr[i][j];
if (arr[i][j] < min)
{
min = arr[i][j];
x = i; y = j;
}
}
}
ans += min;
arr[x][y] = -1;
robot(min, x, y, ans, 0);
return 0;
}