Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- using namespace std;
- bool okay (unsigned short int i, unsigned short int j);
- unsigned short int N, M, K;
- unsigned short int X1, Y1, X2, Y2;
- unsigned short int XY, YY, XB, YB;
- unsigned short int X[14001], Y[14001];
- const short int dx[] = {-1, -1, 0, 1, 1, 1, 0, -1};
- const short int dy[] = { 0, 1, 1, 1, 0, -1, -1, -1};
- short int matrix[1001][1001];
- unsigned int i, j, k, nextI, nextJ;
- struct Queue
- {
- unsigned short int x;
- unsigned short int y;
- };
- Queue Q[1000010];
- unsigned int first, last;
- unsigned short int D;
- int main ()
- {
- ifstream fin ("gigelajungeacasa.in");
- fin >> N >> M >> K;
- fin >> X1 >> Y1 >> X2 >> Y2;
- fin >> XY >> YY >> XB >> YB;
- for (i=1; i<=K; i++)
- fin >> X[i] >> Y[i];
- fin.close();
- matrix[XY][YY] = -1;
- matrix[XB][YB] = -1;
- for (i=1; i<=K; i++)
- matrix[X[i]][Y[i]] = -1;
- Q[first].x = X2;
- Q[first].y = Y2;
- while (first <= last)
- {
- i = Q[first].x;
- j = Q[first].y;
- first++;
- for (k=0; k<8; k++)
- {
- nextI = i + dx[k];
- nextJ = j + dy[k];
- if (okay(nextI,nextJ) == 1 && matrix[nextI][nextJ] == 0)
- {
- matrix[nextI][nextJ] = matrix[i][j] + 1;
- last++;
- Q[last].x = nextI;
- Q[last].y = nextJ;
- }
- }
- }
- D = matrix[X1][Y1];
- ofstream fout ("gigelajungeacasa.out");
- fout << D;
- fout.close();
- return 0;
- }
- bool okay (unsigned short int i, unsigned short int j)
- {
- if (i<1 || j<1 || i>N || j>M)
- return 0;
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement