Advertisement
SergeyPGUTI

11.1.3

Apr 16th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4. //матрица смежности
  5. class AdjacencyMatrix {
  6.     private int Matrix[][];
  7.     private int n;
  8.     private int m;
  9.     public AdjacencyMatrix(int n,int m)
  10.     {
  11.         Matrix=new int[n][m];
  12.         this.n=n;
  13.         this.m=m;
  14.     }
  15.     //считать матрицу с консоли
  16.     public void consoleRead()
  17.     {
  18.         Scanner sc=new Scanner(System.in);
  19.         for (int i=0;i<n;i++)
  20.             for (int j=0;j<m;j++)
  21.                 Matrix[i][j]=sc.nextInt();
  22.     }
  23.     //вернуть элемент матрицы
  24.     public int get(int i,int j)
  25.     {
  26.         return Matrix[i][j];
  27.     }
  28. }
  29. //задание 3
  30. public class Ex_1_3 {
  31.     public static void  main(String args[])
  32.     {
  33.         int n,counter=0; // кол-во мостов, кол-во плохих мостов
  34.         int [] colors; //цвета мостов
  35.  
  36.         Scanner sc=new Scanner(System.in);
  37.         n=sc.nextInt();
  38.         //иницилизировать и считать матрицу смежности
  39.         AdjacencyMatrix matrix=new AdjacencyMatrix(n,n);
  40.         matrix.consoleRead();
  41.         //иницилизировать и считать цвета мостов
  42.         colors=new int[n];
  43.         for (int i=0;i<n;i++)
  44.         {
  45.             colors[i]=sc.nextInt();
  46.         }
  47.  
  48.         //найти мосты , если найден проверить совпадают ли цвета островов
  49.         for (int i=0;i<n;i++)
  50.         {
  51.             for (int j=0;j<i;j++)
  52.             {
  53.                 if (matrix.get(i,j)==1)
  54.                     if (colors[i]!=colors[j])
  55.                         counter++;
  56.             }
  57.         }
  58.         System.out.println(counter);
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement