Pagini recente » Cod sursa (job #380380) | Cod sursa (job #600867) | Cod sursa (job #666455) | Cod sursa (job #1613523) | Cod sursa (job #1489823)
#include <bits/stdc++.h>
const short dx[] = {0,-1,0,1};
const short dy[] = {1,0,-1,0};
using namespace std;
deque < short > lx,ly;
ifstream fin ("bila.in");
ofstream fout ("bila.out");
int ma[127][127][2],n,m;
void afis()
{
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++)
fout << ma[i][j][0] << " ";
fout << "\n";
}
}
void lee()
{
int xx,yy,nx,ny;
lx.push_back(1);
ly.push_back(1);
ma[1][1][1] = 1;
while(!lx.empty()){
xx = lx.front();
yy = ly.front();
for(int i = 0 ; i < 4; i++){
nx = xx + dx[i];
ny = yy + dy[i];
if(ma[xx][yy][0] > ma[nx][ny][0] && ma[xx][yy][1] > ma[nx][ny][1] && ma[nx][ny][0] != -1){
ma[nx][ny][1] = ma[xx][yy][1] + 1;
lx.push_back(nx);
ly.push_back(ny);
}
if(ma[xx][yy][0] < ma[nx][ny][0] && ma[nx][ny][1] == 0 && ma[nx][ny][0] != -1){
ma[nx][ny][1] == 1;
lx.push_back(nx);
ly.push_back(ny);
}
}
lx.pop_front();
ly.pop_front();
}
}
int main()
{
fin >> n >> m;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
fin >> ma[i][j][0];
for(int i = 0; i <= n+1; i++)
ma[i][0][0]=ma[i][n+1][0]=-1;
for(int i = 0; i <= m+1; i++)
ma[0][i][0]=ma[m+1][i][0]=-1;
lee();
afis();
return 0;
}