Huawei 2017 intern on the machine test _ when traveling in a fog _java programming

Source: Internet
Author: User
Tags int size

Travel questions:

1, a total of 6 cities, a staff now from 5th City, to endcity business;

2, if a city meets the fog, then the city (can not reach, can not leave);

3, given distance matrix usehours[][6] = {

0,2,10,5,3,inf,

inf,0,12,inf,inf,10,

Inf,inf,0,inf,7,inf,

2,inf,inf,0,2,inf,

4,inf,inf,1,0,inf,

3,inf,1,inf,2,0,};

Where the unit is in hours and the INF is 1000 hours

4, enter: Destination and Fog city;

5, the output shortest path length and path, can not reach the output-1;

Instance:

Input:

2

4

Output:

6

[5 1 2]

Java Programming

Import java.util.*;

public class Main {

private static int INF = 1000;

private static integer[][] dist;

private static integer[][] path;

private static list<integer> result = new arraylist<integer> ();

Debugging

public static void Printmatrix (integer[][] matrix) {

for (int i = 0; i < matrix.length; i++) {

for (int j = 0; J < Matrix.length; J + +)

System.out.print (Matrix[i][j] + "");

System.out.println ();

}

}

Set Fog City

private static void Setfog (int[][] matrix, int city) {

for (int i = 0; i < matrix.length; i++) {

Matrix[i][city] = matrix[city][i] = INF;

}

}

public static void Main (string[] args) {

int size = 6;

int begin = 4;

Scanner scan = new Scanner (system.in);

int end = Integer.parseint (Scan.nextline ())-1;

int foggy = Integer.parseint (Scan.nextline ())-1;

Scan.close ();

int[][] Matrix = {{0, 2, 5, 3, INF},

{INF, 0, INF, INF, ten}, {inf, INF, 0, INF, 7, INF},

{2, INF, INF, 0, 2, inf}, {4, INF, INF, 1, 0, INF},

{3, INF, 1, INF, 2, 0}};

init (size);

No fog.

if (Foggy! =-1)

Setfog (Matrix, foggy);

Call Freud

Floyd (Matrix);

Findpath (begin, end);

System.out.println (Dist[begin][end]);

for (int i = 0; i < result.size (); i++)

Result.set (i, Result.get (i) + 1);

if (dist[begin][end] = = INF)

Result.removeall (result);

SYSTEM.OUT.PRINTLN (result);

}

Find the path in the path array

public static void Findpath (int i, int j) {

int ci = i, ccj = j;

while (path[i][j]! =-1) {

int CJ = Path[i][j];

Result.add (CJ);

i = CJ;

}

Result.add (0, CI);

Result.add (CCJ);

}

public static void Floyd (int[][] matrix) {

int size = Matrix.length;

for (int i = 0; i < size; i++)

for (int j = 0; J < size; J + +) {

PATH[I][J] =-1;

DIST[I][J] = Matrix[i][j];

}

for (int k = 0; k < size; k++) {

for (int i = 0; i < size; i++) {

for (int j = 0; J < size; J + +) {

if (dist[i][k]! = INF && dist[k][j]! = inf

&& Dist[i][k] + dist[k][j] < Dist[i][j]) {

DIST[I][J] = Dist[i][k] + dist[k][j];

PATH[I][J] = k;

}

}

}

}

}

Initialize two arrays

public static void init (int size) {

Path = new Integer[size][size];

dist = new Integer[size][size];

}

}

Huawei 2017 intern on the machine test _ when traveling in a fog _java programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.