之前使用的是递归的方法来解决的问题,后来有点想用bfs(宽度优先搜索来尝试一下的想法,在网上看到有人使用了dfs(深度优先搜索)更加坚定了自己的这种想法。

这个方法首先是以顶点的四组开始,加入那些没有放置卡片的位置,同时使用另外一个数组来标记距离,就这样一直拓展下去,如果碰到了目标位置,那么我们就对totalStep进行对比赋值。

切记,每次搜索结束后,要对标记数组重新赋值,每个Board结束后,要对队列清空。

#include <bits/stdc++.h>
using namespace std;
class Node{
//int direct,step;
public:
int x,y;
Node(int x_,int y_):x(x_),y(y_){}
};
class directNode{
public:
int direct,step;
directNode(int direct_,int step_):direct(direct_),step(step_){}
};
int w,h;
char s[][];
int mark[][];
directNode * directStep[][];
int direction[][]={{-,},{,},{,},{,-}};
int totalStep=;
queue<Node *> ques;
void findTotalPath(int x1,int y1,int x2,int y2,int direct,int step){
while(ques.size()>){
Node * temp=ques.front();
//cout<<temp->x<<"-"<<temp->y<<endl;
if(temp->x==x2&&temp->y==y2){
if(totalStep>directStep[temp->y][temp->x]->step&&directStep[temp->y][temp->x]->step!=){
totalStep=directStep[temp->y][temp->x]->step;
continue;
}
}
for(int i=;i<;i++){
int xx1=temp->x+direction[i][];
int yy1=temp->y+direction[i][];
if((xx1>-)&&(xx1<w+)&&(yy1>-)&&(yy1<h+)&&(s[yy1][xx1]==' '||(yy1==y2&&xx1==x2&&s[yy1][xx1]=='X'))){
//cout<<"* "<<directStep[temp->y][temp->x]->step<<","<<directStep[temp->y][temp->x]->direct<<endl;
int tempStep=directStep[temp->y][temp->x]->direct!=i?directStep[temp->y][temp->x]->step+:directStep[temp->y][temp->x]->step;
if(mark[yy1][xx1]==){
if(directStep[yy1][xx1]->step>tempStep){
directStep[yy1][xx1]->direct=i;
directStep[yy1][xx1]->step=tempStep;
}
}else{
ques.push(new Node(xx1,yy1));
directStep[yy1][xx1]->direct=i;
directStep[yy1][xx1]->step=tempStep;
mark[yy1][xx1]=;
//cout<<xx1<<" "<<yy1<<endl;
//cout<<"tempStep:"<<tempStep<<endl;
}
}
}
ques.pop();
}
}
int main(){
int id=;
while(){
/*cin>>w>>h;
cin.ignore();*/
scanf("%d %d",&w,&h);
if(w==&&h==) break;
/*for(int i=0;i<h+2;i++){
s[i][0]=s[i][w+1]=' ';
}
for(int j=0;j<w+2;j++){
s[0][j]=s[w+1][j]=' ';
}*/
for(int i=;i<h+;i++){
for(int j=;j<w+;j++){
directStep[i][j]=new directNode(-,);
}
}
for (int i = ; i <; i ++) s[][i] =s[i][] = ' ';
for(int i=;i<h+;i++){
getchar();
//string str="";
//getline(cin,str);
for(int j=;j<w+;j++){
//s[i][j]=str[j-1];
s[i][j]=getchar();
}
}
for (int i = ; i <= w; i ++)
s[h + ][i + ] = ' ';
for (int i = ; i <= h; i ++)
s[i + ][w + ] = ' ';
cout<<"Board #"<<id<<":"<<endl;
id++;
int x1,y1,x2,y2;
int subId=;
while(){
subId++;
totalStep=;
memset(mark, , sizeof(mark));
cin>>x1>>y1>>x2>>y2;
ques.push(new Node(x1,y1));
mark[y1][x1]=;
if(x1==&&y1==&&x2==&&y2==) break;
int step=;
int direct=-;
findTotalPath(x1,y1,x2,y2,direct,step);
if(totalStep<)
cout<<"Pair "<<subId<<": "<<totalStep<<" segments."<<endl;
else{
cout<<"Pair "<<subId<<": "<<"impossible."<<endl;
}
for(int i=;i<h+;i++){
for(int j=;j<w+;j++){
directStep[i][j]=new directNode(-,);
}
}
}
while(ques.size()>){
ques.pop();
}
cout<<endl;
}
}

2802:小游戏利用bfs来实现的更多相关文章

  1. OpenJudge 2802 小游戏 / Poj 1101 The Game

    1.链接地址: http://bailian.openjudge.cn/practice/2802 http://poj.org/problem?id=1101 2.题目: 总时间限制: 1000ms ...

  2. 分享:计算机图形学期末作业!!利用WebGL的第三方库three.js写一个简单的网页版“我的世界小游戏”

    这几天一直在忙着期末考试,所以一直没有更新我的博客,今天刚把我的期末作业完成了,心情澎湃,所以晚上不管怎么样,我也要写一篇博客纪念一下我上课都没有听,还是通过强大的度娘完成了我的作业的经历.(当然作业 ...

  3. 了解python,利用python来制作日常猜拳,猜价小游戏

    初次接触python,便被它简洁优美的语言所吸引,正所谓人生苦短,python当歌.python之所以在最近几年越发的炽手可热,离不开它的一些特点: 1.易于学习:Python有相对较少的关键字,结构 ...

  4. 【转】利用 three.js 开发微信小游戏的尝试

    前言 这是一次利用 three.js 开发微信小游戏的尝试,并不能算作是教程,只能算是一篇笔记吧. 微信 WeChat 6.6.1 开始引入了微信小游戏,初期上线了一批质量相当不错的小游戏.我在查阅各 ...

  5. 推箱子小游戏《格鲁的实验室》13关 - bfs最短路径

    下载了一款推箱子小游戏,第13关的时候怎么也破不了最佳纪录(最少步数是9而我们最好的方案是10步),因为数据比较小(6*8的方阵),所以写了个BFS来找最短路. 游戏的目标是把小黄人推到黄色球,小绿人 ...

  6. C语言-纸牌计算24点小游戏

    C语言实现纸牌计算24点小游戏 利用系统时间设定随机种子生成4个随机数,并对4个数字之间的运算次序以及运算符号进行枚举,从而计算判断是否能得出24,以达到程序目的.程序主要功能已完成,目前还有部分细节 ...

  7. 软件工程:黄金G点小游戏1.0

    我们要做的是黄金G点小游戏: N个同学(N通常大于10),每人写一个0~100之间的有理数 (不包括0或100),交给裁判,裁判算出所有数字的平均值,然后乘以0.618(所谓黄金分割常数),得到G值. ...

  8. flash小游戏在Kongregate上线——BasketBall Master(篮球大师)

    小游戏地址,欢迎上去留言评论.游戏完成度没有达到期望水平,只能算完成了核心玩法吧,一些其他构想来不及实现. BasketBall Master(篮球大师) 这个小游戏很早之前就基本做好了,只因有些细节 ...

  9. 软件工程 Android小游戏 猜拳大战

    一.前言 最近学校举办的大学生程序设计竞赛,自己利用课余时间写了一个小游戏,最近一直在忙这个写这个小游戏,参加比赛,最终是老师说自己写的简单,可以做的更复杂的点的.加油 二.内容简介 自己玩过Andr ...

随机推荐

  1. python中的模块和包

    模块 一 什么是模块 模块就是一组功能的集合体,可以通过导入模块来复用模块的功能. 比如我在同一个文件夹定义两个.py文件,分别命名为A.py和B.py,那么可以通过在A文件里通过import B来使 ...

  2. C# 委托 事件

    一:什么叫委托 通过反射发现,委托其实是一个类,继承自System.MulticastDelegate,但是System.MulticastDelegate这个类是特殊类,不能被继承 二:委托的声明 ...

  3. javascript小实例,实现99乘法表及隔行变色

    人生短暂,废话不多说,直奔主题! 这个小实例的要求: 实现在页面中输出99乘法表.(要求:以每三行为一组,实现隔行变色(颜色为白,红,黄(也可自己定义)),鼠标滑过每一行,行背景颜色变为蓝色,鼠标离开 ...

  4. IdentityServer4 中文文档 -14- (快速入门)使用 ASP.NET Core Identity

    IdentityServer4 中文文档 -14- (快速入门)使用 ASP.NET Core Identity 原文:http://docs.identityserver.io/en/release ...

  5. 第一册:lesson thirteen.

    原文:A new dress. A:What color's your new dress? B:It' green.Come upstairs and see it. A:Thank you. B: ...

  6. sqlserver 操作数据表语句模板

    从网上搜的,一点一点加吧. -----------设置事务全部回滚----------------- SET XACT_ABORT ON BEGIN BEGIN TRY BEGIN TRANSACTI ...

  7. ASP.NET Core中的Startup类

    ASP.NET Core程序要求有一个启动类.按照惯例,启动类的名字是 "Startup" .Startup类负责配置请求管道,处理应用程序的所有请求.你可以指定在Main方法中使 ...

  8. 从零开始学安全(十九)●PHP数组函数

    $temp= array(1,2,3,,,,) 创建一个数组赋值给temp $id=range(1,6,2);     成长值   1到6  跨度为2  就是3个长度数组 也可以是字符“a” &quo ...

  9. SpringMVC表单验证与Velocity整合

    阅读本文约“1.2分钟” 定义表单类 以Login为例,有username和password两个字段 import javax.validation.constraints.NotNull; impo ...

  10. MyBatis:CRUD功能

    在前面已经自动生成了mapper和pojo,接下来我们实现mybatis的CRUD功能,先新建service.controller层的方法. 这里的sid是一个开源的id生成类,写完后,我们还需要在启 ...