题目描述

X星的坦克战车很奇怪,它必须交替地穿越正能量辐射区和负能量辐射区才能保持正常运转,否则将报废。
某坦克需要从A区到B区去(A,B区本身是安全区,没有正能量或负能量特征),怎样走才能路径最短?
已知的地图是一个方阵,上面用字母标出了A,B区,其它区都标了正号或负号分别表示正负能量辐射区。
例如:

A + - + -
- + - - +
- + + + -
+ - + - +
B + - + -

坦克车只能水平或垂直方向上移动到相邻的区。

输入

输入第一行是一个整数n,表示方阵的大小, 4<=n<100
接下来是n行,每行有n个数据,可能是A,B,+,-中的某一个,中间用空格分开。
输入保证A,B都只出现一次。

输出

要求输出一个整数,表示坦克从A区到B区的最少移动步数。
如果没有方案,则输出-1

样例输入
Copy

5
A + - + -
- + - - +
- + + + -
+ - + - +
B + - + -

样例输出 Copy

10

题解一:BFS

#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
int n,flag=;
int vis[][];
char a[][];
int dir[][]={{,},{,-},{,},{-,}};;
struct node
{
int x,y,step;
};
int check(int x,int y)
{
if(x>=&&x<n&&y>=&&y<n)
return ;
else
return ; }
void bfs(int x,int y)
{
node temp;
temp.x=x;
temp.y=y;
temp.step=;
queue<node>p;
p.push(temp);
vis[x][y]=;
while(!p.empty())
{
node now=p.front();
p.pop();
for(int i=;i<;i++)
{
int tx=now.x+dir[i][];
int ty=now.y+dir[i][];
if(check(tx,ty)&&vis[tx][ty]==&&a[tx][ty]!=a[now.x][now.y])
{
if(a[tx][ty]=='B')
{
cout<<now.step+<<endl;
flag=;
return ;
}
vis[tx][ty]=;
node temp;
temp.x=tx;
temp.y=ty;
temp.step=now.step+;
p.push(temp);
}
}
}
}
int main()
{
cin>>n;
int s,e;
memset(vis,,sizeof(vis));
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
cin>>a[i][j];
if(a[i][j]=='A')
{
s=i;
e=j;
}
}
}
bfs(s,e);
if(flag==)
cout<<-<<endl;
return ;
}
// 5
// A + - + -
// - + - - +
// - + + + -
// + - + - +
// B + - + -

题解二:DFS

#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int n,flag=,mn=;
int vis[][],dir[][]={{,},{,},{-,},{,-}};
char a[][];
int check(int x,int y)
{
if(x>=&&x<n&&y>=&&y<n)
return ;
else
{
return ;
} }
void dfs(int x,int y,int cnt)
{
if(mn<=cnt)//优化避免超时
return ;
if(a[x][y]=='B')
{
flag=;
mn=min(mn,cnt);
return ;
}
else
{
for(int i=;i<;i++)
{
int tx=x+dir[i][];
int ty=y+dir[i][];
if(check(tx,ty)&&vis[tx][ty]==&&a[x][y]!=a[tx][ty])
{
vis[tx][ty]=;
dfs(tx,ty,cnt+);
vis[tx][ty]=;
}
}
}
}
int main()
{
cin>>n;
int x,y;
memset(vis,,sizeof(vis));
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
cin>>a[i][j];
if(a[i][j]=='A')
{
x=i;
y=j;
}
}
}
vis[x][y]=;
dfs(x,y,);
if(flag==)
cout<<-<<endl;
else
cout<<mn<<endl;
//system("pause");
return ;
}

