所以我今天大部分时间都在研究这个问题。有人可以帮我弄清楚我哪里出错了吗?
它是贪婪的,因为它会移动到硬币数量最多的位置;它很懒,因为如果没有相邻的位置增加它的硬币宝藏,它就会停止移动。如果几个相邻的位置有相同的最高硬币数量,收集者将选择以顺时针方式移动到最高的位置。收集者从它访问的任何位置清空硬币。
public static int receiveCoins(int[][]map,int r,int c){
int[] coins = {0,0,0,0};
boolean moreCoins = true;
int numOfCoins = 0;
if(map[r][c] > 0){
numOfCoins += map[r][c];
map[r][c] = 0;
}
while(moreCoins == true){
if(c < (map.length-1)){
if(map[r][c+1] > 0){
coins[1] = map[r][c+1];
}
}
if(r < map[0].length-1){
if(map[r+1][c] > 0){
coins[2] = map[r+1][c];
}
}
if(row > 0){
if(map[r-1][c] > 0){
coins[0] = map[r-1][c];
}
}
if(c > 0){
if(map[r][c-1] > 0){
coins[3] = map[r][c-1];
}
}
int maxCoin = 0;
int nRow = 0;
int nCol = 0;
for(int i = 0; i < coins.length; i++){
if(coins[i] > maxCoin){
maxCoin = coins[i];
if(i == 0){
nCol = c;
nRow = r - 1;
}else if(i == 1){
nRow = r;
nCol = c + 1;
}else if(i == 2){
nCol = c;
nRow = r + 1;
}else{
nRow = r;
nCol = c - 1;
}
}
coins[i] = 0;
}
}
}
if (maxCoin == 0){
moreCoins = false;
}else{
r = nRow;
c = nCol;
numOfCoins += map[r][c];
map[r][c] = 0;
}
}
return numOfCoins;
}