Liaoning Ship’s Voyage

LiaoningshipLiaoning\ shipLiaoning ship, which named after a province of China, is the first aircraft carrier commissioned into the People′sLiberationArmyNavy.People's\ Liberation\ Army\ Navy.People′s Liberation Army Navy. It was bought from UkraineUkraineUkraine as a stripped hulk and was rebuilt by China as an important part of China’s blue water Navy plan. Liaoning ship has sailed far into the Pacific Ocean for serval times, which shows the power and resolve of China to defend her integrity of territory.

Now LiaoningshipLiaoning\ shipLiaoning ship is on a new voyage to the Atlantic Ocean for a maneuver! The vast maneuver region on the ocean can be seen as an n×nn\times nn×n grid which has n×nn\times nn×n crosspoints. Each crosspoint stands for a check point of the maneuver region. Liaoning starts from the bottom-left check point whose coordinate is (0,0)(0,0)(0,0), and its destination is the upper-right checkpoint whose coordinate is (n−1,n−1).(n-1,n-1).(n−1,n−1). The positive side of the xxx axis points to the right, and the positive side of the y axis points up. All check points’ coordinates are integral. During each move, LiaoningLiaoningLiaoning can go from one check point to its adjacent 888 check points along a straight line, and each move takes LiaoningLiaoningLiaoning one day. Some check points are not available to go due to the bad weather. And, as you know, on the Atlantic Ocean, there is a Bermuda Triangle in which many ships and planes were missing. LiaoningLiaoningLiaoning can’t take risk to go into that triangle. Of course, Liaoning can’t go outside the maneuver region. Please figure out a route for Liaoning to reach its destination as soon as possible.

Input

There are no more than 303030 test cases.

For each case:

The first line is an integer n(2≤n≤20),n( 2 \leq n \leq 20),n(2≤n≤20), meaning that the maneuver region is an n×nn\times nn×n grid.

The seconds line contains six float numbers x1,y1,x2,y2,x3,y3(−100≤x1,y1,x2,y2,x3,y3≤100)x_1,y_1,x_2,y_2,x_3,y_3( -100 \leq x_1,y_1,x_2,y_2,x_3,y_3 \leq 100)x1​,y1​,x2​,y2​,x3​,y3​(−100≤x1​,y1​,x2​,y2​,x3​,y3​≤100) which have at most 222 digits after the decimal point, indicating the coordinates of the vertices of Bermuda Triangle are (x1,y1),(x2,y2)(x_1,y_1), (x_2,y_2)(x1​,y1​),(x2​,y2​) and (x3,y3).(x_3,y_3).(x3​,y3​). Liaoning ship can’t go into that triangle, but going along its edges or touching its vertices are allowed.

Then an n×n character matrix consists of ‘.’ and ‘#’ follows, indicating the weather condition of all check points. ‘.’ Means good weather and ‘#’ means bad weather. The bottom-left character stands for the weather of check point (0,0),(0,0),(0,0), the character on the right side of it stands for (1,0),(1,0),(1,0), and the upper-right character stands for (n−1,n−1).(n-1,n-1).(n−1,n−1).

It is guaranteed that check point (0,0)(0,0)(0,0) is not inside the Bermuda Triangle or on the edges of the triangle.

Output

For each test case, print the minimum days Liaoning ship needs to reach its destination. If it is impossible for Liaoning to reach its destination, print -1 instead.

Sample Input

3
0.5 1.5 1.5 1.5 1 0.5
.#.
...
..#
3
0.5 1.5 1.5 1.5 1 0.5
.#.
..#
..#

Sample Output

3
-1

题意

  • 就是说有一个地图,你需要从(0,0)(0,0)(0,0)走到(n−1,n−1)(n-1,n-1)(n−1,n−1),每次你可以向相邻的八个整点沿直线走,时间花费都是111,不能走到’#'中,而且走的路径不能到达给定的一个三角形中,但是可以沿着三角形的一条边走或者经过一个顶点,然后求最小的从(0,0)(0,0)(0,0)走到(n−1,n−1)(n-1,n-1)(n−1,n−1)的时间

