标签:搜索
#include<stdio.h> #include<string.h> #include<queue> #include<iostream> using namespace std; struct node { int x,y; int step; bool operator < (const node& p) const { return step>p.step; } }a,b; int mark[1010][1010]; char map[1010][1010]; int n,m; int dir[8][2]={-1,0, -1,1, 0,1, 1,1, 1,0, 1,-1, 0,-1, -1,-1}; int leap[8]={0,-1,-2,-3,-4,-5,-6,-7}; int bfs(int starx,int stary,int endx,int endy) { int i,j; memset(mark,-1,sizeof(mark)); priority_queue<node>q; a.x=starx; a.y=stary; a.step=0; mark[a.x][a.y]=1; q.push(a); while(!q.empty()) { b=q.top(); q.pop(); if(b.x==endx&&b.y==endy) { printf("%d\n",b.step); return 0; } for(int i=0;i<8;i++) { a.x=b.x+dir[i][0]; a.y=b.y+dir[i][1]; if(a.x<0||a.x>=n||a.y<0||a.y>=m) continue; if(leap[map[b.x][b.y]-‘0‘]+i==0) a.step=b.step; else a.step=b.step+1; if(mark[a.x][a.y]==-1||mark[a.x][a.y]>a.step) { mark[a.x][a.y]=a.step; q.push(a); } } } return 0; } int main() { int i,j; while(~scanf("%d%d",&n,&m)) { for(i=0;i<n;i++) scanf("%s",map[i]); int Q; scanf("%d",&Q); int starx,stary,endx,endy; while(Q--) { scanf("%d%d%d%d",&starx,&stary,&endx,&endy); starx--; stary--; endx--; endy--; bfs(starx,stary,endx,endy); } } return 0; }
标签:搜索
原文地址:http://blog.csdn.net/zxf654073270/article/details/42804697