あにずむ

Learn from yesterday, live for today, hope for tomorrow.

 

java

ふ・・w できませんでしたよ!どうせ! 人材獲得作戦・4 試験問題ほか3 このエントリーをはてなブックマークに追加

( ゜▽゜)/こんばんは!

こんなのがありまして、、、

人材獲得作戦・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');
	}
}

幻の本を求めて・・・続編5 このエントリーをはてなブックマークに追加

( ゜▽゜)/こんばんわ!

おひさしぶりですw

さて、以前こんな記事を書いたのですが
皆様覚えてらっしゃいますでしょうか?
幻の本を求めて

良いプログラマを目指すなら「Java並行処理プログラミング」は今すぐ読むべき


というエントリを見て、すっごい欲しくなった
「Java並行処理プログラミング」

実は「幻の本を求めて」には続きがあって

紀伊国屋書店BookWebに在庫あった!
と喜んでたんですが、後日

「品切れでしたすみません!」

的なメールが来て結局手に入らなかったのです^^;

そ・れ・が!

さっきふとAmazon徘徊してたら・・・

じゃじゃーん!

Java並行処理プログラミング ―その「基盤」と「最新API」を究める―Java並行処理プログラミング ―その「基盤」と「最新API」を究める―
著者:Brian Goetz
販売元:ソフトバンククリエイティブ
発売日:2006-11-22
おすすめ度:5.0
クチコミを見る

在庫あり!!!!(2009/12/23 21:28現在)

うおーーーーーーーー!

ってなわけで早速お急ぎ便注文!

サンタさんは私にとっても素敵なプレゼントをくれたみたいです

#探してた方いらっしゃいましたらお早めにw
こんてんつ
アクセスカウンター
  • 今日:
  • 昨日:
  • 累計:

相互リンク☆
iphoneアプリ,ipodアプリの動画レビューサイト
TopHatenar
ぷちカンパ
WebMoney ぷちカンパ
人気ブログランキング
人気ブログランキング
にほんブログ村
ブログランキング【ブロブロ】
Silverlignt
ブログタイムズ
  • ライブドアブログ