POJ 1101 The Game(BFS+判方向)
The Game
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. (PE不知不觉)
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.
题目大意:类似于一个连连看游戏吧。求最小线段数。如例图。线段数。一个4,一个3。
思路:我想那种求图,两点最小步数的题,大家都做过。
可是,这个怎么办了。仅仅有加一个 dire记录方向就可以。假设,next.dire!=cur.dire.则线段数+1.
代码例如以下:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cctype>
#include<queue>
#include<stack>
#define INF 0x3f3f3f3f
#define maxn 100001 using namespace std; int n,m;
char map[80][80];
int go[4][2]={{-1,0},{0,1},{1,0},{0,-1}};// 0 1 2 3
int vis[80][80];
int sx,sy,ex,ey;
struct node{
int x,y;
int seg;
int dire;
node(){}
node(int a,int b,int c,int d)
{
x=a;
y=b;
seg=c;
dire=d;
}
}; int bfs()
{
queue<node> que;
que.push(node(sx,sy,0,-1));
memset(vis,0,sizeof vis);
vis[sx][sy]=1;
while(!que.empty())
{
node cur=que.front();
node next;
que.pop();
for(int i=0;i<4;i++)
{
next.x=cur.x+go[i][0];
next.y=cur.y+go[i][1];
next.dire=i;
if(next.x>=0&&next.x<=n+1&&next.y>=0&&next.y<=m+1&&!vis[next.x][next.y])
{
if(next.dire==cur.dire)
next.seg=cur.seg;
else
next.seg=cur.seg+1;
if(next.x==ex&&next.y==ey)
return next.seg;
if(map[next.y][next.x]!='X')
{
//cout<<next.x<<next.y<<next.seg<<endl;
vis[next.x][next.y]=1;
que.push(next);
}
}
} } return 0;
} int main()
{
int t=0;
while(scanf("%d%d",&n,&m),n||m)
{
t++;
int cas=0;
memset(map,' ',sizeof map); for(int i=1;i<=m;i++)
{
getchar();
for(int j=1;j<=n;j++)
scanf("%c",&map[i][j]); }
//cout<<"15"<<map[4][2]<<endl; printf("Board #%d:\n",t);
while(1)
{
//cout<<"---->\n";
scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
//cout<<sx<<sy<<ex<<ey<<endl;
if(sx||sy||ex||ey)
{
int temp=bfs();
//cout<<"--------->>>>>>>>\n";
if(temp)
printf("Pair %d: %d segments.\n",++cas,temp);
else
printf("Pair %d: impossible.\n",++cas);
} else
break; }
printf("\n"); } return 0;
}
POJ 1101 The Game(BFS+判方向)的更多相关文章
- poj 3414 Pots 【BFS+记录路径 】
//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...
- poj 2049(二分+spfa判负环)
poj 2049(二分+spfa判负环) 给你一堆字符串,若字符串x的后两个字符和y的前两个字符相连,那么x可向y连边.问字符串环的平均最小值是多少.1 ≤ n ≤ 100000,有多组数据. 首先根 ...
- 最短路径问题,BFS,408方向,思路与实现分析
最短路径问题,BFS,408方向,思路与实现分析 继上回挖下的坑,不知道大家有没有认真看最小生成树呢?很简单,这回也讲讲正常难度的,看不懂就来这里看看,讲的很好~~ 最短路径问题 说起这个问题,先说个 ...
- poj 1465 Multiple(bfs+余数判重)
题意:给出m个数字,要求组合成能够被n整除的最小十进制数. 分析:用到了余数判重,在这里我详细的解释了.其它就没有什么了. #include<cstdio> #include<cma ...
- POJ 1101 简单BFS+题意
The Game 题意: Description One morning, you wake up and think: "I am such a good programmer. Why ...
- Poj 1166 The Clocks(bfs)
题目链接:http://poj.org/problem?id=1166 思路分析:题目要求求出一个最短的操作序列来使所有的clock为0,所以使用bfs: <1>被搜索结点的父子关系的组织 ...
- poj 1077 Eight(双向bfs)
题目链接:http://poj.org/problem?id=1077 思路分析:题目要求在找出最短的移动路径,使得从给定的状态到达最终状态. <1>搜索算法选择:由于需要找出最短的移动路 ...
- poj 3026 Borg Maze (bfs + 最小生成树)
链接:poj 3026 题意:y行x列的迷宫中,#代表阻隔墙(不可走).空格代表空位(可走).S代表搜索起点(可走),A代表目的地(可走),如今要从S出发,每次可上下左右移动一格到可走的地方.求到达全 ...
- poj 1324 状态压缩+bfs
http://poj.org/problem?id=1324 Holedox Moving Time Limit: 5000MS Memory Limit: 65536K Total Submis ...
随机推荐
- jquery中$.get()提交和$.post()提交有区别
jquery中$.get()提交和$.post()提交有区别吗? 相同点:都是异步请求的方式来获取服务端的数据: 异同点: 1.请求方式不同:$.get() 方法使用GET方法来进行异步请求的.$.p ...
- 各大IT企业招聘所须要求技能
1.中兴 ZTE 软件研发project师 工作地点:西安.深圳.上海.天津 主要职责: 1.从事通讯产品相关软件开发 2.进行软件具体设计,代码编写.单元測试.集成測试.系统測试等 3.进行软件代码 ...
- 线程池系列三:ThreadPoolExecutor讲解
三.一个用队列处理线程池例子 package demo; import java.util.Queue; import java.util.concurrent.ArrayBlockingQueue; ...
- thinkphp5内置标签
thinkphp5内置标签 知道内置标签怎么用,查手册的时候好查 却功能的时候在里面找着来用 内置标签一览 内置标签 变量输出使用普通标签就足够了,但是要完成其他的控制.循环和判断功能,就需要借助模板 ...
- vue --- axios发post请求后台接收不到参数的三种解决方案
最近用vue 做项目使用axios 发送post 请求时遇到了前端传数据后端接收不到的情况: 后来仔细对比发现axios传值是这样的: 而 ajax 传值是这样的: 一个 Request Paylo ...
- HDU 1512 左偏树+并查集
思路: 左偏树里面掺了一些并查集的应用 这里放一份左偏树的代码模板 重点就是merge函数了-- int merge(int k1,int k2){ if(!k1||!k2)return k1+k2; ...
- PHP写文件到指定位置
<?php $fp = fopen("output.json", "r+"); $flag = fseek($fp, -3, SEEK_END); if( ...
- Codefroces A. Saitama Destroys Hotel
A. Saitama Destroys Hotel time limit per test 1 second memory limit per test 256 megabytes input sta ...
- Ubunut PPA源概述
Ubuntu 自带的“软件”应用,可以安装海量软件,既包括发行者支持的软件.社区支持的软件,也包括专有驱动和版权软件.有时,我们需要的软件通过这些渠道仍然无法找到.这时,可以到 PPA 软件源中查找. ...
- 【js基础】判断是否是合法邮箱地址(正则表达式的应用)
2019-01-21 09:11:21 <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...