Bomb dropping algorithm

后端 未结 30 1219
挽巷
挽巷 2021-01-29 16:55

I have an n x m matrix consisting of non-negative integers. For example:

2 3 4 7 1
1 5 2 6 2
4 3 4 2 1
2 1 2 4 1
3 1 3 4 1
2 1 4 3 2
6 9 1 6 4
         


        
30条回答
  •  独厮守ぢ
    2021-01-29 17:49

    I believe that to minimize the amount of bombs you simply need maximize the amount of damage.. for that to happen need to check the area that has the strongest force.. so you first analyze the field with a 3x3 kernel and check where the sum is stronger.. and bomb there.. and do until the field is flat.. for this filed the answer is 28

    var oMatrix = [
    [2,3,4,7,1],
    [1,5,2,6,2],
    [4,3,4,2,1],
    [2,1,2,4,1],
    [3,1,3,4,1],
    [2,1,4,3,2],
    [6,9,1,6,4]
    ]
    
    var nBombs = 0;
    do
    {
        var bSpacesLeftToBomb = false;
        var nHigh = 0;
        var nCellX = 0;
        var nCellY = 0;
        for(var y = 1 ; ynHigh)
                {
                    nHigh = nValue;
                    nCellX = x;
                    nCellY = y; 
                }
    
            }
        if(nHigh>0)
        {
            nBombs++;
    
            for(var yy = nCellY-1;yy<=nCellY+1;yy++)
            {
                for(var xx = nCellX-1;xx<=nCellX+1;xx++)
                {
                    if(oMatrix[yy][xx]<=0)
                        continue;
                    oMatrix[yy][xx] = --oMatrix[yy][xx];
                }
            }
            bSpacesLeftToBomb = true;
        }
    }
    while(bSpacesLeftToBomb);
    
    alert(nBombs+'bombs');
    

提交回复
热议问题