Bzoj 1967 Ahoi2005 Cross through magnetic field FLOODFILL+BFS

Source: Internet
Author: User

Question: N squares on a given plane, to find at least the number of boundaries through which one point goes to another

At first, I want to determine for each square whether the start and end points are on the same side = = But the inverse is obviously

Consider the n<=100, which can be discretized and then labeled with FloodFill for each block of area

Then run the shortest way. Because the edge is 1, so you can use BFS to find the shortest possible.

Even the edge of the company hung up half a night.

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 310using namespace Std;struct abcd{int x,y,c;friend istream& operator >> (IStream &_,abcd &a) {scanf ("%d%d%d", & AMP;A.X,&AMP;A.Y,&AMP;A.C); return _;}} A[m];struct rectangle{int X1,x2,y1,y2;} Rectangles[m];struct edge{int To,next;} Table[500500];int head[m*m],tot;int m=1,n=1,cnt,t;int xa,ya,xb,yb,map[m][m],f[100100];p air<int,int*> B[M]; BOOL map1[m][m],map2[m][m];void Add (int x,int y) {table[++tot].to=y;table[tot].next=head[x];head[x]=tot;} void FloodFill (int x,int y) {int xx,yy;map[x][y]=t;xx=x,yy=y-1;if (!map1[x][y]) {if (y!=1 &&!map[xx][yy]) FloodFill (XX,YY);} else if (Map[xx][yy]) Add (T,map[xx][yy]), add (map[xx][yy],t), Xx=x,yy=y+1;if (!map1[x][y+1]) {if (Y!=n &&!map[ XX][YY]) FloodFill (XX,YY);} else if (Map[xx][yy]) Add (T,map[xx][yy]), add (map[xx][yy],t), Xx=x-1,yy=y;if (!map2[x][y]) {if (x!=1 &&!map[xx] [YY]) FloodFill (XX,YY);} else if (map[xx][yy]) Add (T,map[xx][yy]), add (map[xx][yy],t), Xx=x+1,yy=y;if (!map2[x+1][y]) {if (x!=m &&!map[xx][yy]) FloodFill ( XX,YY);} else if (Map[xx][yy]) Add (T,map[xx][yy]), add (map[xx][yy],t);} void BFS () {static int q[65540];static unsigned short r,h;int i;f[q[++r]=map[xa][ya]]=1;while (r!=h) {int x=q[++h];for (i= Head[x];i;i=table[i].next) if (!f[table[i].to]) f[q[++r]=table[i].to]=f[x]+1;}} int main () {int i,j;cin>>cnt;for (i=1;i<=cnt;i++) cin>>a[i];cin>>xa>>ya>>xb> >yb;for (i=1;i<=cnt;i++) {B[i+i-1]=make_pair (a[i].x,&rectangles[i].x1); B[i<<1]=make_pair (a[i].x+ A[I].C,&AMP;RECTANGLES[I].X2);} B[cnt<<1|1]=make_pair (XA,&AMP;XA); B[cnt+1<<1]=make_pair (XB,&AMP;XB); sort (b+1,b+ (cnt+1<<1) +1) ; for (i=1;i<= (cnt+1<<1); i++) {if (i==1| | B[i].first!=b[i-1].first) ++m;*b[i].second=m;} M+=2;for (i=1;i<=cnt;i++) {B[i+i-1]=make_pair (a[i].y,&rectangles[i].y1); B[i<<1]=make_pair (a[i].y+a[ I].C,&AMP;RECTANGLES[I].Y2);} B[cnt<<1|1]=make_pair (YA,&AMp;ya); B[cnt+1<<1]=make_pair (YB,&AMP;YB); sort (b+1,b+ (cnt+1<<1) +1); for (i=1;i<= (cnt+1<<1); I + +) {if (i==1| | B[i].first!=b[i-1].first) ++n;*b[i].second=n;} N+=2;for (i=1;i<=cnt;i++) {for (j=rectangles[i].x1;j<rectangles[i].x2;j++) map1[j][rectangles[i].y1]=map1[j] [Rectangles[i].y2]=true;for (j=rectangles[i].y1;j<rectangles[i].y2;j++) map2[rectangles[i].x1][j]=map2[ Rectangles[i].x2][j]=true;} for (i=1;i<=m;i++) for (j=1;j<=n;j++) if (!map[i][j]) ++t,floodfill (I,J); BFS (); Cout<<f[map[xb][yb]]-1<<endl;return 0;}


Bzoj 1967 Ahoi2005 Cross through magnetic field FLOODFILL+BFS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.