UVa-12069-Robots inside the Labyrinth (离散化+bfs)
来源:互联网 发布:日本你是谁实验 知乎 编辑:程序博客网 时间:2024/06/16 05:19
题意:有A组测试数据,每组给定N个长方形障碍物的左下角和右上角坐标,再给定T个机器人的初始位置和目标位置,求每个机器人到目标点的最小转弯次数。
0<=N<=100
坐标范围在10^8内。
策略:暴力搜索显然是不可行的。但矩形的个数只有一百个,显然可以想到离散化坐标缩图。对于每个矩形,我们保留每个矩形边的横坐标和纵坐标及其外相邻一格的坐标,再加入起点和终点横纵坐标缩图,这样缩图后不会破坏原图的性质,并且每个坐标产生映射的范围不超过4n+2,所以缩图后图最大范围为(4n+2)*(4n+2),bfs搜索即可。
//Author:Wenjun Shi//on 2016/7/14#include <iostream>#include <map>#include <cstdio>#include <cstring>#include <utility>#include <algorithm>#include <vector>#include <queue>#define MAX_N 105#define MAX_Range 500#define lx first.first#define ly first.second#define rx second.first#define ry second.secondusing namespace std;const int dx[4]={0,-1,0,1};const int dy[4]={-1,0,1,0};vector<int> posX,posY;typedef pair<pair<int,int>,pair<int,int> > Rectangle;pair<int,int> Start,End;Rectangle rectangle[MAX_N];int n,q;bool Map[MAX_Range][MAX_Range];int used[MAX_Range][MAX_Range];void readRectangle(){ posX.clear(); posY.clear(); cin>>n; for(int i=0;i<n;i++) { //first.first==lx ;first.second=ly;second.first=rx;second.second.ry cin>>rectangle[i].lx>>rectangle[i].ly>>rectangle[i].rx>>rectangle[i].ry; posX.push_back(rectangle[i].lx); posY.push_back(rectangle[i].ly); posX.push_back(rectangle[i].lx-1); posY.push_back(rectangle[i].ly-1); posX.push_back(rectangle[i].rx); posY.push_back(rectangle[i].ry); posX.push_back(rectangle[i].rx+1); posY.push_back(rectangle[i].ry+1); }}//离散化建图void discrete(){ memset(Map,false,sizeof(Map));//false代表非墙 sort(posX.begin(),posX.end()); sort(posY.begin(),posY.end()); posX.resize(unique(posX.begin(),posX.end())-posX.begin()); posY.resize(unique(posY.begin(),posY.end())-posY.begin()); int x1,y1,x2,y2; for(int i=0;i<n;i++) { x1=lower_bound(posX.begin(),posX.end(),rectangle[i].lx)-posX.begin()+1; x2=lower_bound(posX.begin(),posX.end(),rectangle[i].rx)-posX.begin()+1; y1=lower_bound(posY.begin(),posY.end(),rectangle[i].ly)-posY.begin()+1; y2=lower_bound(posY.begin(),posY.end(),rectangle[i].ry)-posY.begin()+1; for(int j=y1;j<=y2;j++) Map[x1][j]=Map[x2][j]=true; //true代表墙 for(int j=x1;j<=x2;j++) Map[j][y1]=Map[j][y2]=true; }}struct node{ int x,y,turns; node(int a,int b,int c) { x=a;y=b;turns=c; }};int bfs(){ int uplimit=posY.size()+1,rightlimit=posX.size()+1; memset(used,-1,sizeof(used)); queue<node> Q; Q.push(node(Start.first,Start.second,-1)); used[Start.first][Start.second]=0; while(!Q.empty()) { node tmp=Q.front();Q.pop(); int nturns=tmp.turns+1; for(int k=0;k<4;k++) { int nx=tmp.x+dx[k],ny=tmp.y+dy[k]; while(nx>=0 && ny>=0 && nx<=rightlimit && ny<=uplimit && Map[nx][ny]==false && (used[nx][ny]<0||nturns<=used[nx][ny]) ) { used[nx][ny]=nturns; Q.push(node(nx,ny,nturns)); nx+=dx[k]; ny+=dy[k]; } } if(used[End.first][End.second]>=0) return used[End.first][End.second]; } return -1;}void solve(){ int ans; cin>>q; for(int i=0;i<q;i++){ cin>>Start.first>>Start.second>>End.first>>End.second; posX.push_back(Start.first); posY.push_back(Start.second); posX.push_back(End.first); posY.push_back(End.second); discrete(); Start.first=lower_bound(posX.begin(),posX.end(), Start.first)-posX.begin()+1; Start.second=lower_bound(posY.begin(),posY.end(),Start.second)-posY.begin()+1; End.first=lower_bound(posX.begin(),posX.end(), End.first)-posX.begin()+1; End.second=lower_bound(posY.begin(),posY.end(),End.second)-posY.begin()+1; ans=bfs(); if(ans>=0) cout<<ans<<endl; else cout<<"Impossible."<<endl; }}int main(){ // freopen("E:\\out.txt","w",stdout); int cas; cin>>cas; for(int i=1;i<=cas;i++) { printf("Labyrinth #%d\n",i); readRectangle(); solve(); } return 0;}
0 0
- UVa-12069-Robots inside the Labyrinth (离散化+bfs)
- codeforces 616C The Labyrinth(bfs)
- CodeForces 616C:The Labyrinth(BFS)
- hdu 2771(uva 12171) Sculpture bfs+离散化
- URAL 1145. Rope in the Labyrinth(两次BFS啊 )
- uva 1077 - The Sky is the Limit(离散化)
- hdu5925 离散化+bfs
- poj 1383 Labyrinth bfs
- BFS 1056. Labyrinth
- POJ 1383 --Labyrinth【BFS】
- hdu5925 Coconuts(离散化+BFS)
- hdu5925 Coconuts 【离散化+bfs】
- hdu 5925(离散化+BFS)
- UVA 10047 - The Monocycle BFS
- UVA 10047 The Monocycle (BFS)
- 【UVa】10047 The Monocycle BFS
- UVA 10047 The Monocycle (bfs)
- uva 10047 The Monocycle BFS
- 在一个字符串中找到第一个只出现一次的字符。如输入:abcdab,则输出:c。
- 【Leetcode】 LRU Cache
- UESTC 483 Data Structure Problem
- UITextField的使用
- 【Leetcode】Unique Binary Search Trees
- UVa-12069-Robots inside the Labyrinth (离散化+bfs)
- JAVA学习笔记--多线程(二)线程常用方法
- ListView中notifyDataSetChanged()无法刷新数据的错误实例
- java 读取json文件
- 经典重拾-第一部分 第三章-数组和字符串
- MY_GEEK_MYMENU--左右切换菜单
- Android View 绘制流程(Draw) 完全解析
- 畅通工程 最短路数
- Android系统框架介绍