ZOJ 1148

来源:互联网 发布:张卜天的翻译水平知乎 编辑:程序博客网 时间:2024/05/29 03:32
这道题目要求最少多少线段,广搜的过程中我们将处于同一直线上的步数设为同样的值(因为他们可以连成一条线,也就是只有一条线段),我们搜索时候,在同一个方向上一直往前走
#include<iostream>#include<queue>#include<memory.h>#include<stdio.h>using namespace std;char ch;int n,m,sx,sy,dx,dy;int mat[80][80];int d[80][80];int dir[4][2]={{0,1},{-1,0},{0,-1},{1,0}};struct Node{int x,y;};queue<Node> q;Node nd[6400];int main(){int board=1;while(cin>>n>>m){if(m==0&&n==0)break;cout<<"Board #"<<board++<<":"<<endl;getchar();//保证了拓宽的边界为0memset(mat,0,sizeof(mat));for(int i=1;i<=m;i++){for(int j=1;j<=n;j++){scanf("%c",&ch);if(ch=='X')mat[i][j]=1;}getchar();}int pair=1;while(cin>>sy>>sx>>dy>>dx){if(sx==0&&sy==0&&dx==0&&dy==0)break;while(!q.empty())    q.pop();memset(d,-1,sizeof(d));mat[sx][sy]=mat[dx][dy]=0;d[sx][sy]=0;Node st;st.x=sx,st.y=sy;q.push(st);while(!q.empty()){Node tmp=q.front();q.pop();for(int i=0;i<4;i++){ int a=tmp.x+dir[i][0]; int b=tmp.y+dir[i][1]; while(a>=0&&a<=m+1&&b>=0&&b<=n+1&&mat[a][b]==0&&d[a][b]==-1){st.x=a;st.y=b;q.push(st);d[a][b]=d[tmp.x][tmp.y]+1;a+=dir[i][0];b+=dir[i][1]; }}}//end whilemat[sx][sy]=mat[dx][dy]=1;if(d[dx][dy]==-1)cout<<"Pair "<<pair++<<": "<<"impossible."<<endl;elsecout<<"Pair "<<pair++<<": "<<d[dx][dy]<<" segments."<<endl;}cout<<endl;//忘记了}return 0;}