לוגו אתר Fresh          
 
 
  אפשרות תפריט  ראשי     אפשרות תפריט  צ'אט     אפשרות תפריט  מבזקים     אפשרות תפריט  צור קשר     חץ שמאלה ‎print ‎"Hello World!"; if‎ ‎not rules.‎know ‎then rules.‎read(); חץ ימינה  

לך אחורה   לובי הפורומים > מחשבים > תכנות ובניית אתרים
שמור לעצמך קישור לדף זה באתרי שמירת קישורים חברתיים
תגובה
 
כלי אשכול חפש באשכול זה



  #1  
ישן 27-11-2010, 18:00
  i1991 i1991 אינו מחובר  
 
חבר מתאריך: 27.11.10
הודעות: 2
java רקורסיה-שאלה

ש לי עבודה לעשות.
הנה הקישור:
http://www.cs.bgu.ac.il/~intro111/wi...ssignment3.pdf

אני מדבר על חלק 3-משחק ריצוף
עשיתי את כל חלק 3 (משחק ריצוף) עד לקטע עם הרקורסיה.
שם עשיתי רקורסיה שעובדת אך רק על לוח של 2 על 2.
הסיבה שזה לא עובד לשאר הגדלים זה החוסבר יעילות הרציני בה עשיתי את התוכנית.
אני בטוח שזה לא אמור להיות כמו שעשיתי, אבל אין לי רעיון אחר איך לייעל את התוכנית.

אשמח לקבל עצות.


