大牛们,帮忙看一下poj1101,貌似连连看的一个题目!
题目的描述太多了,发个网址上来吧http://像连连看一样,求的是两个点之间用了几个线段~!最短的!
程序代码:#include <iostream>
#include <memory.h>
#include <cstdio>
#include <queue>
using namespace std;
int w,h;
int x1,y1,x2,y2;
char str[80][80];
int vis[80][80],cnt;
int x[4] = {1, -1, 0, 0};
int y[4] = {0, 0, 1, -1};
queue<struct Node> q;
struct Node
{
int x;
int y;
};
struct Node tmp,top;
int main()
{
while(cin>>w>>h,w&&h)
{
cnt=1;
getchar();
for(int i = 1; i <= h; i++)
gets(&str[i][1]);
for(int j=0; j<=w; j++)
str[0][j]=str[h+1][j]=' ';
for(int i=0; i<=h; i++)
str[i][0]=str[i][w+1]=' ';
while(cin>>y1>>x1>>y2>>x2)
{
if(x1==0&&x2==0&&y1==0&&y2==0)
break;
for(int i=0; i<=h+1; i++)
{
memset(str[i],0,sizeof(str[i]));
memset(vis[i],-1,sizeof(vis[i]));
}
tmp.x=x1;
tmp.y=y1;
q.push(tmp);
vis[x1][y1]=0;
while(!q.empty())
{
top=q.front();
q.pop();
for(int i=0; i<4; i++)
{
tmp.x=top.x+x[i];
tmp.y=top.y+y[i];
while(str[tmp.x][tmp.y]==' '&&top.x+x[i]>=0&&top.x+x[i]<=h+1&&top.y+y[i]>=0&&top.y+y[i]<=w+1&&!vis[top.x+x[i]][top.y+y[i]])
{
q.push(tmp);
vis[tmp.x][tmp.y]=vis[top.x][top.y]+1;
tmp.x+=x[i];
tmp.y+=y[i];
}
}
}
if(vis[x2][y2]==-1)
printf("Pair %d: impossible.\n", cnt++);
else
printf("Pair %d: %d segments.\n", cnt++, vis[x2][y2]);
}
}
return 0;
}
这是我用BFS做的,可是不太对,希望大家指出错误之处,
先谢谢啦!






怎么木有人啊!


