joj1113


 1113: The Game


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
3s 8192K 380 97 Standard
One morning, you wake up and think: ``I am such a good programmer. Why not make some money?'' So you decide to write a computer game.

The game takes place on a rectangular board consisting of w * h squares. Each square might or might not contain a game piece, as shown in the picture.

One important aspect of the game is whether two game pieces can be connected by a path which satisfies the two following properties:

It consists of straight segments, each one being either horizontal or vertical. 

It does not cross any other game pieces. 

(It is allowed that the path leaves the board temporarily.)

Here is an example:

The game pieces at (1,3) and at (4, 4) can be connected. The game pieces at (2, 3) and (3, 4) cannot be connected; each path would cross at least one other game piece.

The part of the game you have to write now is the one testing whether two game pieces can be connected according to the rules above.

Input

The input contains descriptions of several different game situations. The first line of each description contains two integers w and h (1 <= w,h <= 75), the width and the height of the board. The next h lines describe the contents of the board; each of these lines contains exactly w characters: a ``X'' if there is a game piece at this location, and a space if there is no game piece.

Each description is followed by several lines containing four integers x1, y1, x2, y2 each satisfying 1 <= x1,x2 <= w, 1 <= y1,y2 <= h. These are the coordinates of two game pieces. (The upper left corner has the coordinates (1, 1).) These two game pieces will always be different. The list of pairs of game pieces for a board will be terminated by a line containing ``0 0 0 0".

The entire input is terminated by a test case starting with w=h=0. This test case should not be procesed.

Output

For each board, output the line ``Board #n:'', where n is the number of the board. Then, output one line for each pair of game pieces associated with the board description. Each of these lines has to start with ``Pair m: '', where m is the number of the pair (starting the count with 1 for each board). Follow this by ``ksegments.'', where k is the minimum number of segments for a path connecting the two game pieces, or ``impossible.'', if it is not possible to connect the two game pieces as described above.

Output a blank line after each board.

Sample Input

5 4
XXXXX
X   X
XXX X
 XXX
2 3 5 3
1 3 4 4
2 3 3 4
0 0 0 0
0 0

Sample Output

Board #1:
Pair 1: 4 segments.
Pair 2: 3 segments.
Pair 3: impossible.


This problem is used for contest: 149 


Submit / Problem List / Status / Discuss





这道题跟2558差不多都是一种特殊的队列应用。。
我感觉这道题的难点在于如何输出。我是首先在主函数中
首先对一些特殊情况进行输出从而减轻bfs的压力。
然后在bfs里面 有两个输出函数分别应对不同的情况。
第一个函数是为了能够在一个直通路上对通路两侧的点
进行分析判断有没有目标点
第二个函数目的在于对通路的尽头那个点进行分析判断是不是目标点。。
(我感觉我没有说清楚如果那个地方不明白可以给我留言。)




#include<cstdio>
#include<cstring>
#include<queue>
#include<iostream>
using namespace std;
char map[100][100];
int h,w;
int visited[100][100];
bool success;
class Node
{
public:
int x,y;
int step;
};
bool ok(int x,int y,Node node)
{
//up
if(x-1==node.x&&y==node.y&&map[node.x][node.y]=='X')return true;
//down
if(x+1==node.x&&y==node.y&&map[node.x][node.y]=='X')return true;
//left
if(x==node.x&&y-1==node.y&&map[node.x][node.y]=='X')return true;
//right
if(x==node.x&&y+1==node.y&&map[node.x][node.y]=='X')return true;
return false;
}
int move[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
void bfs(Node node1,Node node2)
{
node1.step=0;
queue<Node>q;
q.push(node1);
visited[node1.x][node1.y]=1;
while(!q.empty())
{
Node temp;
temp=q.front();
q.pop();
if(ok(temp.x,temp.y,node2))
{
printf("%d segments.\n",temp.step+1);
success=false;
return ;
}
node1=temp;
node1.step++;
int x,y;
for(int i=0;i<4;i++)
{
temp=node1;
while(1)
{
x=temp.x+move[i][0];
y=temp.y+move[i][1];
if(x>=0&&x<=h+1&&y>=0&&y<=w+1&&!visited[x][y]&&map[x][y]!='X')
{
visited[x][y]=1;
temp.x=x;
temp.y=y;
temp.step=node1.step;
q.push(temp);
if(x+move[i][0]==node2.x&&y+move[i][1]==node2.y
   &&map[node2.x][node2.y]=='X')
{
printf("%d segments.\n",temp.step);
success=false;
return ;
}//对那一些
}
else break;
};
}
}
}
int main()
{
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
int n=1;
Node node1,node2;
while(scanf("%d%d",&w,&h),w&&h)
{
memset(map,' ',sizeof(map));
getchar();
for(int i=1;i<=h;i++)
{
for(int j=1;j<=w;j++)
{
map[i][j]=getchar();
}
getchar();
}
printf("Board #%d:\n",n++);
int m=1;
while(1)
{
memset(visited,0,sizeof(visited));
success=true;
scanf("%d%d%d%d",&node1.y,&node1.x,&node2.y,&node2.x);
if(node1.x==0&&node1.y==0&&node2.x==0&&node2.y==0)
break;
printf("Pair %d: ",m++);
if(node1.x==node2.x&&node1.y==node2.y&&map[node1.x][node1.y]=='X')
{printf("0 segments.\n");success=false;}
else if(map[node1.x][node1.y]!='X'||map[node2.x][node2.y]!='X')
{
printf("impossible.\n");
success=false;
}//首先我需要对一些简单情况进行分析从而能够在bfs减少负担
else bfs(node1,node2);
if(success)printf("impossible.\n");
}
printf("\n");
}
return 0;
}
已经博主授权,源码转载自 https://pan.quark.cn/s/a4b39357ea24 常见问题解答 网页打开速度慢或者打不开网页? 受到多种因素的影响,对于非会员用户我们无法提供最优质的服务。 如果您希望得到最棒的体验,请至大会员页面("右上角菜单 → 大会员")根据说明操作。 请注意:受制于国际网络的诸多不确定性,我们无法对任何服务的可靠性做出任何保证。 如果出现了网络连接相关的问题,我们建议您先等待一段时间,之后再重试。 如果您在重试后发现问题仍然存在,请联系我们,并说明网络问题持续的时间。 图片下载后无法找到? 打开"右上角菜单 → 更多 → 修改下载路径",在弹出的对话框中可以看到当前图片的保存路径。 此外,由于网络因素,在保存图片之后,等待屏幕下方出现"已保存到..."后,才能在本地找到图片。 如何更改图片保存的目录? 请参见"右上角菜单 → 更多 → 修改下载路径"。 翻页不方便? 在点进某个图片后,通过在图片上向左或向右滑动,即可翻页查看下一个作品。 如何保存原图/导出动图? 长按图片/动图,在弹出的菜单中选择保存/导出即可。 输入账号密码后出现"进行人机身份验证"? 此为pixiv登陆时的验证码,请按照要求点击方框或图片。 在pxvr中注册pixiv账号后,收到验证邮件,无法访问邮件中的验证链接? 请复制邮件中的链接,打开pxvr中的"右上角菜单 → 输入地址"进行访问。 能否自动将页面内容翻译为汉语? 很抱歉,pxvr暂不提供语言翻译服务。 图片下载类型是否可以选择? 能否批量下载/批量管理下载? 已支持批量下载多图作品中的所有原图:找到一个多图作品,进入详情页面后,点击图片进入多图浏览模式,长按任意一张图片即可看到批量下载选项。 关于上述其他功能,我们...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值