传送门:点击打开链接
题意:一个N*N的网格,从左下角走到右上角需要的最短步数。要求不能经过# 而且路径不能经过一个三角形的内部,可以沿边走。
分析:计算几何题里不算难题,但若没想到坑点就一直卡题了。
我采用的判断线段相交方法是规范相交,判断点是否在线段上包括了点在线段端点的情况
假设当前路径是线段L,三角形为ABC
先预处理,若格子在三角形内部,设置为#
case1:L的一个端点在外,一个在内。只要枚举L跟三角形所有边是否规范相交即可。
case2:L的两个端点都在三角形的边上,判断L的中点是否在三角形内部即可
case3:最难想到吧。。一个端点在边上,另一个端点经过了三角形的一个顶点,而且L的中点在三角形外部。。
只要稍微缩短一下L的长度,也就是L的两个端点彼此靠拢很微小的距离,再判断两个端点是否在三角形内部。
还是刘汝佳蓝书的模板用的爽啊~~
AC代码:
#include <cstdio>
#include <cmath>
#include <queue>
#include <vector>
#include <cstring>
using namespace std;
const double t = 1e-3;
const double eps = 1e-8;
int dcmp(double x){
if (fabs(x)<eps) return 0;
return x<0?-1:1;
}
struct Point{
double x,y;
Point(double x=0.0, double y=0.0) : x(x) , y(y) {}
bool operator ==(const Point & rhs) const {
return dcmp(x-rhs.x) == 0 && dcmp(y-rhs.y) == 0;
}
};
Point A,B,C;
vector<Point>poly;
typedef Point Vector;
Vector operator +(Vector A, Vector B) {return Vector(A.x+B.x,A.y+B.y); }
Vector operator -(Vector A, Vector B) {return Vector(A.x-B.x,A.y-B.y); }
Vector operator *(Vector A, double p) {return Vector(A.x*p,A.y*p); }
double Dot(Vector A, Vector B) {return A.x*B.x+A.y*B.y; }
double Cross(Vector A, Vector B) {return A.x*B.y-A.y*B.x; }
bool isPointOnSegment(Point p, Point a1, Point a2) {
if ( dcmp(Cross(a1-p,a2-p)) == 0 && dcmp(Dot(a1-p,a2-p))<0 ) return 1;
return a1 == p || a2 == p;
}
struct Line{
Point p1,p2;
Line() {}
Line(Point p1, Point p2) : p1(p1),p2(p2) {}
void change(double t) {
Vector v = p2-p1;
p1 = p1 + v*t;
v = v*(-1.0);
p2 = p2 + v*t;
}
};
Line AB,AC,BC;
bool LineIntersection(Line a, Line b) {
double c1 = Cross(a.p2-a.p1,b.p1-a.p1), c2 = Cross(a.p2-a.p1,b.p2-a.p1);
double c3 = Cross(b.p2-b.p1,a.p1-b.p1), c4 = Cross(b.p2-b.p1,a.p2-b.p1);
return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0;
}
const int maxn = 25;
char s[maxn][maxn];
int n;
int isPointInPolygon(Point p) {
int wn = 0 ,n = poly.size();
for (int i=0; i<n; i++) {
if (isPointOnSegment(p,poly[i],poly[(i+1)%n])) return -1;
int k = dcmp(Cross(poly[(i+1)%n]-poly[i],p-poly[i]));
int d1 = dcmp(poly[i].y-p.y);
int d2 = dcmp(poly[(i+1)%n].y-p.y);
if (k>0 && d1<=0 && d2>0) wn++;
if (k<0 && d2<=0 && d1>0) wn--;
}
if (wn!=0) return 1;
return 0;
}
void init(){
double x,y;
scanf("%lf%lf",&x,&y); A = Point(x,y);
scanf("%lf%lf",&x,&y); B = Point(x,y);
scanf("%lf%lf",&x,&y); C = Point(x,y);
poly.clear();
AB = Line(A,B); poly.push_back(A);
BC = Line(B,C); poly.push_back(B);
AC = Line(A,C); poly.push_back(C);
char tmp[maxn][maxn];
for (int i=0; i<n; i++) scanf("%s",tmp[i]);
for (int i=0; i<n; i++)
for (int j=0; j<n; j++) s[i][j] = tmp[n-1-j][i];
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) if (s[i][j] == '.') {
if (isPointInPolygon(Point(i,j)) == 1) s[i][j] = '#';
}
}
}
queue<Point>que;
int step[maxn][maxn];
int dx[] = {-1,1,0,0 , -1,-1,1,1};
int dy[] = {0,0,1,-1 , -1,1,-1,1};
int BFS(){
while (!que.empty()) que.pop();
memset(step,-1,sizeof(step));
step[0][0] = 0;
int x,y;
int tx,ty;
Point p,q;
Point mid,tpoint;
Vector v;
p = Point(0.0,0.0); que.push(p);
while (!que.empty()) {
p = que.front(); que.pop();
x = (int)p.x; y = (int)p.y;
s[x][y] = '#';
for (int i=0; i<8; i++) {
tx = x + dx[i];
ty = y + dy[i];
if (tx<0 || ty<0 || tx>=n || ty>=n) continue;
if (s[tx][ty] == '#') continue;
if (step[tx][ty]!=-1) continue;
q = Point(1.0*tx,1.0*ty);
Line L = Line(p,q);
// case1
if (LineIntersection(L,AB) || LineIntersection(L,AC) || LineIntersection(L,BC)) continue;
//casee2
mid = Point((x+tx)*0.5,(y+ty)*0.5);
if ( isPointInPolygon(mid) == 1 ) continue;
// case3
L.change(t);
if (isPointInPolygon(L.p1) == 1 || isPointInPolygon(L.p2) == 1) continue;
step[tx][ty] = step[x][y]+1;
que.push(q);
}
}
return step[n-1][n-1];
}
int main(){
while (scanf("%d",&n)==1) {
init();
printf("%d\n",BFS());
}
return 0;
}