题解

  • nnn很小,考虑暴力找出所有可行路径然后bfsbfsbfs
  • 重点以及难点就是判断一条路径s→es\rightarrow es→e是否与△ABC\triangle ABC△ABC在内部有公共点,步骤如下
    • 首先如果两个点sss和eee都在△ABC\triangle ABC△ABC内部的话(可以根据三个小三角形面积之和等于大三角形面积),进一步判断一下两个点是否都在三角形的某一条边上,如果是,则合法,否则不合法
    • 现在主要考虑枚举三角形的每一条边与s→es\rightarrow es→e求交,如果没有交点或者平行或者重合,继续枚举,否则剩下的情况就是不平行而且相交的情况,求出交点,如果交点intersectintersectintersect不等于任何一个线段端点,这时可以直接判定在三角形内部有公共点,那么显然不合法,否则的话如果交点等于三角形的一个顶点,那么也就是下图这种情况


      直接判断∠CBA\angle CBA∠CBA与∠CBe\angle CBe∠CBe大小即可判断是否相交

代码

#include<bits/stdc++.h>
using namespace std;
const double eps=1e-8;
const int maxn=50;
#define pi acos(-1.0)
int sgn(double k) {return k<-eps?-1:(k<eps?0:1);}
double Acos(double k) {return k>=1?0:(k<=-1?pi:acos(k));}
double Sqrt(double k) {return sgn(k)<=0?0:sqrt(k);}
char s[maxn][maxn];
bool vis[maxn][maxn];
bool ok[25][25][25][25];
int n,dir[8][2]={{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0}};struct point{double x,y;point(double a=0,double b=0) {x=a;y=b;}point operator+(point other) {return point(x+other.x,y+other.y);}point operator-(point other) {return point(x-other.x,y-other.y);}point operator*(double k) {return point(x*k,y*k);}friend bool operator==(const point &p1,const point &p2) {return sgn(p1.x-p2.x)==0&&sgn(p1.y-p2.y)==0;}double operator*(point other) {return x*other.x+y*other.y;}double operator^(point other) {return x*other.y-y*other.x;}friend double area(point a,point b,point c) {return fabs((b-a)^(c-a))/2;}friend int point_to_line(point p,point s,point e) {return -sgn((e-s)^(p-s));}friend double len(point p) {return Sqrt(p.x*p.x+p.y*p.y);}friend double angle(point p1,point p2) {if(point_to_line(p2,point(0,0),p1)<=0) return Acos((p1*p2)/len(p1)/len(p2));return 2*pi-Acos((p1*p2)/len(p1)/len(p2));}
};struct line{point s,e;line () {}line (point a,point b) {s=a;e=b;}friend point intersect(line l1,line l2) {double k=((l2.e-l2.s)^(l2.s-l1.s))/((l2.e-l2.s)^(l1.e-l1.s));return l1.s+(l1.e-l1.s)*k;}friend bool onseg(point p,line l) {return sgn((l.s-p)^(l.e-p))==0&&sgn((l.s-p)*(l.e-p))<=0;}friend int seg_to_seg(line l1,line l2) {if(sgn((l1.e-l1.s)^(l2.e-l2.s))==0) {if(onseg(l1.s,l2)||onseg(l1.e,l2)||onseg(l2.s,l1)||onseg(l2.e,l1)) return 1;return 0;}point inter=intersect(l1,l2);if(onseg(inter,l1)&&onseg(inter,l2)) return 3;return 2;}friend bool onright(point p,line l) {return ((l.e-l.s)^(p-l.s))<=0;}
};point p[3];
line l[3];
struct node{int x,y,step;};
bool valid(int x,int y) {return x>=0&&x<=n-1&&y>=0&&y<=n-1&&s[x][y]!='#'&&!vis[x][y];}
int bfs() {memset(vis,false,sizeof(vis));queue<node> que;if(s[0][0]=='#'||s[n-1][n-1]=='#') return -1;que.push(node{0,0,0});vis[0][0]=true;while(!que.empty()) {node cur=que.front();que.pop();for(int i=0;i<8;i++) {int nxtx=cur.x+dir[i][0],nxty=cur.y+dir[i][1];if(valid(nxtx,nxty)&&ok[cur.x][cur.y][nxtx][nxty]) {vis[nxtx][nxty]=true;que.push(node{nxtx,nxty,cur.step+1});if(nxtx==n-1&&nxty==n-1) return cur.step+1;}}}return -1;}bool point_in_triangle(point p,point a,point b,point c) {return sgn(area(p,a,b)+area(p,b,c)+area(p,c,a)-area(a,b,c))==0;
}bool zhongjian(point p,point a,point o,point b) {double k1=angle(b-o,p-o);double k2=angle(b-o,a-o);return sgn(k1)>0&&sgn(k1-k2)<0;
}bool check(int a,int b,int c,int d) {bool ok1=a>=0&&a<=n-1&&b>=0&&b<=n-1&&c>=0&&c<=n-1&&d>=0&&d<=n-1&&s[a][b]!='#'&&s[c][d]!='#';if(!ok1) return false;point p1=point(a,b),p2=point(c,d);if(point_in_triangle(p1,p[0],p[1],p[2])&&point_in_triangle(p2,p[0],p[1],p[2]))  {bool ok=false;for(int i=0;i<=2;i++) if(onseg(p1,l[i])&&onseg(p2,l[i])) {ok=true;break;}return ok;}for(int i=0;i<=2;i++) {int k=seg_to_seg(line(p1,p2),l[i]);if(k==3){point inter=intersect(line(p1,p2),l[i]);if(!(inter==p1||inter==p2||inter==l[i].s||inter==l[i].e)) return false;else if(inter==p[i]) {if(inter==p1||inter==p2) continue;if(zhongjian(p1,p[((i-1)+3)%3],p[i],p[(i+1)%3])||zhongjian(p2,p[((i-1)+3)%3],p[i],p[(i+1)%3])) return false;}}}return true;
}int main() {while(~scanf("%d",&n)) {memset(ok,false,sizeof(ok));for (int i = 0; i <= 2; i++) scanf("%lf %lf",&p[i].x,&p[i].y);if(onright(p[2],line(p[0],p[1]))) swap(p[1],p[2]);for(int i=0;i<=2;i++) l[i]=line(p[i],p[(i+1)%3]);for(int j=n-1;j>=0;j--) for(int i=0;i<=n-1;i++) scanf(" %c",&s[i][j]);for(int i=0;i<=n-1;i++) {for(int j=0;j<=n-1;j++) {for (int k = 0; k < 8; k++) {int nxtx =i+dir[k][0],nxty=j+dir[k][1];if(check(i,j,nxtx,nxty)) {ok[i][j][nxtx][nxty]=ok[nxtx][nxty][i][j]=true;}}}}printf("%d\n",bfs());}
}

