Algorithms and data structures (11) Figure 2 - Application of articles

Topic 1: Rescue 007

Here Insert Picture Description
As illustrated, the island 007 around an initial position, the island lakes, assume that the plurality of surrounding lake alligator, crocodile stationary, a lake shore rescue program 007 is adopted: hopping from Island to crocodile head, another jump in the crocodile's head, assuming 007 per beat length 1, the writing program, request 007 can skip the shore;
Here Insert Picture Description
could each point as a crocodile; Here Insert Picture Description
as , each of the points shown in FIG crocodile, regarded as a node, this problem can be seen as in FIG dfs or bfs be solved, only smaller than the distance between two points in the range of 007 to jump, i.e. two junction there is an edge between the points;

Then select bfs thinking or dfs, which is better algorithm do?
Clearly, better depth-first, because our final exit conditions for the jump to the shore;

code show as below:

public class LiveOrDie {
    private Node[] crocodiles;//鳄鱼的结点
    private int xEdge;//x轴上的边界,若设为50,即范围-50~50
    private int yEdge;//y轴上的边界,若设为50,即范围-50~50
    private Node startPos;//007开始位置

    class Node {
        int x;
        int y;
        boolean visited;

        public Node(int x, int y) {
            this.x = x;
            this.y = y;
            this.visited = false;
        }
		//用来判断边是否存在
        public boolean canJump(Node node) {
            int x1 = this.x - node.x;
            int y1 = this.y - node.y;
            double distance = Math.sqrt(Math.pow(x1, 2) + Math.pow(y1, 2));
            return distance < 1;
        }
    }

    public boolean liveOrDie(Node node) {
        if ((Math.abs(node.x) - xEdge) < 1 || (Math.abs(node.y) - yEdge) < 1) return true;
        node.visited = true;
        for (int i = 0; i < crocodiles.length; i++) {
            if (node.canJump(crocodiles[i]) && !crocodiles[i].visited) {
                return liveOrDie(crocodiles[i]);
            }
        }
        return false;
    }
}

Note : This title, did not achieve a data structure diagram, because there is no need to realize that this topic involved in drawing its data structures just more than a layer has a boundless relationship, this question, we can in the Node class in canJump by visted and there is a direct method of determining whether or not edge;
data structure, if implemented, or whether it is the adjacency matrix adjacency table, will be more trouble to obtain the answer;

Topic 2: Six Degrees of Separation
a mathematics conjecture, called Six Degrees of Separation, theory states that: you and anyone that elapses between a stranger no more than six words, by up to six middleman you You will be able to recognize any stranger;

Here Insert Picture Description

That is, people who know you through your relationship layer, when this relationship is greater than the layer 6, you may know a single person;

Given the social network diagram, calculate for each node in line with the percentage of "Six Degrees of Separation" theory nodes accounted for summary points

Conversion is the language code: In the data structure of a given figure, starting from a node outside the six-phase expansion and it has edge nodes, the nodes contain all six of this figure?

Consider algorithm, six layers expand outward; it is clear that this question should use breadth-first, BFS; and can only do 6 layers, and the need to preserve access to the node;
bfs review:

public void bfs(Vertex v){
	Queue que = new Queue();
	v.visited = true;
	que.add(v);
	while(!que.isEmpty){
    Vertex v2 = que.pop();
		for(v2的邻接点:w){	
			que.add(w);
			w.visited = true;
		}
	}
}

This Title pseudocode:

int bfs(Vertex x){
	int level = 0;//层数;
	int count = 1;//此人肯定认识自己;
	Vertx last = x;
	Queue queue = new Queue();//得到队列对象
	queue.add(x);
	x.visited = true;
	while(!queue.isEmpty()){
		Vertex v = queue.pop();
		vertex  tail;
		for(v的邻接结点 w:v){
			if(!w.visited){
				w.visited = true;
				queue.add(w);
				count++;
				tail=w;
			}
		}
		//逻辑,每一层的bfs时,当for循环结束时,tail指向了其这一层的最后结点,保存起来;
		//v=last时,表示这一层意见循环结束了,level应该+1了;
		if(v == last){
			level++;
			last = tail;//last等于其这一层的最后一个结点
		}
		if(level == 6) break;
	}
	return count;
}
Published 17 original articles · won praise 0 · Views 359

Guess you like

Origin blog.csdn.net/qq_32193775/article/details/104146261