Cod sursa(job #1943368)

Utilizator nicu_serteSerte Nicu nicu_serte Data 28 martie 2017 15:42:11
Problema Matrice 2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.05 kb
#include <fstream>
using namespace std;
ifstream fin("matrice2.in");
ofstream fout("matrice2.out");
int a[502][502], n, m, xi, yi, xf, yf, dmax;
void citire()
{
    int i, j;
    fin>>n>>m;
    for(i=1; i<=n; i++)
        for(j=1; j<=n; j++)
            fin>>a[i][j];
}
void drum(int x, int y, int cost)
{
    int aux;
    if(x>=1 && y>=1 && x<=n && y<=n)
        if(a[x][y])
        {
            aux=a[x][y];
            a[x][y]=0;
            if(x==xf && y==yf)
            {
                if(cost>dmax)
                    dmax=cost;
                a[x][y]=aux;
                return;
            }
            cost=min(aux, cost);
            drum(x+1, y, cost);
            drum(x-1, y, cost);
            drum(x, y+1, cost);
            drum(x, y-1, cost);
            a[x][y]=aux;
        }
}
int main()
{
    citire();
    while(m)
    {
        m--;
        fin>>xi>>yi>>xf>>yf;
        dmax=0;
        drum(xi, yi, a[xi][yi]);
        fout<<dmax<<'\n';
    }
    fin.close();
    fout.close();
    return 0;
}