Minimum number of steps Nyoj

Source: Internet
Author: User

1#include <cstdio>2#include <cstring>3#include <queue>4 using namespacestd;5 6 intd[9][9];//Mark whether this point accesses and records distances7 intmaze[9][9]={8     1,1,1,1,1,1,1,1,1,9     1,0,0,1,0,0,1,0,1,Ten     1,0,0,1,1,0,0,0,1, One     1,0,1,0,1,1,0,1,1, A     1,0,0,0,0,1,0,0,1, -     1,1,0,1,0,1,0,0,1, -     1,1,0,1,0,1,0,0,1, the     1,1,0,1,0,0,0,0,1, -     1,1,1,1,1,1,1,1,1}; - Const intinf=1<<Ten; -typedef pair<int,int>P; + intN,sx,sy,gx,gy; - intdx[4]={1,0,-1,0},dy[4]={0,1,0,-1};  +  A intBFS () at { -memset (D,0,sizeof(d)); -Queue<p>que; -      for(intI=0;i<9; i++) -          for(intj=0;j<9; j + +) -d[i][j]=INF; in Que.push (P (Sx,sy)); -d[sx][sy]=0; to      +      while(!que.empty ()) -     { theP p=Que.front (); * Que.pop (); $         if(p.first==gx&&p.second==gy)Panax Notoginseng              Break; -          the          for(intI=0;i<4; i++) +         { A             intNx=p.first+dx[i],ny=p.second+Dy[i]; the              +             if(0<=nx&&nx<=8&&0<=ny&&ny<=8&&maze[nx][ny]==0&&d[nx][ny]==inf)//whether the current position can be joined -             { $ Que.push (P (Nx,ny)); $d[nx][ny]=d[p.first][p.second]+1; -             } -         } the     } -     returnD[gx][gy];Wuyi } the  - intMain () Wu { -scanf"%d",&n); About      while(n--) $     { -scanf"%d%d%d%d",&sx,&sy,&gx,&gy); -printf"%d\n", BFS ()); -     } A     return 0; +}
View Code

Challenge Programming Maze Shortest Path template

Minimum number of steps Nyoj

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.