Val_Kir

2lab4

Sep 26th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.33 KB | None | 0 0
  1. /* Выяснить есть ли в графе путь из вершины i в вершину j. */
  2.  
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <stdio.h>
  6.  
  7. typedef unsigned int word;
  8.  
  9. word inbool(int n)
  10. {
  11.     word res=0,mask=1;
  12.     char s[33];
  13.     gets(s);
  14.     for(int i=0;i<n;++i,mask<<=1)
  15.     {
  16.         if(s[i]=='1') res|=mask;
  17.     }
  18.     return res;
  19. }
  20.  
  21. void outbool(word v, int n)
  22. {
  23.     word mask=1;
  24.     for(int i=0; i<n;++i,mask<<=1)
  25.     {
  26.         if(v&mask) printf("1");
  27.         else printf("0");
  28.     }
  29.     printf("\n");
  30. }
  31.  
  32. void main()
  33. {
  34.     int n,i,j;
  35.     word m[32],res=0,to_look=0,looked=0;
  36.     /*
  37.     res-вершины, которые надо просмотреть в следующем цикле
  38.     to_look- мн-во, которое надо просмотреть
  39.     looked- просмотренные вершины
  40.     */
  41.     scanf("%d",&n);
  42.     fflush(stdin);
  43.  
  44.     for(int i=0; i<n; ++i)
  45.         m[i]=inbool(n);
  46.  
  47.     putchar('\n');
  48.  
  49.     scanf("%d %d",&i,&j);
  50.    
  51.     to_look=1<<i;
  52.  
  53.     while(to_look)
  54.     {
  55.         for(int i=0; i<n;i++) //перебираем все вершины из to_look и запоминаем их соседей
  56.             if(to_look&(1<<i)) res|=m[i]; //два амперсанда
  57.  
  58.         if(res&(1<<j))
  59.         {
  60.             printf("yes\n");
  61.             return;
  62.         }
  63.         looked|=to_look;
  64.         to_look=res^(looked&res);
  65.     }
  66.     printf("no\n");
  67. }
Add Comment
Please, Sign In to add comment