「2017ACM/ICPC亚洲区域赛北京站现场赛G」Liaoning Ship's Voyage【计算几何】相关推荐

  1. 2014 ACM亚洲区域赛 - 北京现场赛

    2014 ACM亚洲区域赛 - 北京现场赛A - A Curious Matt一个排序,找最大值,overB - Black And White数据范围很小,单纯的暴力会TLE,看题解,染色问题有一个 ...

  2. 2020年 ICPC 亚洲区域赛(上海)G-Fibonacci

    ICPC 亚洲区域赛(上海) G-Fibonacci 题目 斐波那契数列为1,1,2,3,5,8,13,21,- 可以看到,这个数列有以下特点: 奇,奇,偶,奇,奇,偶- 当 xxx 与 yyy 相乘 ...

  3. 2018年 ACM/ICPC亚洲区域赛 青岛赛区现场赛 比赛总结

    首先祝贺自己收获了ACM生涯中的第二枚铜牌. 首先吐槽一下中石油: 周六早上来到中国石油大学,连个志愿者小姐姐都没看到.(但是看到了女装大佬).报完到之后发现教练少了一张午餐券(要不要这么粗心).为了 ...

  4. 2014ACM/ICPC亚洲区域赛牡丹江现场赛总结

    不知道怎样说起-- 感觉还没那个比赛的感觉呢?如今就结束了. 9号.10号的时候学校还评比国奖.励志奖啥的,由于要来比赛,所以那些事情队友的国奖不能答辩.自己的励志奖班里乱搞要投票,自己又不在,真是无 ...

  5. 2020年安徽省大数据网络赛与现场赛hive习题

    目录 第一章 2020年安徽省大数据比赛hive习题 (网络赛) 第二章 2020年安徽省大数据比赛hive习题 (现场赛) 前言 主要记录hive习题 一.2020年安徽省大数据比赛hive习题(网 ...

  6. 2018ACM/ICPC亚洲区域赛(焦作)F. Honeycomb

    目录 F. Honeycomb (2018-ACM/ICPC焦作) F. Honeycomb (2018-ACM/ICPC焦作) Problem F. Honeycomb Input file: st ...

  7. 第43届ACM icpc亚洲区域赛焦作站感想

    青岛痛失银牌,焦作又是铜牌一枚,现在想想,前面三个水题,我的原因太大了,老是犯各种zz小错误,明明能秒,却花了大量时间debug,导致三个小时才签完到,最后一个小时,F题的bfs没出来,B题也没出来, ...

  8. 2021 ICPC 昆明(22-4-17) C L E | 第46届ICPC亚洲区域赛(昆明)

    ICPC 2021 昆明 传送门 补题计划 CLE, C - Cup of Water prob : 在0-V内随机取数灌满1升水的期望操作次数 idea1: 首先将题给的"在0-V内随机取 ...

  9. 2022 ICPC 亚洲区域赛(杭州)赛后总结

    寄!我愿称之为最抽象的一站 20分钟连过两个签到,状态起飞,然后全程坐牢未过一题~~ 不过这次并没有什么遗憾,因为确实够不到铜牌的边缘,看到同校队伍济南站银牌,这一场打铁,我们也就释然了 只能说以后的 ...

  10. 第46届ICPC亚洲区域赛(沈阳)L-Perfect Matchings【dp,组合数学】

    正题 题目链接:https://ac.nowcoder.com/acm/contest/24346/L 题目大意 有一张2n2n2n个点的完全图,在上面删除一棵生成树,然后求这张图的完全匹配方案数. ...

最新文章

  1. python回归建模_Python实现回归预测及模型优化
  2. Cache系列:spring-cache简单三步快速应用ehcache3.x-jcache缓存(spring4.x)
  3. STM32 基础系列教程 50 – MbedTls
  4. windows下 sqlplus / as sysdba 报ora-12560的终极解决方法
  5. Java自动化邮件中发送图表(一)
  6. 海洋大数据关键技术及在灾害天气下船舶行为预测上的应用
  7. java使用POST发送soap报文请求webservice返回500错误解析
  8. Rpm另类用法加固Linux安全
  9. python聊天室详细教程_Python基础教程书籍案例:在线聊天室(虚拟茶话会)【下】...
  10. PyTorch搭建GoogLeNet模型(在CIFAR10数据集上准确率达到了85%)
  11. 分析微信小程序生成二维码接口报错41030: invalid page hint
  12. 微信聊天记录生成词云图
  13. SAP 采购订单入库——库存查询
  14. html头像生成器,Personas – 免费的卡通风格头像生成器
  15. [wirteup] unctf-web-can_you_hacked_me
  16. 压缩文件服务器返回出错,解压缩多部分文件 - 错误的zipfile偏移量(本地头信号):4...
  17. 谷歌浏览器驱动_谷歌驱动的配置与测试
  18. 脑电EEG的几种蒙太奇
  19. 数据堂亮相GMIC2015全球移动互联网大会
  20. java毕业生设计园艺生活网站计算机源码+系统+mysql+调试部署+lw

热门文章

  1. 迅为4418开发板-驱动-以模块的方式编译内核驱动
  2. ruby与ruby on rails环境部署
  3. 使用Ruby搭建Redis 集群
  4. 关于apapche aurora rpm包
  5. 利用注册表更改文件默认打开方式及图标
  6. android获取Bitmap对象,获取图片宽高
  7. [经验]零线和地线的区别,不注意可是要出大事的哦!
  8. 这片“农场”被他们承包了|第一届中国农业人工智能创新创业大赛初赛回顾
  9. EndNote 使用教程
  10. Java+spring+springmvc 基于ssm的超市进销存管理系统#毕业设计