שאלה 1
השאלה הסטנדרטית של מחלקה A ומחלקה B שיורשת ממנה לשניהם פונקציית
foo וירטואלית , ו-main מה יודפס ולתת הסבר על הvtable
שאלה 2
נתונה מטריצה , מצא מסלול בין 0'0 ל-m,n כאשר אסור לדרוך על 1 רק על אפסים
---------------------------------------
|0|0|1|1
|1|0|0|0
1|0|1|1
0|0|0|0
0|0|1|0
------------------------------------
תשובה : ברקורסיה
LookForPath(Matrix Mat,int I,int j,int m,int n)
{
If (I <0)||(j<0)|| i>m)||(j>n)
Return false;
If (i==m)&&(j == n)
Return true;
Else
{
Return (LookForPath(Mat, I+1, j, m, n) || LookForPath(Mat, I-1, j, m, n) ||
LookForPath(Mat, I, j-1, m, n) || LookForPath(Mat, I, +1j, m, n) )
}
}
שאלה 3
עצים , למצוא את הרמה של העלה הכי נמוך בעץ
תשובה : ברקורסיה
Recursively
DFS or BFS
ShortestPath(Tree t,int Level)
{
If (isLeave(Tree)
Return level;
Else
{
Level1 = ShortestPath(t, Level+1)
Level2 = ShortestPath(t,Level+1)
}
If (level1 <level2)
Return level1;
Else
Return level2;
}
isLeave(Tree t)
{
If (t.left == null)&& (tree.right ==null)
Return true
Else
return false;
}
Main function
ShortestPath(Tree t)
{
Return ShortestPath(0);
}
ע"י: 1_אורח_כללי
תודה רבה :)
רק תיקון קטן כשאתה קורא רקורסיבי, צריך לקרוא עם t.left ו t.right