Cod sursa(job #1749504)

Utilizator vlasiuflaviusVlasiu Flavius vlasiuflavius Data 28 august 2016 13:57:13
Problema Castel Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.46 kb
#include <fstream>
#include <iostream>
#include <queue>
///#include <valuta>
using namespace std;
ofstream fout ("castel.out");
ifstream fin ("castel.in");
queue <int> q;
queue <int> lst[23000];
int dx[] = { 1 , -1 , 0 , 0 };
int dy[] = { 0 , 0 , -1 , 1 };
int aux,n,m,l,frc[23000],v[23000],k,i,rsp,poz,ln,col,j;
int main()
{
    fin>>n>>m>>k;
    for( i = 1 ; i <= n * m ; i++ )
        fin>>v[ i ];
    q.push( k );
    frc[ k ] = 1;
    while( q.size( ) )
    {
        aux = q.front( );
        q.pop( );
        while( lst[ aux ].size( ) )
        {
            if( frc[ lst[ aux ].front( ) ] == 0 )
                q.push( lst[ aux ].front( ) );
            frc[ lst[ aux ].front( ) ] = 1;
            lst[ aux ].pop( );
        }
        for( i = 0 ; i < 4 ; i++ )
        {
            ln = ( aux - 1 ) / m + 1 + dx[ i ];
            col = ( aux - 1 ) % m + 1 + dy[ i ];
            if( ln < 1 || col < 1 || ln > n || col > m )
                continue;
            if( frc[ v[ ( ln - 1 ) * m + col ] ] == 1 && frc[ ( ln - 1 ) * m + col ] == 0 )
            {
                frc[ ( ln - 1 ) * m + col ] = 1;
                q.push( ( ln - 1 ) * m + col );
            }
            else if( frc[ ( ln - 1 ) * m + col ] == 0 )
                lst[ v[ ( ln - 1 ) * m + col ] ].push( ( ln - 1 ) * m + col );
        }
    }
    for( i = 1 ; i <= n * m ; i++ )
        if( frc[ i ] )
            rsp++;
    fout<<rsp;
    return 0;
}