Cod sursa(job #760219)

Utilizator andyvAndrei Vancea andyv Data 20 iunie 2012 17:13:19
Problema Barbar Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.8 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <set>
using namespace std;
int d[1001][10001], sol[1001][10001];
int r, c;
int sx, sy;
int ex, ey;

struct Point
{
	int x;
	int y;
	Point() {}
	Point (int xx, int yy) : x(xx), y(yy) {}
};


int dx[] = {-1,  0, 1, 0};
int dy[] = { 0, -1, 0, 1};

main()
{
	{
	vector<Point> buffer;
	ifstream in("barbar.in");
	in >> r >> c;
	for (int i = 0; i < r; i++)
		for (int j = 0; j < c; j++)
	{
		d[i][j] = -1;
		sol[i][j] = -1;
		char w; in >> w;
		if (w == 'I') { sx = i; sy = j;}
		else
		if (w == 'O') { ex = i; ey = j;}
		else
		if (w == 'D') {buffer.push_back(Point(i, j)); d[i][j] = 0;} 
		else
		if (w == '*') d[i][j] = -2;
	}
	int i = 0;
	while (i < buffer.size())
	{
		int x = buffer[i].x; int y = buffer[i].y;
		for (int j = 0; j < 4; j++)
		{
			int xx = x + dx[j];
			int yy = y + dy[j];
			if (xx < 0 || xx >= r || yy < 0 || yy >= c) continue;
			if (d[xx][yy] != -1) continue;
			d[xx][yy] = d[x][y] + 1;
			buffer.push_back(Point(xx, yy));
		}
		i++;
	}
	}
	{
		set< pair<int, pair<int, int> > > points;
		points.insert(make_pair(d[sx][sy], make_pair(sx, sy)));
		while (!points.empty())
		{
			pair<int, pair<int, int> > q = *points.rbegin();
			int x = q.second.first; int y = q.second.second; int w = q.first;
			for (int j = 0; j < 4; j++)
			{
				int xx = x + dx[j];
				int yy = y + dy[j];
				if (xx < 0 || xx >= r || yy < 0 || yy >= c) continue;
				if (d[xx][yy] == -2) continue;
				
				int neww = min(w, d[xx][yy]);
				if (neww > sol[xx][yy])
				{
					if (sol[xx][yy] != -1)
					{
						points.erase(make_pair(sol[xx][yy], make_pair(xx, yy)));
					}
					points.insert(make_pair(neww,make_pair(xx, yy)));
					sol[xx][yy] = neww;
				}
			}
			points.erase(q);
		}
		ofstream out("barbar.out");
		out << sol[ex][ey] << endl;
	}
	
}