קוד:
הנה 3 המשימות הראשונות בחלק שלוש(שבדקתי שהם עובדות ואני מקווה שאין בהן טעות): public class Ex3 { public static int north(int[] tile){ return tile[0]; } public static int east(int[] tile){ return tile[1]; } public static int south(int[] tile){ return tile[2]; } public static int west(int[] tile){ return tile[3]; } /******************** Task 1 ********************/ public static boolean canPut(int[]tile, int x, int y, int[][][] board) { boolean ans = true; //המיקום אכן נמצא על הלוח והינו פנוי עוד לא מיקמנו שם אריח אחר if (x<0 || x> board.length-1) ans=false; // check if the column x is on the board if (y<0 || y> board.length-1) ans=false;// check if the row y is on the board if (ans=true && ((board[x][y])!= null)) ans=false; // board[x][y] is empty if(ans=true) { // אם המיקום נמצא על מסגרת הלוח, אזי צלעות הסף הינן בצבע אפור if (x==0) { if (west(tile)!=0) ans=false; } if (x==board.length-1) { if (east(tile)!=0) ans=false; } if (y==0) { if (north(tile)!=0) ans=false; } if (y==board.length-1) { if (south(tile)!=0) ans=false; } } if(ans==true) { // בדיקת התאמה לאיבר המזרחי בתנאי שהשכן מימין קיים if((x<board.length-1) && ((board[x+1][y])!= null)) { if(east(tile)!= board [x+1][y][3]) ans=false; } // בדיקת התאמה לאיבר הדרומי בתנאי שהשכן מלמטה קיים if((y<board.length-1) && ((board[x][y+1])!= null)) { if(south(tile)!= board [x][y+1][0]) ans=false; } ///שיניתי [y-1] [x][y-1][2] // בדיקת התאמה לאיבר המערבי בתנאי שהשכן משמאל קיים if((x>0) && ((board[x-1][y])!= null)) { if(west(tile)!= board [x-1][y][1]) ans=false; } // בדיקת התאמה לאיבר הצפוני בתנאי שהשכן מלמעלה קיים if((y>0) && ((board[x][y-1])!= null)) { if(north(tile)!= board [x][y-1][2]) ans=false; } } return ans; } /******************** Task 2 ********************/ public static int[][][] put(int[] tile, int x, int y, int[][][] board) { int n = board.length; int[][][] newBoard= new int[n][n][]; for(int row=0; row<n; row++) { for(int col=0; col<n; col++) { if(board[row][col]!=null) { { newBoard[row][col]=board[row][col]; } } else newBoard[row][col]=null; } } newBoard[x][y]=tile; return newBoard; } /******************** Task 3 ********************/ public static int[][] delete(int i, int[][] tiles) { int[][] restTiles = new int[tiles.length-1][tiles[0].length]; int pointerRowTiles=0; int pointerColTiles=0; int pointerRowRestTiles=0; int pointerColRestTiles=0; while(pointerRowTiles<tiles.length && pointerRowRestTiles<restTiles.length ) { if( pointerRowTiles==i) { pointerRowTiles++; } pointerColTiles=0; pointerColRestTiles=0; while(pointerColTiles<4 && pointerColRestTiles<4 ) { restTiles[pointerRowRestTiles][pointerColRestTiles]=tiles[pointerRowTiles][ pointerColTiles]; pointerColRestTiles++; pointerColTiles++; } pointerRowTiles++; pointerRowRestTiles++; } return restTiles; } public static int[] rotate(int j, int[] tile){ int[] nextTile = new int[tile.length]; int temp; int counter=1; for(int i=0; i<tile.length;i++) nextTile[i]=tile[i]; while(counter<=j) { temp= nextTile[3]; for(int r=nextTile.length-1;r>= 1; r--) nextTile[r]= nextTile[r-1]; nextTile[0]=temp; counter++; } return nextTile; } זוהי המשימה האחרונה בחלק 3 בה צריך לעשות את הרקורסיה. כרע אני מנסה לפתור את הרקורסיה עם אריחים שאין צורך לסובבם ופה אני צריך את העזרה שלכם-איך לעשות זאת בצורה היעילה ביותר? (מספיק כדי שיפתור לי לוחות עד גודל של 5*5 וגם 4*4 יהיה מספיק טוב בשבילי). אז הנה מה שכתבתי--אני יודע שזה לא פתרון יעיל בכלל בכלל אבל בכל זאת... /******************** Task 4 ********************/ public static int[][][] solve(int[][] tiles){ int size = (int) Math.sqrt(tiles.length); int[][][] board = new int[size][size][]; return solve(board,tiles); } public static int[][][] solve(int[][][] board, int[][] tiles){ int[][][] solution = null; if(tiles.length>0 ) { if(tiles.length==1) { for (int y=0; y<board.length ; y++) for (int x=0; x<board[y].length ; x++) if(canPut(tiles[0], x, y, board)) { board=put(tiles[0], x, y, board); } } else { for (int y=0; y<board.length; y++) // lines for (int x=0; x<board.length; x++) // column for (int i=0; i<tiles.length ; i++) // tiles { if(board!=null) { if(canPut(tiles[i], y, x, board)) { board=put(tiles[i], y, x, board); solve(board,(delete( i, tiles))); } } } } } return board; }
תגובה ללא ציטוט תגובה עם ציטוט חזרה לפורום
  #3  
ישן 27-11-2010, 19:31
  i1991 i1991 אינו מחובר  
 
חבר מתאריך: 27.11.10
הודעות: 2
בתגובה להודעה מספר 2 שנכתבה על ידי hellfrost שמתחילה ב "289 שורות של קוד בלי הזחות?..."

צודק, לא שמתי לב...
מצורף הקובץ שוב ועם שינוי שעשיתי בפונקציה.

הקריאה הזאת לדוגמא לפונקציה
int[][] solver ={{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0 },{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};

מחזירה

0000
null
null
null
null
null
null
null
null


אני יודע שזה בגלל השורות:

קוד:
y=board.length; // ?? x=board.length; // ??



אבל שמתי אותם כדי לקדם את y ו-x אחרי הסיום כדי שהלולאה תסתיים.

אשמח לכל עזרה שהיא הפתרון.
(פתרון שלא כולל צורך בסיבוב האריחים)

תודהה

קוד:
/************************************************** * * intro111/ipis111: Third assignment * * * * This class is for assignment #3 - Part 3 * * * * Author(s): ### YOUR NAME(S) AND EMAIL(S). ##### * * Date: ##/11/2010 * * * ************************************************** */ /* * Important! Add comments to every method! * * The "return" statement at the end of each method and the initial value * is just so this skeleton file compiles, * change it according to your needs */ public class Ex3 { public static int north(int[] tile){ return tile[0]; } public static int east(int[] tile){ return tile[1]; } public static int south(int[] tile){ return tile[2]; } public static int west(int[] tile){ return tile[3]; } /******************** Task 1 ********************/ public static boolean canPut(int[]tile, int x, int y, int[][][] board) { boolean ans = true; // לבדוק שאין אפור באמצע? //המיקום אכן נמצא על הלוח והינו פנוי עוד לא מיקמנו שם אריח אחר if (x<0 || x> board.length-1) ans=false; // check if the column x is on the board if (y<0 || y> board.length-1) ans=false;// check if the row y is on the board if (ans=true && ((board[x][y])!= null)) ans=false; // board[x][y] is empty if(ans=true) { // אם המיקום נמצא על מסגרת הלוח, אזי צלעות הסף הינן בצבע אפור if (x==0) { if (west(tile)!=0) ans=false; } if (x==board.length-1) { if (east(tile)!=0) ans=false; } if (y==0) { if (north(tile)!=0) ans=false; } if (y==board.length-1) { if (south(tile)!=0) ans=false; } } if(ans==true) { // בדיקת התאמה לאיבר המזרחי בתנאי שהשכן מימין קיים if((x<board.length-1) && ((board[x+1][y])!= null)) { if(east(tile)!= board [x+1][y][3]) ans=false; } // בדיקת התאמה לאיבר הדרומי בתנאי שהשכן מלמטה קיים if((y<board.length-1) && ((board[x][y+1])!= null)) { if(south(tile)!= board [x][y+1][0]) ans=false; } ///שיניתי [y-1] [x][y-1][2] // בדיקת התאמה לאיבר המערבי בתנאי שהשכן משמאל קיים if((x>0) && ((board[x-1][y])!= null)) { if(west(tile)!= board [x-1][y][1]) ans=false; } // בדיקת התאמה לאיבר הצפוני בתנאי שהשכן מלמעלה קיים if((y>0) && ((board[x][y-1])!= null)) { if(north(tile)!= board [x][y-1][2]) ans=false; } } return ans; } /******************** Task 2 ********************/ public static int[][][] put(int[] tile, int x, int y, int[][][] board) { int n = board.length; int[][][] newBoard= new int[n][n][]; for(int row=0; row<n; row++) { for(int col=0; col<n; col++) { if(board[row][col]!=null) { { newBoard[row][col]=board[row][col]; } } else newBoard[row][col]=null; } } newBoard[x][y]=tile; return newBoard; } /******************** Task 3 ********************/ public static int[][] delete(int i, int[][] tiles) { int[][] restTiles = new int[tiles.length-1][tiles[0].length]; int pointerRowTiles=0; int pointerColTiles=0; int pointerRowRestTiles=0; int pointerColRestTiles=0; ///לתקן 2 3 while(pointerRowTiles<tiles.length && pointerRowRestTiles<restTiles.length ) { if( pointerRowTiles==i) { pointerRowTiles++; //Kבדוק יעילות א נמצאה השורה שצריך למחוק להפסיק? } /// Kבדוק במקרה שאי הוא השורה האחרונה שאותה צריך להוציא pointerColTiles=0; pointerColRestTiles=0; while(pointerColTiles<4 && pointerColRestTiles<4 ) { restTiles[pointerRowRestTiles][pointerColRestTiles]=tiles[pointerRowTiles][ pointerColTiles]; pointerColRestTiles++; pointerColTiles++; } pointerRowTiles++; pointerRowRestTiles++; } return restTiles; } public static int[] rotate(int j, int[] tile){ int[] nextTile = new int[tile.length]; int temp; int counter=1; for(int i=0; i<tile.length;i++) nextTile[i]=tile[i]; while(counter<=j) { temp= nextTile[3]; for(int r=nextTile.length-1;r>= 1; r--) nextTile[r]= nextTile[r-1]; nextTile[0]=temp; counter++; } return nextTile; } /******************** Task 4 ********************/ public static int[][][] solve(int[][] tiles){ int size = (int) Math.sqrt(tiles.length); int[][][] board = new int[size][size][]; return solve(board,tiles); } public static int[][][] solve(int[][][] board, int[][] tiles){ int[][][] solution = null; int y; int x; if(tiles.length>0 ) { for (int i=0; i<tiles.length; i++) { x=0; while (x<board.length) // column { y=0; while (y<board.length) // lines { if(canPut(tiles[i], x, y, board)) { board= put(tiles[i], x, y, board); solve(board, delete( i, tiles)); y=board.length; // ?? x=board.length; // ?? } else y++; } x++; } } } return board; } public static void main(String[] args) { Tests.testCanPut(2); int[][] solver ={{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0 },{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}}; int[][][] boardnu = solve(solver); if(boardnu==null) System.out.println("it is null"); else { for(int i=0; i< (int) Math.sqrt(solver.length);i++) { for(int j=0;j< (int) Math.sqrt(solver.length);j++) { if (boardnu[i][j]!=null) { for(int k=0;k< 4;k++) System.out.print( boardnu[j][i][k]); } else System.out.print(boardnu[i][j]+" "); System.out.println(" "); } } } } } /* int x=8; if(x!=8) { int [] tile={1,2,3,4}; int[]try2=new int[4]; try2= rotate(1,tile); for(int j=0;j< 4;j++) System.out.print( tile[j]); System.out.println( " the new "); for(int j=0;j< 4;j++) System.out.print( try2[j]); //int[][][] board = new int[5][5][]; //EternityPrint.showBoard(board); int[][] tiles = {{1,2,3,4},{0,2,4,5},{5,2,5,1}}; int[][]ans=new int[tiles.length-1][tiles[0].length]; ans= delete(1,tiles); for(int i=0; i< tiles.length;i++) { for(int j=0;j< tiles[i].length;j++) System.out.print( tiles[i][j]); System.out.println(" h "); } Tests.testPut(0); int [] tilemm={1,2,3,4}; int[]tryk2=new int[4]; try2= rotate(1,tile); for(int j=0;j< 4;j++) System.out.print( tile[j]); System.out.println( " the new "); for(int j=0;j< 4;j++) System.out.print( try2[j]); } */
תגובה ללא ציטוט תגובה עם ציטוט חזרה לפורום
  #4  
ישן 03-12-2010, 12:25
צלמית המשתמש של Narxx
  משתמש זכר Narxx Narxx אינו מחובר  
 
חבר מתאריך: 21.12.04
הודעות: 30,021
בתגובה להודעה מספר 3 שנכתבה על ידי i1991 שמתחילה ב "צודק, לא שמתי לב... מצורף..."

מה ש hellfrost ניסה להגיד זה שאתה לא יכול לתת פה תוכנית כל כך גדולה ולבקש מאיתנו לייעל לך אותה.
אם יש לך בעיה ספציפית עם ריקורסיה, פשוט שים פה ציטוט של הריקורסיה ונעזור לך להבין אותה / לכתוב אותה וכו'.
כל הקוד לא באמת מעניין... רק החלק שאתה מתקשה בו.
אם אתה לא מבין איך לכתוב משהו בהתאם לשאלה, אתה מוזמן לפרסם את השאלה בנפרד, ואז נוכל אולי לסייע לך לחשוב איך לגשת לפתרון השאלה בעזרת ריקורסיה ואולי גם איך להתחיל לכתוב אותה...
בכל מקרה, היה רציני - שים פה רק את מה שרלוונטי לשאלה שלך, ולא קוד של מאות שורות שאף אחד לא יטרח באמת לקרוא...
_____________________________________
בברכה, מתן.
www.MatanNarkiss.com

תגובה ללא ציטוט תגובה עם ציטוט חזרה לפורום
תגובה

כלי אשכול חפש באשכול זה
חפש באשכול זה:

חיפוש מתקדם
מצבי תצוגה דרג אשכול זה
דרג אשכול זה:

מזער את תיבת המידע אפשרויות משלוח הודעות
אתה לא יכול לפתוח אשכולות חדשים
אתה לא יכול להגיב לאשכולות
אתה לא יכול לצרף קבצים
אתה לא יכול לערוך את ההודעות שלך

קוד vB פעיל
קוד [IMG] פעיל
קוד HTML כבוי
מעבר לפורום



כל הזמנים המוצגים בדף זה הם לפי איזור זמן GMT +2. השעה כעת היא 21:09

הדף נוצר ב 0.05 שניות עם 12 שאילתות

הפורום מבוסס על vBulletin, גירסא 3.0.6
כל הזכויות לתוכנת הפורומים שמורות © 2024 - 2000 לחברת Jelsoft Enterprises.
כל הזכויות שמורות ל Fresh.co.il ©

צור קשר | תקנון האתר