import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.LinearLayout; import android.widget.TextView; import java.util.ArrayList; import java.util.Stack; /** * @author:created By ZhangHao * 时间:2019/6/7 20 * 邮箱:188660586@qq.com */ public class query_result extends Activity { public final int stanum=118; private String[] site; public int inf=1000; private int sunOfsite; private String line; @Override protected void onCreate( Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.query_result); Site_chose site_chose=new Site_chose(); int distance[][]=new int[stanum][stanum]; int path[][]=new int[stanum][stanum]; /* * 加载布局找到控件 * */ TextView start_end =(TextView) findViewById(R.id.起点_终点); TextView sumOfsite=(TextView) findViewById(R.id.经站数); TextView balanceSite =(TextView) findViewById(R.id.剩余班车数); LinearLayout click_layout = findViewById(R.id.result_query); /*传递第一个需要上传的数据*/ Intent intent = getIntent(); String startSite = intent.getStringExtra("startSite"); String endSite=intent.getStringExtra("endSite"); start_end.setText(startSite+"-->"+endSite); /* 利用flody算法计算第二项数据*/ //传递存站点信息的数组 site = site_chose.getSite(); //初始化连接矩阵 for(int i=0;i<stanum;i++) { for(int j=0;j<stanum;j++) { if(i==j) distance[i][j]=0; else distance[i][j]=inf; } } //初始化技巧 int s1[]={0,1,2,3,4,5,6,7,106,8,9,10,109,11,108,12,13,14,15,110,16,111,17,18,19,20,107}; for( int i=0;i<s1.length-1;i++) { distance[s1[i]][s1[i+1]]=1; distance[s1[i+1]][s1[i]]=1; } int s2[]={21,22,23,24,106,25,26,27,28,113,29,112,30,31,32,33,114,34,35,115,36,37,38,107,39,40,41,42,43,44,45}; for(int i=0;i<s2.length-1;i++) { distance[s2[i]][s2[i+1]]=1; distance[s2[i+1]][s2[i]]=1; } int s3[]={46,47,48,49,50,51,52,53,54,55,112,56,108,117,116,57,58,59,60,61,62,63}; for(int i=0;i<s3.length-1;i++) { distance[s3[i]][s3[i+1]]=1; distance[s3[i+1]][s3[i]]=1; } int s4[]={64,65,66,67,68,69,70,71,72,73,74,113,75,109,116,76,77,78,79,80,81,82,83,84,85,86,87,88,89}; for(int i=0;i<s4.length-1;i++) { distance[s4[i]][s4[i+1]]=1; distance[s4[i+1]][s4[i]]=1; } int s5[]={116,117,90,91,92,93,94,95,110,96,114,97,98,99,100,101,115,111,102,103,104,105}; for(int i=0;i<s5.length-1;i++) { distance[s5[i]][s5[i+1]]=1; distance[s5[i+1]][s5[i]]=1; } floyd(distance,path); ArrayList<Object> a= printres(distance,path,startSite,endSite); sunOfsite = (int) a.get(1); System.out.println("sumofsite:"+ sunOfsite); line = a.get(0).toString(); System.out.println("line:"+ line); sumOfsite.setText(""+ sunOfsite); balanceSite.setText("30"); /*为布局添加点击事件*/ click_layout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent1 = new Intent(getApplication(),xiangxi.class); System.out.println("click_line:"+line); intent1.putExtra("line", line); intent1.putExtra("money", sunOfsite); startActivity(intent1); } }); } void floyd(int dis[][],int path[][]) { //初始化path矩阵 for(int row=0;row<stanum;row++) for(int col=0;col<stanum;col++) path[row][col]=row; //找最短路径 for(int k=0;k<stanum;k++) for(int i=0;i<stanum;i++) for(int j=0;j<stanum;j++) if(dis[i][j]>dis[i][k]+dis[k][j]) { dis[i][j]=dis[i][k]+dis[k][j]; path[i][j]=path[k][j]; } } //转换车站的名字到矩阵的索引 int string2int(String s) { for(int i=0;i<stanum;i++) { if(s.equals(site[i])) { return i; } } return 0; } ArrayList<Object> printres(int dis[][], int path[][], String start, String dest) { int s; int d; ArrayList<Object> arrayList=new ArrayList<Object>(); s=string2int(start); System.out.println("s:"+s); d=string2int(dest); System.out.println("d:"+d); System.out.println("最少经过的车站数量: "+(dis[s][d]+1)); int sumSite=dis[s][d]; System.out.println("经过的车站路径编号: "); String line=""; for(int i=0;i<stanum;i++) for(int j=0;j<stanum;j++) { if(i==s&&j==d) //输出路径 { Stack<Integer>pathrout=new Stack<>();//压栈 int k=j; do{ k=path[i][k]; Integer temp_k=new Integer(k); pathrout.push(temp_k.intValue()); }while(k!=i); //弹栈 System.out.print(site[pathrout.peek()]); line+=site[pathrout.peek()]; pathrout.pop(); int length=pathrout.size(); for(int t=0;t<length;t++) { System.out.print("->" + site[pathrout.peek()]); line += "->" + site[pathrout.peek()]; pathrout.pop(); } System.out.println("->"+site[d]); line+="->"+site[d]; break; } } arrayList.add(line); arrayList.add(sumSite); return arrayList; } }