( ゜▽゜)/こんばんは!
こんなのがありまして、、、
人材獲得作戦・4 試験問題ほか
ちょっとやってみようかなぁ〜と
一時間ほどやってみたんですが
最短ルート出す方法がわかりませんw
ぐぐってアルゴリズムを見ちゃえばできるんでしょうけど
我慢してやってみましたが限界ですw
なるべく最短になるように頑張って進んでるけど
最短にならないパターンもあるっていうなんとも中途半端な感じになっちゃいましたw
もうねむいので今日はこれで就寝・・・
思いつきをごちゃごちゃ張り付けまくったような
きったなくて(しかも長いw)ソースで、ここに載せるかちょっと迷いましたが悩んでる間にも睡魔がおそってきてるし、なにより折角作ったので公開w
まぁ明日またちょっと考えてみようかな〜
これと
これw
こんなのがありまして、、、
人材獲得作戦・4 試験問題ほか
ちょっとやってみようかなぁ〜と
一時間ほどやってみたんですが
最短ルート出す方法がわかりませんw
ぐぐってアルゴリズムを見ちゃえばできるんでしょうけど
我慢してやってみましたが限界ですw
なるべく最短になるように頑張って進んでるけど
最短にならないパターンもあるっていうなんとも中途半端な感じになっちゃいましたw
もうねむいので今日はこれで就寝・・・
思いつきをごちゃごちゃ張り付けまくったような
きったなくて(しかも長いw)ソースで、ここに載せるかちょっと迷いましたが悩んでる間にも睡魔がおそってきてるし、なにより折角作ったので公開w
まぁ明日またちょっと考えてみようかな〜
これと
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; public class SearchRoot { public static void main(String args[]){ char[][] map = null; String path = "D:\\map.txt"; ArrayList<String> list = null; try{ //ファイルから読み込み FileReader fr = new FileReader(path); BufferedReader br = new BufferedReader(fr); list = new ArrayList<String>(); String str; while((str = br.readLine()) != null){ list.add(str); } br.close(); }catch(IOException e){ System.out.println("読み込み失敗"); System.exit(1); } map = new char[list.size()][]; for(int i=0; i<list.size(); i++){ map[i] = list.get(i).toString().toCharArray(); } FindRoot fr = new FindRoot(map); fr.execute(); } }
これw
import java.util.LinkedList; public class FindRoot { int x; int y; int goal_x; int goal_y; char[][] map; LinkedList<String> root = new LinkedList<String>(); final String up = "u"; final String right = "r"; final String down = "d"; final String left = "l"; public FindRoot(char[][] map){ this.map = map; this.x = this.y = -1; for(int i=0; i<this.map.length; i++){ for(int j=0; j<this.map[i].length; j++){ if(this.map[i][j] == 'S'){ this.x = j; this.y = i; }else if(this.map[i][j] == 'G'){ this.goal_x = j; this.goal_y = i; } } } } public void execute(){ //dispMap(); if(getUp() == 'G' || getRight() == 'G' || getDown() == 'G' || getLeft() == 'G'){ mark('$'); dispMap(); System.out.println("goal"); return; } //最初にゴールの位置からアタリをつけよう・・・ if(this.goal_x > this.x && getRight() == ' '){ if(root.size() != 0) mark('$'); root.add(right); this.x += 1; execute(); return; }else if(this.goal_x < this.x && getLeft() == ' '){ if(root.size() != 0) mark('$'); root.add(left); this.x -= 1; execute(); return; } if(this.goal_y < this.y && getUp() == ' '){ if(root.size() != 0) mark('$'); root.add(up); this.y -= 1; execute(); return; }else if(this.goal_y > this.y && getDown() == ' '){ if(root.size() != 0) mark('$'); root.add(down); this.y += 1; execute(); return; } if(getUp() == ' '){ if(root.size() != 0) mark('$'); root.add(up); this.y -= 1; }else if(getRight() == ' '){ if(root.size() != 0) mark('$'); root.add(right); this.x += 1; }else if(getDown() == ' '){ if(root.size() != 0) mark('$'); root.add(down); this.y += 1; }else if(getLeft() == ' '){ if(root.size() != 0) mark('$'); root.add(left); this.x -= 1; }else{ mark('-'); if(root.getLast() == down){ this.y -= 1; }else if(root.getLast() == left){ this.x += 1; }else if(root.getLast() == up){ this.y += 1; }else if(root.getLast() == right){ this.x -= 1; } mark(' '); root.removeLast(); } execute(); } public void mark(char mk){ this.map[this.y][this.x] = mk; } public char getUp(){ return getPoint(-1, 0); } public char getRight(){ return getPoint(0, 1); } public char getDown(){ return getPoint(1, 0); } public char getLeft(){ return getPoint(0, -1); } public char getPoint(int y, int x){ int xx = this.x + x; int yy = this.y + y; if(xx > 0 && yy > 0 && xx < this.map[0].length && yy < this.map.length ){ return map[yy][xx]; } return '*'; } public void dispMap(){ for(int i=0; i<this.map.length; i++){ for(int j=0; j<this.map[i].length; j++){ System.out.print(map[i][j]); } System.out.print('\n'); } System.out.print('\n'); } }