Cod sursa(job #1400837)

Utilizator eu3neuomManghiuc Teodor-Florin eu3neuom Data 25 martie 2015 14:45:08
Problema Castel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.56 kb
#include <iostream>
#include <fstream>
#include <deque>

using namespace std;

ifstream fin("castel.in");
ofstream fout("castel.out");

const int NMax = 155;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
int v[NMax][NMax];
bool go[NMax * NMax];
int acces = 1,n,m;
deque < int > qx,qy;

void lee(){
    int k = 0;
    bool loop = true;
    while(loop == true){
        loop = false;
        for(int i = 0; i <= k; i++){
            for(int j = 0; j < 4; j++){
                int x = qx[i] + dx[j];
                int y = qy[i] + dy[j];
                if(v[x][y] != -1){
                    if(go[v[x][y]] == true){
                        k++;
                        qx.push_back(x);
                        qy.push_back(y);
                        acces++;
                        v[x][y] = -1;
                        go[(x - 1) * m + y] = true;
                        loop = true;
                    }
                }
            }
        }
    }
}

int main()
{
    int k;
    fin >> n >> m >> k;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            fin >> v[i][j];
        }
    }
    for(int i = 0; i <= n + 1; i++){
        v[i][0] = v[i][m + 1] = -1;
    }
    for(int i = 0; i <= m + 1; i++){
        v[0][i] = v[n + 1][i] = -1;
    }
    int r = k % m;
    int c = k / m;
    if(r == 0){
        r = m;
    } else {
        c++;
    }
    qx.push_back(c);
    qy.push_back(r);
    v[c][r] = -1;
    go[k] = true;
    lee();
    fout << acces;
    return 0;
}