"Algorithmic Learning note" 63. BFS SJTU OJ 1281 Bounce

Source: Internet
Author: User

Typical BFS, find the starting point to search directly. It is important to note that the processing of layers. Coordinates-to-ID conversions. There is also the early judgment on whether to reach the end.

The code comment is very detailed, the last two functions are written at the beginning with the extraction adjacency matrix +dijkstra to calculate, very troublesome mind a hot result.

#include <vector>#include<queue>#include<iostream>using namespacestd;intmap[ to][ to]={0};intm,n,m1,m2;structpoint{intx; inty; intLay//The number of layers is used to calculate results}; Point S;//starting pointPoint e;//Endintgraph[ to* to][ to* to]={0};inlineintGetID (intXinty) {    if(X<=m and x>=1and y>=1and y<=N) {        if(map[x][y]!=0and map[x][y]!=2)//not water or swamp to return .            return(X-1) * N +y; }    return-1;} InlinevoidGetPoint (intIdint& X,int&y) {y= ID%N; if(y==0) y=N; X= ID/N;}voidinit () {cin>>M>>N>>M1>>M2;  for(inti =1; I <= M; ++i) { for(intj =1; J <= N; ++j) {cin>>Map[i][j]; if(map[i][j]==3) {s.x=i; S.y=J; }Else if(map[i][j]==4) {e.x=i; E.y=J; }        }    }}intBFs () {//Eight Directions    intdx[8] = {m1,m1,-m1,-m1,m2,m2,-m2,-M2}; intdy[8] = {m2,-m2,m2,-m2,m1,-m1,m1,-M1}; Queue<Point> Q;//the BFS queueS.lay =0;//indicates the number of layersQ.push (s); BOOLvis[ to* to] = {0}; intres=0;  while(!Q.empty ()) {Point cur=Q.front (); if(cur.x = = e.x and cur.y = = e.y)//In fact, this step is not necessary. Legacy of early Code             Break;        Q.pop (); Res=Cur.lay; Vis[getid (CUR.X,CUR.Y)]=true;//Access state Changes         for(inti =0; I <8; ++i) {intx = cur.x +Dx[i]; inty = cur.y +Dy[i]; intNEWID =GetID (x, y); if(newid!=-1and!Vis[newid])                {point P; P.x=x; P.Y=y; P.lay= Cur.lay +1; if(X==e.x and Y==e.y)//We found the end.                    returnP.lay;                Q.push (P); VIS[NEWID]=true;//Access state Changes            }        }    }        returnRes;}intMainintargcChar Const*argv[]) {        //Shortest Path Probleminit (); //Another method is to extract the adjacency matrix and then use the shortest path D algorithm//Buildadjmap (); //Cout<<dijkstra () <<endl;cout&LT;&LT;BFS () <<Endl; return 0;}voidBuildadjmap () {//Constructing adjacency matrices    intdx[8] = {m1,m1,-m1,-m1,m2,m2,-m2,-M2}; intdy[8] = {m2,-m2,m2,-m2,m1,-m1,m1,-M1};  for(inti =1; I <= M; ++i) {         for(intj=1; J <= N; ++j) {            intx, y; intFromid =GetID (I,J);  for(intK =0; K <8; ++K) {//Traverse 8 Directionsx = i +Dx[k]; Y= j +Dy[k]; intEndid =GetID (x, y); if(Endid! =-1) {Graph[fromid][endid]=1; Graph[endid][fromid]=1;//graph without Direction                }            }        }    }    }intDijkstra () {//Shortest Path algorithm It's too much trouble to write, it's mostly about trying to make    intres =0; intCNT = m*n;//total number of points    intBegin = GetID (s.x, S.Y);//starting point    intEnd = GetID (e.x, e.y);//End    Const intINF =9999999; intd[ to* to];//Storing temporary distances     for(inti =1; I <= CNT; ++i) d[i]= Graph[begin][i] >0?Graph[begin][i]: INF; D[begin]=0; ints[ to* to],q[ to* to];//doesn't Matter        intp_s=0, p_q=0, len_s =1, len_q = cnt-1 ; s[p_s++] =begin;  for(inti =1; I <= CNT; ++i)if(I! =begin) {Q[p_q++] =i; }         while(Len_q >1){        intu=0, mind =INF; intqid =0;  for(inti =0; i < p_q; ++i)if(q[i]!=0)        {            if(d[q[i]]<Mind ) {u=Q[i]; Mind=D[u]; QID=i; }        }        if(U = =0or U = =end) {             Break; } Q[qid]=0; len_q--; s[p_s+ +] = u; len_s++;  for(inti =0; i < p_q; ++i)if(q[i]!=0)        {            intv =Q[i]; if(Graph[u][v] >0){                if(D[v] > d[u]+Graph[u][v]) D[V]= D[u]+graph[u][v];//Update            }        }            }            returnd[end];}/*4 5 1 2 1 0 1 0 1 3 0 2 0 4 0 1 2 0 0 0 0 0 1 0 1-1 3-1 5 6-1 -1-1 10-1 12-1 -1-1 -1-1-1 19-1 */

"Algorithmic Learning note" 63. BFS SJTU OJ 1281 Bounce

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.