Afişează mesaje
Pagini: [1]
1  infoarena - concursuri, probleme, evaluator, articole / Arhiva de probleme / Răspuns: 477 Alee : Februarie 06, 2018, 18:16:43
error : stray \377

 Read This!
Cod:
#include<fstream>
#include<queue>


using namespace std;

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

int di[4] = { 0, 0,1, -1 };
int dj[4] = { 1, -1, 0, 0 };
int map[180][180];
int N, M, startx, starty, endx, endy;
queue<pair<int, int>> myQueue;

void read()
{
int x, y;
fin >> N >> M;
for(int i=0;i<M;i++)
{
fin >> x >> y;
map[x][y] = -1;
}
fin >> startx >> starty;
fin >> endx >> endy;
}

bool ok(int i, int j)
{
if (i<1 || j<1 || i>N || j>N)
return false;
if (map[i][j] == -1)
return false;
return true;
}

void Lee()
{
int i, j, nexti, nextj;
map[startx][starty] = 1;
myQueue.push(make_pair(startx, starty));
while (!myQueue.empty())
{
i = myQueue.front().first;
j = myQueue.front().second;
myQueue.pop();
for(int dir=0;dir<=3;dir++)
{
nexti = i + di[dir];
nextj = j + dj[dir];
if (ok(nexti, nextj) && map[nexti][nextj] ==0)
{
map[nexti][nextj] = map[i][j] + 1;
myQueue.push(make_pair(nexti, nextj));
}
}
}
}

int main()
{
read();
Lee();
fout << map[endx][endy];
    return 0;
}


Pagini: [1]
Powered by SMF 1.1.19 | SMF © 2006-2013, Simple Machines