标签:
6 6 ..X... XXX.X. ....X. X..... X..... X.X... 3 5 6 3 0 0
3
#include <iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; int n,m,i,j,sx,sy,tx,ty; char mp[1005][1005]; bool vis[1005][1005]; //int爆内存,用bool型 struct node { int x,y,ti; node(int a,int b,int c){x=a;y=b;ti=c;} }; int dr[4][2]={{1,0},{-1,0},{0,1},{0,-1} }; struct cmp { bool operator()(node a,node b) { return a.ti>b.ti; } }; bool check(int x,int y) { if(x>=0 && x<n && y>=0 && y<m && !vis[x][y]) return 1; return 0; } int bfs() { priority_queue<node,vector<node>,cmp> Q; Q.push(node(sx,sy,0)); vis[sx][sy]=1; while(!Q.empty()) { node p=Q.top(); Q.pop(); for(int i=0;i<4;i++) { int xx=p.x+dr[i][0]; int yy=p.y+dr[i][1]; if(!check(xx,yy)) continue; vis[xx][yy]=1; int ti=0; if (mp[xx][yy]==‘X‘) ti=p.ti; else ti=p.ti+1; Q.push(node(xx,yy,ti)); if (xx==tx && yy==ty) return ti; } } } int main() { while(scanf("%d%d",&n,&m)) { if(n==0 && m==0) break;//以0 0 结束 for(i=0;i<n;i++) scanf("%s",&mp[i]); scanf("%d%d",&sx,&sy); scanf("%d%d",&tx,&ty); sx--; sy--; tx--; ty--; memset(vis,0,sizeof(vis)); if (sx==tx && sy==ty) printf("0\n"); else printf("%d\n",bfs()); } return 0; }
标签:
原文地址:http://www.cnblogs.com/stepping/p/5679290.html