The Celebrity Problem

#include<bits/stdc++.h>
using namespace std;
int f(int n,int ar[4][4])
{
    stack<int>s;
    int i,a,b;
    for(i=0; i<3; i++)
    {
        s.push(i);
    }

    while(s.size()>1)
    {
        a=s.top();
        s.pop();
         b=s.top();
         s.pop();
        if(ar[a][b]==0)
        {
            s.push(a);
        }
        else
        {
            s.push(b);
        }
    }
    int x=s.top();
    //cout<<"ss"<<x<<endl;
    int flag=1;
    for(i=0; i<3; i++)
    {
        if(i==x)
            continue;
        if(ar[x][i]==1 ||ar[i][x]==0)
        {
            flag=0;
        }

    }
    if(flag==0) return -1;
        return x;
}
int main()
{
    int ar[4][4]= {{0,1,0},{0,0,0},{0,1,0}},i,j;
    cout<<f(3,ar);
    return 0;
}

মন্তব্যসমূহ

এই ব্লগটি থেকে জনপ্রিয় পোস্টগুলি

BSF

DFS

Dijkstra's shortest path algorithm-2