穿越雷区--蓝桥杯--DFS/BFS的更多相关文章

  1. 方格填数--蓝桥杯---dfs

    答案:1580 相似题目:N皇后问题 注意要枚举的是什么 #include<iostream> #include<string.h> using namespace std; ...

  2. 蓝桥杯dfs搜索专题

    2018激光样式 #include<bits/stdc++.h> using namespace std; /* dfs(i) 第i个激光机器 有两种选择:vis[i-1] == 0 时 ...

  3. 寒假作业---蓝桥杯---DFS

    题目描述 现在小学的数学题目也不是那么好玩的. 看看这个寒假作业: 每个方块代表1~13中的某一个数字,但不能重复. 比如: 6  + 7 = 13 9  - 8 = 1 3  * 4 = 12 10 ...

  4. 四阶幻方-蓝桥杯-DFS

    答案:416 用next_permutation()全部排列的话会超时 所以用dfs搜索,只搜索前三行就好,前三行确定之后,第四行也就确定 #include<iostream> #incl ...

  5. 蓝桥杯---剪格子(DFS&BFS)(小总结)

    问题描述 如下图所示,3 x 3 的格子中填写了一些整数. +--*--+--+ |10* 1|52| +--****--+ |20|30* 1| *******--+ | 1| 2| 3| +--+ ...

  6. java实现第六届蓝桥杯穿越雷区

    穿越雷区 题目描述 X星的坦克战车很奇怪,它必须交替地穿越正能量辐射区和负能量辐射区才能保持正常运转,否则将报废. 某坦克需要从A区到B区去(A,B区本身是安全区,没有正能量或负能量特征),怎样走才能 ...

  7. 蓝桥杯---数独(模拟 || dfs)

    [编程题](满分33分) "数独"是当下炙手可热的智力游戏.一般认为它的起源是"拉丁方块",是大数 学家欧拉于1783年发明的. 如图[1.jpg]所示:6x6 ...

  8. 蓝桥杯之大臣的旅费(两次dfs)

    Description 很久以前,T王国空前繁荣.为了更好地管理国家,王国修建了大量的快速路,用于连接首都和王国内的各大城市. 为节省经费,T国的大臣们经过思考,制定了一套优秀的修建方案,使得任何一个 ...

  9. 蓝桥杯之剪格子(经典dfs)

    如下图所示,3 x 3 的格子中填写了一些整数. +--*--+--+ |10* 1|52| +--****--+ |20|30* 1| *******--+ | 1| 2| 3| +--+--+-- ...

随机推荐

  1. 【游戏体验】Buttonhunt(鼠标寻找按钮)

    虽然画风千奇百变 但这款益智游戏的趣味性相当高 推荐试玩 个人测评 游戏性 9/10 音乐 4/10 剧情 6/10 总评 19/30

  2. redis (一) --- 基本使用

    概述 redis是基于key-value 我们所说的数据类型实际是 key-value 中的 value .文章主要介绍的是redis 几个重要的数据类型的使用. 简单使用 //keys patter ...

  3. http的长连接和短连接(史上最通俗!)

    1.以前的误解 很久之前就听说过长连接的说法,而且还知道HTTP1.0协议不支持长连接,从HTTP1.1协议以后,连接默认都是长连接.但终究觉得对于长连接一直懵懵懂懂的,有种抓不到关键点的感觉. 今天 ...

  4. Unable to create a debugging engine.

    用QT Creator调试的时候报如下错误: Unable to create a debugging engine. QT里面打开Tools -> Options -> Kits 发现D ...

  5. 【MYSQL建库和建表】MYSQL建库和建表

    1.Navicat创建Mysql数据库 2.创建创建用户表和索引 CREATE TABLE `t_user` ( `id` ) NOT NULL AUTO_INCREMENT COMMENT '主键' ...

  6. 如何使用 Workman 做一个聊天室

    一:首先,得简单说说 thinkphp+workerman 的安装. 安装 thinkphp5.1 composer create-project topthink/think=5.1.x-dev t ...

  7. 鸡汤 - Choice is yours

    传送门 https://kamranahmed.info/blog/2018/03/24/choice-is-yours/ Our whole lives are driven by the choi ...

  8. 拓扑排序板子 hihocoder-1174

    思路 不断删入度为1的点及其出边. 图解 #include <bits/stdc++.h> using namespace std; const int maxn=1e5+10; vect ...

  9. CSS样式的引入&区别&权重&CSS层叠性&CSS样式的来源

    CSS样式的引入: 内部样式: 内部样式:写在当前页面style标签中的样式 内联样式:写在style属性中的样式 外部样式: link标签引入的CSS文件 @import引入的CSS文件,需要写在c ...

  10. 吴裕雄--天生自然Numpy库学习笔记:Numpy 数组操作

    import numpy as np a = np.arange(8) print ('原始数组:') print (a) print ('\n') b = a.reshape(4,2) print ...