Pagini recente » Cod sursa (job #58907) | Cod sursa (job #507720) | Cod sursa (job #2937978) | Cod sursa (job #881533) | Cod sursa (job #1343156)
#include <stdio.h>
#include <vector>
#include <unistd.h>
#define BLOCKED 1
using namespace std;
struct pos{
unsigned char x;
unsigned char y;
void set(unsigned char a, unsigned char b);
};
void pos::set (unsigned char a, unsigned char b) {
x = a;
y = b;
}
int m, k;
pos S, E;
void print (unsigned char **M) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
printf("%d ", M[i][j]);
}
printf("\n");
}
}
unsigned char ** read (FILE *in) {
fscanf(in, "%d %d", &m, &k);
unsigned char **M = new unsigned char *[m];
for (int i = 0; i < m; i++) {
M[i] = new unsigned char[m]();
}
for (int i = 0; i < k; i++) {
int x, y;
fscanf(in, "%d %d", &x, &y);
M[x - 1][y - 1] = BLOCKED;
}
fscanf(in, "%hhu %hhu %hhu %hhu", &S.x, &S.y, &E.x, &E.y);
--S.x; --S.y; --E.x; --E.y;
M[S.x][S.y] = 1;
return M;
}
void lee (unsigned char **M) {
M[S.x][S.y] = 1;
vector <pos> Q;
Q.push_back(S);
int i = 0;
while (Q.at(i).x != E.x || Q.at(i).y != E.y) {
pos t = Q.at(i);
pos aux;
int mark = M[t.x][t.y] + 1;
if (t.x != 0) {
if (M[t.x - 1][t.y] == 0) {
M[t.x - 1][t.y] = mark;
aux.set(t.x - 1, t.y);
Q.push_back(aux);
}
}
if (t.x != m - 1) {
if (M[t.x + 1][t.y] == 0) {
M[t.x + 1][t.y] = mark;
aux.set(t.x + 1, t.y);
Q.push_back(aux);
}
}
if (t.y != 0) {
if (M[t.x][t.y - 1] == 0) {
M[t.x][t.y - 1] = mark;
aux.set(t.x, t.y - 1);
Q.push_back(aux);
}
}
if (t.y != m - 1) {
if (M[t.x][t.y + 1] == 0) {
M[t.x][t.y + 1] = mark;
aux.set(t.x, t.y + 1);
Q.push_back(aux);
}
}
i++;
}
}
int main (void) {
FILE *in = fopen("alee.in", "r");
FILE *out = fopen("alee.out", "w");
unsigned char **M = read(in);
lee(M);
fprintf(out, "%d", M[E.x][E.y]);
for (int i = 0; i < m; i++) {
delete[] M[i];
}
delete[] M;
fclose(in);
fclose(out);
return 0;
}