poj--1101--The Game(bfs)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 9746 | Accepted: 2967 |
Description
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
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
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.
Source
#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
using namespace std;
int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}}; /* 右左上下 */
char map[77][77];
bool vis[77][77];
int x1,y1,x2,y2,w,h;
struct Node
{
int x,y,step,dir; /* x,y代表方向,step走了几步,dir = (0初始状态,1直走,2横走)*/
};
bool operator <(const Node &a,const Node &b)
{
return a.step > b.step;
}
int bfs()
{
memset(vis,0,sizeof(vis));
queue<Node>q;
Node s,e;
s.x = x1;
s.y = y1;
s.step = 0;
s.dir = 0;
vis[s.x][s.y] = 1;
q.push(s);
while(!q.empty())
{
s = q.front();
q.pop();
for(int i=0;i<4;i++)
{
e.x = s.x + dir[i][0];
e.y = s.y + dir[i][1];
if(e.x == s.x) e.dir = 2; /* 横 */
else e.dir = 1; /* 竖 */
if(e.x>=0&&e.x<=h+1&&e.y>=0&&e.y<=w+1&&!vis[e.x][e.y])
{ if(s.dir == 0) e.step = 1;
else
{
if(s.dir == e.dir) e.step = s.step;
else e.step = s.step + 1 ;
}
if(e.x == x2&&e.y==y2) return e.step;
if(map[e.x][e.y] ==' ')
{
vis[e.x][e.y] = 1;
q.push(e);
}
}
}
}
return false;
} int main()
{
int i,j,cnt=1,Step;
while(cin>>w>>h&&(w||h))
{
memset(map,' ',sizeof(map));
for(i=1;i<=h;i++)
{
getchar();
for(j=1;j<=w;j++)
map[i][j] = getchar();
}
printf("Board #%d:\n",cnt++);
int count = 1;
while(cin>>y1>>x1>>y2>>x2)
{
if(x1+y1+x2+y2==0) break;
printf("Pair %d: ",count++);
Step = bfs();
if(!Step)
printf("impossible.\n");
else printf("%d segments.\n",Step);
}
printf("\n");
}
return 0;
}
poj--1101--The Game(bfs)的更多相关文章
- POJ 1101 The Game(BFS+判方向)
The Game Description One morning, you wake up and think: "I am such a good programmer. Why ...
- POJ 3984 迷宫问题(BFS)
迷宫问题 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, ...
- POJ 2435Navigating the City(bfs)
题意:给你一个地图,’+’代表十字路口,‘-’‘|’表示街道,‘.’表示建筑物,‘s’,’E’ 起点和终点.输出从起点到终点的的 最短路径(包括方向和沿该方向的经过的十字路口数) 分析:ans[i][ ...
- poj 3669 Meteor Shower(bfs)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
- 【POJ - 3414】Pots(bfs)
Pots 直接上中文 Descriptions: 给你两个容器,分别能装下A升水和B升水,并且可以进行以下操作 FILL(i) 将第i个容器从水龙头里装满(1 ≤ i ≤ 2); DRO ...
- POJ 1573 Robot Motion(BFS)
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12856 Accepted: 6240 Des ...
- POJ 3126 Prime Path (BFS)
[题目链接]click here~~ [题目大意]给你n,m各自是素数,求由n到m变化的步骤数,规定每一步仅仅能改变个十百千一位的数,且变化得到的每个数也为素数 [解题思路]和poj 3278类似.b ...
- poj 2251 Dungeon Master(bfs)
Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...
- POJ 3221 Diamond Puzzle(BFS)
Description A diamond puzzle is played on a tessellated hexagon like the one shown in Figure 1 below ...
- 题解报告:poj 3669 Meteor Shower(bfs)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
随机推荐
- html——行内元素、块元素、行内块元素
行内元素:span ,a, ,strong , em, del, ins.特点:在一行上显示:不能直接设置宽高:元素的宽和高就是内容撑开的宽高. 块元素:div,h1-h6,p,ul,li.特 ...
- 【译】x86程序员手册14-5.1段转换
5.1 Segment Translation 段转换 Figure 5-2 shows in more detail how the processor converts a logical add ...
- RadioButtonList的兩種實現方式
一種是修改ItemTemplate,即ListBoxItem裏面的内容 <ListBox ItemsSource="{Binding}"> <ListBox.It ...
- http服务器与https服务器的区别
1.HTTPS服务器使用的是HTTPS协议,而HTTP使用的是HTTP协议. 2.HTTPS服务器需要向证书授权中心申请证书,一般免费证书很少,需要交费. 3.HTTP服务器与客户端传递的是明文数据, ...
- 【sqli-labs】 less56 GET -Challenge -Union -14 queries allowed -Variation3 (GET型 挑战 联合查询 只允许14次查询 变化3)
单引号括号闭合 http://192.168.136.128/sqli-labs-master/Less-56/?id=1')%23 http://192.168.136.128/sqli-labs- ...
- 通过PHP怎样取到android系统下apk应用的包名,版本号等信息
公司项目关系,要求在通过PHP解析android系统应用apk包内的一切可用的信息.比如说:APK包名,版本号,版本名,安装权限等一系列关于对应包的信息.通过google查找相关的解决方案,都没有找到 ...
- 如何在Centos里面,把.net core程序设为开机自启动
确定你的.net core程序可以在centos手动启动后,下一步,就是把这个程序做成一个服务,让它开机自自动了 1.创建脚本文件 到目录/etc/rc.d/init.d下面,创建一个myserver ...
- 戴尔14G服务器用H740P配置阵列
公司采购了几台dell r740机器.做阵列的方式跟之前ctrl+r有很大改动. 戴尔14G机器已经面世一段时间了,14G的机器使用过后都能发现器性能比上一代机器提升了很多,今天给大家带来戴尔14代服 ...
- 洛谷——P3906 Geodetic集合
P3906 Geodetic集合 题目描述 图G是一个无向连通图,没有自环,并且两点之间至多只有一条边.我们定义顶点v,u最短路径就是从v到u经过边最少的路径.所有包含在v-u的最短路径上的顶点被称为 ...
- 【POJ 1860】Currency Exchange
[题目链接]:http://poj.org/problem?id=1860 [题意] 给你n种货币,m种货币之间的交换信息; 交换信息以 A,B,RA,CA,RB,CB的形式给出; 即A换B的话假设A ...