标签:路径 nes lin || style and memset algorithm number
1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #include<algorithm> 5 using namespace std; 6 7 struct node{ 8 int x,y,s; 9 bool operator < (const node &a) const { 10 return a.s<s;}; 11 }; 12 bool bk[210][210]; 13 char map[210][210]; 14 void bfs(int sx,int sy); 15 int n,m,ex,ey; 16 17 int main() 18 { 19 //freopen("E:\\testin.txt","r",stdin); 20 int sx,sy; 21 while(scanf("%d%d",&n,&m) != EOF){ 22 memset(map,0,sizeof(map)); 23 24 for(int i=1;i<=n;i++){ 25 for(int j=1;j<=m;j++){ 26 scanf(" %c",&map[i][j]); 27 if(map[i][j] == ‘r‘) 28 { 29 sx=i;sy=j; 30 } 31 } 32 } 33 34 bfs(sx,sy); 35 } 36 return 0; 37 } 38 39 void bfs(int sx,int sy){ 40 priority_queue<struct node> q; 41 struct node head,temp,temp1; 42 memset(bk,0,sizeof(bk)); 43 head.x=sx; 44 head.y=sy; 45 head.s=0; 46 int next[4][2]={-1,0,0,1,1,0,0,-1}; 47 q.push(head); 48 bk[sx][sy]=1; 49 int flag=0,tx,ty; 50 51 while(!q.empty()){ 52 temp=q.top();q.pop(); 53 54 for(int i=0;i<4;i++){ 55 tx=temp.x+next[i][0]; 56 ty=temp.y+next[i][1]; 57 58 if(tx < 1 || tx > n || ty < 1 || ty > m || map[tx][ty] == ‘#‘ || map[tx][ty] == ‘r‘) 59 continue; 60 if(bk[tx][ty] == 0){ 61 bk[tx][ty] = 1; 62 63 if(map[tx][ty] == ‘.‘){ 64 temp1.x=tx; 65 temp1.y=ty; 66 temp1.s=temp.s+1; 67 q.push(temp1); 68 } 69 if(map[tx][ty] == ‘x‘){ 70 temp1.x=tx; 71 temp1.y=ty; 72 temp1.s=temp.s+2; 73 q.push(temp1); 74 } 75 if(map[tx][ty] == ‘a‘){ 76 flag=1; 77 printf("%d\n",temp.s+1); 78 } 79 } 80 } 81 } 82 if(flag == 0) 83 printf("Poor ANGEL has to stay in the prison all his life.\n"); 84 }
标签:路径 nes lin || style and memset algorithm number
原文地址:https://www.cnblogs.com/wenzhixin/p/9319267.html