public int[,] adj = new int[6, 6] { { 0, 1, 0, 1, 0, 0 }, { 1, 0, 1, 1, 0, 0 }, { 0, 1, 0, 0, 0, 0 }, { 1, 1, 0, 0, 1, 0 }, { 0, 0, 0, 1, 0, 1 }, { 0, 0, 0, 0, 1, 0 } };
강의에서 처럼 위와 같이 그래프를 생성 하였고
public void BFS(int start)
{
bool[] found = new bool[6];
Queue<int> q = new Queue<int>();
found[start] = true;
q.Enqueue(start);
Console.WriteLine(start);
while (q.Count > 0)
{
int now = q.Dequeue();
for (int next = 0; next < adj.GetLength(now); next++)
{
if (adj[now, next] == 0)
continue;
if (found[next])
continue;
q.Enqueue(next);
found[next] = true;
Console.WriteLine(next);
}
}
}
아래 BFS함수도 강의를 보고 작성 해보았는데 빨간색 글씨로 써놓은 adj.GetLength(now)부분에서 IndexOutOfRangeException 예외가 발생합니다.
선생님은 강의중 6으로 코딩을 하셨지만 조금 더 동적인 코딩을 해보고 싶어서 GetLength함수를 사용하였습니다.
그런데 디버깅을 해보면 adj.GetLength(0) 은 6으로 잘 나오지만 adj.GetLength(3)이 되면 예외가 발생하는데, 왜 예외가 발생하는지 궁금합니다.
adj 배열의 4번째 차원은 원소가 6개이므로 6이 반환되어야 하는것 아닌가요?