-1

I'm starting my own question because I can't find anything directly related to c# and the solution I am using is different approach. The question is described as follows

In a given grid, each cell can have one of three values:

the value 0 representing an empty cell;
the value 1 representing a fresh orange;
the value 2 representing a rotten orange.

Every minute, any fresh orange that is adjacent (4-directionally) to a rotten orange becomes rotten.

Return the minimum number of minutes that must elapse until no cell has a fresh orange.
If this is impossible, return -1 instead.

Example 1:

Input: [[2,1,1],[1,1,0],[0,1,1]] Output: 4

Example 2:

Input: [[2,1,1],[0,1,1],[1,0,1]] Output: -1 Explanation: The orange in the bottom left corner (row 2, column 0) is never rotten, because rotting only happens 4-directionally.

Example 3:

Input: [[0,2]] Output: 0 Explanation: Since there are already no fresh oranges at minute 0, the answer is just 0.

Note:

1 <= grid.length <= 10
1 <= grid[0].length <= 10
grid[i][j] is only 0, 1, or 2.

I currently have the following as my code:

public class Solution {
    public int OrangesRotting(int[][] grid) {
        var rows = grid.Length;
        var cols = grid[0].Length;
        
        HashSet<string> fresh = new HashSet<string>();
        HashSet<string> rotten = new HashSet<string>();
        
        for(var i = 0; i < rows; i++){
            for(var j = 0; j < cols; j++){
                var current = grid[i][j];
                if(current == 1){
                    fresh.Add(""+i+j);
                } else if (current == 2){
                    rotten.Add(""+i+j);
                }
            }
        }
        
        int minutes = 0;
        int[,] directions = {{0,1},{1,0},{-1,0},{0,-1}};
        while(fresh.Count > 0){
            HashSet<string> infected = new HashSet<string>();
            
            foreach(string s in rotten){
                int i = s[0] - '0';
                int j = s[1] - '0';
                
                foreach(int[] direction in directions){
                    int nextI = i + direction[0];
                    int nextJ = j + direction[1];
                    
                    if(fresh.Contains(""+nextI+nextJ)){
                        fresh.Remove(""+nextI+nextJ);
                        infected.Add(""+nextI+nextJ);
                    }
                }
            }
            if(infected.Count == 0) return -1;
            
            minutes++;
        }
        return minutes;
    }
}

The issue I am having is I get the following compile error:

Line 29: Char 17: error CS0030: Cannot convert type 'int' to 'int[]' (in Solution.cs)

  • Which of the lines is line 29? – Eric J. Aug 10 '20 at 04:25
  • Use a jagged array instead of a multidimensional one. Change your `int[,] directions` to `int[][] directions`. Multidimensional array in C# is not an array of arrays, so your `foreach` is actually fetching `int`s, not `int[]`s – CoolBots Aug 10 '20 at 04:27
  • 2
    Your question is way too verbose for a simple compile error. We don't need to know your system requirements or unrelated parts of the code. You didn't even indicate _which_ was **line 29** arguably the most important thing to know. [mcve] –  Aug 10 '20 at 04:32
  • @MickyD Complaining about a question being too verbose (a rare thing by the way) and at the same time about lack of specific information risks confusing comparably inexperienced users. Please keep their point of view in mind when trying to help. – Yunnosch Aug 10 '20 at 05:16
  • @Yunnosch [mcve]. [ask] –  Aug 10 '20 at 05:19

1 Answers1

0

The array declaration int[,] directions defines a multidimensional array, which, strangely enough, is not an IEnumerable in C#. This means quite a few items that a "normal" array supports aren't available; one of them is a foreach iteration through the top-level array, which would make intuitive sense to write. Instead, what you're iterating through is a single-dimension representation of your multidimensional array - not what you want.

You can do one of two things: either use a jagged array instead - int[][] directions - or switch to a nested for loop instead, accessing each element of the array in the inner loop.

CoolBots
  • 4,770
  • 2
  • 16
  • 30
  • foreach is supported with multidimensional array. Its just that the foreach enumerates through all the elements and its not possible to seperately enumerate through only rows as being used by op. – Jasmeet Aug 10 '20 at 04:52
  • @Jasmeet Yep, that's exactly what I said. :) – CoolBots Aug 10 '20 at 05:50