#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<algorithm>
#include<numeric>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#pragma warning(disable:4996)
using namespace std;
class Block//障碍物类,(x1,y1)表示其左下角坐标,(x2,y2)表示其右上角坐标
{
public:
int x1, x2, y1, y2;
Block(int X1 = 0, int Y1 = 0, int X2 = 0, int Y2 = 0)
{
x1 = X1, y1 = Y1, x2 = X2, y2 = Y2;
}
};
class Point//点类
{
public:
int x, y;
Point(int X=0,int Y=0)
{
x = X, y = Y;
}
};
class Position:public Point//位置类,在点类的基础上,增加了当前的运动方向
{
public:
int di;
Position(int X = 0, int Y = 0,int D=0):Point(X, Y)
{
x = X, y = Y, di = D;
}
};
//离散化坐标,并返回图
vector<vector<bool>>discretize(vector<Block>&block, vector<pair<Point, Point>>&path)
{
map<int, int>Map;
for (size_t i = 0; i != block.size(); i++)
{
Map.insert({ block[i].x1,0 });
Map.insert({ block[i].x2,0 });
Map.insert({ block[i].y1,0 });
Map.insert({ block[i].y1,0 });
}
for (size_t i = 0; i != path.size(); i++)
{
Map.insert({ path[i].first.x,0 });
Map.insert({ path[i].first.y,0 });
Map.insert({ path[i].second.x,0 });
Map.insert({ path[i].second.y,0 });
}
int count = 1;
for (auto iter = Map.begin(); iter != Map.end(); iter++)
{
iter->second = count++;
}
int max_x, max_y;
for (size_t i = 0; i != block.size(); i++)
{
block[i].x1 = Map[block[i].x1];
max_x = max(max_x, block[i].x1);
block[i].x2 = Map[block[i].x2];
max_x = max(max_x, block[i].x2);
block[i].y1 = Map[block[i].y1];
max_y = max(max_y, block[i].y1);
block[i].y2 = Map[block[i].y2];
max_y = max(max_y, block[i].y2);
}
for (size_t i = 0; i != path.size(); i++)
{
path[i].first.x = Map[path[i].first.x];
max_x = max(max_x, path[i].first.x);
path[i].first.y = Map[path[i].first.y];
max_y = max(max_y, path[i].first.y);
path[i].second.x = Map[path[i].second.x];
max_x = max(max_x, path[i].second.x);
path[i].second.y = Map[path[i].second.y];
max_y = max(max_y, path[i].second.y);
}
vector<vector<bool>>visited(max_x + 2, (vector<bool>)(max_y + 2));
for (size_t i = 0; i != block.size(); i++)
{
for (auto j = block[i].x1; j <= block[i].x2; j++)
{
visited[j][block[i].y1] = true;
}
for (auto j = block[i].x1; j <= block[i].x2; j++)
{
visited[j][block[i].y2] = true;
}
}
return visited;
}
const vector<Point>direction{ {0,-1},{0,1},{-1,0},{1,0} };
int bfs(const Point&start,const Point &end,vector<vector<bool>>visited)
{
vector<vector<int>>turn(visited.size(), (vector<int>)visited[0].size());
for (size_t i = 0; i != turn.size(); i++)
{
for (size_t j = 0; j != turn[i].size(); j++)
{
turn[i][j] = 1000000000;
}
}
turn[start.x][start.y] = 0; visited[start.x][start.y] = true;
queue<Position>Q;
for (int i = 0; i < 4; i++)
{
Q.push({ start.x,start.y,i });
}
while (!Q.empty())
{
auto curpos = Q.front(); Q.pop();
for (int i = 0; i < 4; i++)
{
auto nextpos = curpos;
nextpos.x += direction[i].x;
nextpos.y += direction[i].y;
nextpos.di = i;
if (nextpos.x>=0&&nextpos.x<visited.size()&&nextpos.y>=0&&nextpos.y<visited[0].size())
{
if(turn[nextpos.x][nextpos.y]>turn[curpos.x][curpos.y]+(curpos.di==nextpos.di?0:1))
{
turn[nextpos.x][nextpos.y] = turn[curpos.x][curpos.y] + (curpos.di == nextpos.di ? 0 : 1);
}
if (!visited[nextpos.x][nextpos.y])
{
visited[nextpos.x][nextpos.y] = true;
Q.push(nextpos);
}
}
}
}
return turn[end.x][end.y];
}
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int T,count=0; cin >> T;
while (T--)
{
int N; cin >> N;//障碍物的数量
vector<Block>block(N);
for (int i = 0; i != N; i++)
{
cin >> block[i].x1 >> block[i].y1 >> block[i].x2 >> block[i].y2;
}
int K; cin >> K;//请求的数量
vector<pair<Point, Point>>path(K);
for (int i = 0; i != K; i++)
{
cin >> path[i].first.x >> path[i].first.y >> path[i].second.x >> path[i].second.y;
}
auto visited = discretize(block,path);
printf("Labyrinth #%d\n",++count);
for (int i = 0; i != K; i++)
{
auto ans= bfs(path[i].first,path[i].second,visited);
if (ans >= 0 && ans < 1000000000)
{
cout << ans << endl;
}
else
{
cout << "Impossible." << endl;
}
}
}
return 0;
}
uva_12069_Robots inside the Labyrinth_TLE
最新推荐文章于 2023-01-16 17:47:31 发布