Pagini recente » Cod sursa (job #2073459) | Cod sursa (job #1943079) | Cod sursa (job #1773033) | Cod sursa (job #1939749) | Cod sursa (job #1749456)
#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( ) )
{
frc[ lst[ aux ].front( ) ] = 1;
q.push( lst[ aux ].front( ) );
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;
}