Pages

বৃহস্পতিবার, ২ জানুয়ারী, ২০১৪

Solution code of uva 567-Risk


#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
int main()
{
    //freopen("c:\\temp\\in.txt","r",stdin);
    vector<int>graph[21];
    int i=1;
    int number,t=1;
    while(scanf("%d",&number)==1)
    {
        for(int j=1;j<=number;j++)
        {
            int c;
            scanf("%d",&c);
            graph[i].push_back(c);
            graph[c].push_back(i);
        }
        i++;
        if(i==20)
        {
            int query;i=1;
            scanf("%d",&query);
            printf("Test Set #%d\n",t++);
            for(int j=1;j<=query;j++)
            {
                int source,destination;

                int visit[25];
                memset(visit,0,sizeof visit);
                scanf("%d %d",&source,&destination);
                queue<int>q;
                q.push(source);
                visit[source]=1;
                while(!q.empty())
                {
                    int pop=q.front();q.pop();
                    if(pop==destination)break;
                    for(int k=0;k<graph[pop].size();k++)
                    {
                         int v=graph[pop][k];
                         if(visit[v]==0)
                         {
                             visit[v]=1+visit[pop];
                             q.push(v);
                         }
                    }
                }
             printf("%2d to %2d: %d\n",source,destination,visit[destination]-1);

            }
            printf("\n");
            for(int i=1;i<=20;i++)graph[i].clear();
        }
    }
    return 0;
}

0 comments:

একটি মন্তব্য পোস্ট করুন