Pagini recente » Cod sursa (job #1149741) | Cod sursa (job #3120543) | Cod sursa (job #1745306) | Cod sursa (job #2110579) | Cod sursa (job #2579124)
#include <iostream>
#include <fstream>
#include <queue>
#include <bitset>
#include <vector>
#define per pair <unsigned char, unsigned char>
#define xx first
#define yy second
using namespace std;
ifstream in ("castel.in");
ofstream out("castel.out");
int dx[]={0, 1, 0, -1, 0};
int dy[]={0, 0, 1, 0, -1};
int n, m, k, rez=1;
bool accesabil[22510];
bitset <153> b[153];
vector <per> rem[22510];
short int neckey[153][153];
inline int tocode(int x, int y)
{
return (x-1)*m+y;
}
inline per fromcode(int cod)
{
int x=cod/m; x++;
int y=cod%m;
if(y==0) y=m, x--;
return {(unsigned char) x, (unsigned char) y};
}
int main()
{
in>>n>>m>>k;
for(int i=0; i<=n+1; i++)
b[i][0]=b[i][m+1]=1;
for(int i=0; i<=m+1; i++)
b[0][i]=b[n+1][i]=1;
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
in>>neckey[i][j];
queue <per> q;
q.push(fromcode(k));
per temp=fromcode(k);
b[temp.xx][temp.yy]=1;
accesabil[k]=true;
while(!q.empty())
{
per elem=q.front();
q.pop();
for(int i=1; i<=4; i++)
{
int xt=elem.xx+dx[i], yt=elem.yy+dy[i];
if(b[xt][yt]==0) //daca nu am gandit asupra lui
{
b[xt][yt]=1;
if(accesabil[neckey[xt][yt]]==true) //daca pot sa intru in el
{
q.push( {(unsigned char)(xt), (unsigned char)(yt)} ); rez++;
int codact=tocode(xt, yt);
if(accesabil[codact]==false) //daca imi deschide mai multe posibilitati
{
accesabil[codact]=true;
while(!rem[codact].empty()) //imi adaug posibilitatile
{
q.push({rem[codact].back().xx, rem[codact].back().yy}); rez++;
rem[codact].pop_back();
}
}
}
else
{
rem[neckey[xt][yt]].push_back({(unsigned char)(xt), (unsigned char)(yt)});
}
}
}
}
out<<rez;
return 0;
}