POJ 1101 简单BFS+题意
The Game
题意:
Description
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.
这个游戏是在一个长方形板上的,这个板由w*h个小正方形组成。每一个小正方形可能会包括一个游戏的块,如下图所示。
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.
1.路径是由直的部分组成,每个部分或水平或垂直。
It does not cross any other game pieces.
2.路径不能跨过其它的游戏方块。
(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.
(1,3)和(2,4)可以被连接。(2,3)和(3,4)不能被连接。
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.
输入是多种情况的,第一行包括两个整数w和h(q<=w,h<=75),即板的宽度和长度,下h行表示板上都有什么。每行包括w个字符,’X’表示有一个游戏方块,空格表示没有游戏方块。
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”.
每种情况后包括几行询问,每行包括4个整数X1,Y1,X2,Y2(1<=X1,X2<=w;I<=Y1,Y2<=h)。这是2个游戏片段的坐标。(左上角为坐标(1,1)。) 这两个坐标将永远是不同的。“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.
整个输入结束 是输入了(0,0) 这个case不用考虑。
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.
每个板,输出一行“Board #n:”,n是第n块板。然后输出一行为每对坐标是否相关联。如果关联,输出“Pair m: k segments.”m为这块板的第m次询问,k为路径包括几个部分。否则输出“Pair m: impossible.”
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.
思路: 一个搜直线的bfs 完了。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
char a[99][99];
int x,y,sx,sy,ex,ey,CNT=0,cnt;
int xxx[]={1,-1,0,0},yyy[]={0,0,1,-1},vis[99][99];
bool check(int xx,int yy)
{
if(xx<=x+1&&yy<=y+1&&xx>=0&&yy>=0&&a[xx][yy]!='X'&&!vis[xx][yy])return 1;
return 0;
}
int bfs(int xx,int yy)
{
queue <int> aa,bb;
aa.push(xx);bb.push(yy);
while(!aa.empty())
{
int tempa=aa.front(),tempb=bb.front();
aa.pop();bb.pop();
if(tempa==ex&&tempb==ey) return vis[tempa][tempb];
for(int i=0;i<=3;i++)
{
int qa=tempa,qb=tempb;
while(check(qa+xxx[i],qb+yyy[i]))
{
qa+=xxx[i];qb+=yyy[i];
vis[qa][qb]=vis[tempa][tempb]+1;
aa.push(qa);bb.push(qb);
}
}
}
return -1;
}
int main()
{
while(scanf("%d%d",&y,&x)&&x)
{
memset(a,0,sizeof(a));
CNT++;
cnt=0;
printf("Board #%d:\n",CNT);
for(int i=1;i<=x;i++)
{
for(int j=0;j<=y;j++)
scanf("%c",&a[i][j]);
}
while(scanf("%d%d%d%d",&sy,&sx,&ey,&ex)&&sx)
{
cnt++;
memset(vis,0,sizeof(vis));
a[ex][ey]=' ';
int temp=bfs(sx,sy);
if(~temp)
printf("Pair %d: %d segments.\n",cnt,temp);
else printf("Pair %d: impossible.\n",cnt);
a[ex][ey]='X';
}
printf("\n");
}
}
PE了一次,因为忘了每个case输出空行了,。。。
POJ 1101 简单BFS+题意的更多相关文章
- poj 3414(简单bfs)
题目链接:http://poj.org/problem?id=3414 思路:bfs简单应用,增对瓶A或者瓶B进行分析就可以了,一共6种状态. #include<iostream> #in ...
- poj 3278 简单BFS
题意:给定农夫和奶牛的初始位置,农夫可以当前位置+1.-1.*2三种移动方式,问最少需要多少分钟抓住奶牛 AC代码: #include<cstdio> #include<cstrin ...
- POJ 3669 简单BFS
标号 搜 完了-- //By SiriusRen #include <queue> #include <cstdio> #include <cstring> #in ...
- POJ3185(简单BFS,主要做测试使用)
没事做水了一道POJ的简单BFS的题目 这道题的数据范围是20,所以状态总数就是(1<<20) 第一次提交使用STL的queue,并且是在队首判断是否达到终点,达到终点就退出,超时:(其实 ...
- 【POJ 3669 Meteor Shower】简单BFS
流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封 ...
- LightOJ 1012 简单bfs,水
1.LightOJ 1012 Guilty Prince 简单bfs 2.总结:水 题意:迷宫,求有多少位置可去 #include<iostream> #include<cstr ...
- 逃脱 (简单BFS)
题目传送门 G逃脱 题目描述 这是mengxiang000和Tabris来到幼儿园的第四天,幼儿园老师在值班的时候突然发现幼儿园某处发生火灾,而且火势蔓延极快,老师在第一时间就发出了警报,位于幼儿园 ...
- POJ 3278 经典BFS
进一步了解了bfs; 题意:给你n,然后用+,-,*三种运算使n变成k; 漏洞:在算出新的数字之后,一定要判边界,否则RE,而且在每一步后面都得加判断是否等于K,如果是即刻退出,否则WA,判这个的时候 ...
- hdu1312 Red and Black 简单BFS
简单BFS模版题 不多说了..... 直接晒代码哦.... #include<cstdlib> #include<iostream> #include<cstdio> ...
随机推荐
- Jstorm可靠性分析
去掉storm可靠性有三种方式: 1.Config.TOPOLOGY_ACKERS 设置为0: 2.在发送数据时不带上mesage id: 3.将tuple不做anchor发送到下一个节点,因为没有a ...
- C# 多线程限制方法调用(monitor)
多线程执行方法 改方法没有执行完时 别的方法不能调用次方法.用循环执行一个方法可以需要一分钟 在这一分钟只内任何 成员都不能再调用该方法. class MonitorSample { ; //生产者和 ...
- uboot环境变量实现分析
u-boot的环境变量用来存储一些经常使用的参数变量,uboot希望将环境变量存储在静态存储器中(如nand nor eeprom mmc). 其中有一些也是大家经常使用,有一些是使用人员自己定义的, ...
- NET出现频率非常高的笔试题
又到了金三银四的跳槽季,许多朋友又开始跳槽了,这里我简单整理了一些出现频率比较高的.NET笔试题,希望对广大求职者有所帮助. 一..net基础 1. a=10,b=15,请在不使用第三方变量的情况下 ...
- as3 代码优化之pmd
首先下载com.abobe,ac.pmd.eclipse_...jar 和 flex-pmd-all-in-one...zip俩文件 前面一个装在xx\adobe flash builder xx\e ...
- STM32——DMA接收和发送的实现
最近写程序,需要一段一段数据的接收,再通过其他串口发送出去. 老司机们建议用DMA通信,以节约CPU资源.然后,我听了,发现挺好用的.特此,把自己写的代码贴上了. DMA发送接收的步骤如下: 1.初始 ...
- C++用法的学习心得(要求包含示例,并反映出利用网络获取帮助的过程)
大一一年C++的学习生涯,让我感慨颇多!回想起,当初上课时的情形,一切是那么的清晰,仿佛就像是发生在昨天一样. 任何一门学科的学习都是有技巧的.对于c++,我学的并不好,刚开 ...
- Maven 常用的命令
运行几个基本的Maven命令 mvn compile 编译主程序 mvn test-compile 编译测试程序 mvn clean 清理 mvn test 测试 mvn pac ...
- [SHELL]判断一个命令是否存在
首先要说明的是,不要使用which来进行判断,理由如下: 1.which非SHELL的内置命令,用起来比内置命令的开销大,并且非内置命令会依赖平台的实现,不同平台的实现可能不同. # type typ ...
- Docker简明教程
Docker简明教程 [编者的话]使用Docker来写代码更高效并能有效提升自己的技能.Docker能打包你的开发环境,消除包的依赖冲突,并通过集装箱式的应用来减少开发时间和学习时间. Docker作 ...