java - Android how to stop recursive function when I got one result -
i new android. writing simple app user solve sudoku. user asked input numbers program provide 1 solution puzzle.
my recursive function find results works dont know how stop function when got first solution. therefore, app keep running , crash.
here code
public class solver extends actionbaractivity implements onclicklistener{ private final string tag = "solver"; public final int[][] = new int[9][9]; private solverview solverview; public static final int[][] temp = { {1,1,1,2,2,2,3,3,3}, {1,1,1,2,2,2,3,3,3}, {1,1,1,2,2,2,3,3,3}, {4,4,4,5,5,5,6,6,6}, {4,4,4,5,5,5,6,6,6}, {4,4,4,5,5,5,6,6,6}, {7,7,7,8,8,8,9,9,9}, {7,7,7,8,8,8,9,9,9}, {7,7,7,8,8,8,9,9,9} }; @override protected void oncreate(bundle savedinstancestate){ super.oncreate(savedinstancestate); solverview = new solverview(this); setcontentview(r.layout.activity_solver); solverview.requestfocus(); solverview.bringtofront(); setcontentview(solverview); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.solver, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { int id = item.getitemid(); if (id == r.id.action_settings) { log.d(tag, "menuselect: something"); run(a, 0); return true; } return super.onoptionsitemselected(item); } @override public void onclick(view v) { // todo auto-generated method stub if (v.getid() == r.id.solve) { log.d(tag, "solve button pressed"); } } protected void showkeypad(int x, int y) { dialog dialog = new keyboard(this, this.solverview); dialog.show(); } public boolean setcellifvalid(int selu, int selv, int t) { log.d(tag, " in setcellifvalid " + selu + " " + selv + " " + t); this.a[selu][selv] = t; solverview.invalidate(); return true; } public string getcellstring(int i, int j) { int v = this.a[i][j]; if (v == 0) return ""; else return string.valueof(v); } public static void run(int[][] a, int n) { if (n >= 81) { printarray(a); // want program stop here return; } int = n / 9; int j = n % 9; if (a[i][j] != 0) run(a, n + 1); (int k = 1; k <= 9; k++) if (valid(a, i, j, k)) { a[i][j] = k; run(a, n + 1); a[i][j] = 0; } } public static boolean valid(int[][] a, int u, int v, int k) { //check column (int = 0; < 9; i++) if (a[i][v] == k) return false; //check row (int j = 0; j < 9; j++) if (a[u][j] == k) return false; //check small square (int = 0; < 9; i++) (int j = 0; j < 9; j++) if (temp[i][j] == temp[u][v] && a[i][j] == k) return false; return true; } public static void printarray(int[][] a) { (int = 0; < a.length; i++) { (int j = 0; j < a[0].length; j++) system.out.print(a[i][j] + " "); system.out.println(); } }
}
anyone me? thank reading. sincerely
you have 2 possible approaches choose from. best discard recursive approach , try iterative one. way can stop searching answers when find answer. doesn't need iterative function per se: tail-recursive well.
the easier solution store global variable, boolean solutionfound
, , initialize false
. then, when 1 of recursions find solution, change flag true
. recursive function should check flag, , return if it's true
.
this pseudocode of sorts find myself using when ever made brute-force sudoku solver:
void solvesquare(currentlayout, row, column) { if row > maxrows print(layout); return; if column > maxcolumns solvesquare(currentlayout, row + 1, 0) return; foreach digit d in [1, 2, 3, 4, 5, 6, 7, 8, 9] : if isvaliddigitatsquare(currentlayout, row, column) temp = currentlayout[row][column]; currentlayout[row][column] = d; solvesquare(currentlayout, row, column + 1); currentlayout[row][column] = temp; }
the above pseudocode assumes there exists 1 solution sudoku, since decent (some prefer valid) sudoku there exists 1 solution. particular pseudocode, have ever return 1 solution add flag follows:
boolean solutionfound = false; void solvesquare(currentlayout, row, column) { if solutionfound return; if row > maxrows print(layout); solutionfound = true; return; if column > maxcolumns solvesquare(currentlayout, row + 1, 0) return; foreach digit d in [1, 2, 3, 4, 5, 6, 7, 8, 9] : if isvaliddigitatsquare(currentlayout, row, column) temp = currentlayout[row][column]; currentlayout[row][column] = d; solvesquare(currentlayout, row, column + 1); currentlayout[row][column] = temp; }
Comments
Post a Comment