穿越雷区--蓝桥杯--DFS/BFS
题目描述
某坦克需要从A区到B区去(A,B区本身是安全区,没有正能量或负能量特征),怎样走才能路径最短?
已知的地图是一个方阵,上面用字母标出了A,B区,其它区都标了正号或负号分别表示正负能量辐射区。
例如:
A + - + -
- + - - +
- + + + -
+ - + - +
B + - + -
坦克车只能水平或垂直方向上移动到相邻的区。
输入
接下来是n行,每行有n个数据,可能是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的更多相关文章
- 方格填数--蓝桥杯---dfs
答案:1580 相似题目:N皇后问题 注意要枚举的是什么 #include<iostream> #include<string.h> using namespace std; ...
- 蓝桥杯dfs搜索专题
2018激光样式 #include<bits/stdc++.h> using namespace std; /* dfs(i) 第i个激光机器 有两种选择:vis[i-1] == 0 时 ...
- 寒假作业---蓝桥杯---DFS
题目描述 现在小学的数学题目也不是那么好玩的. 看看这个寒假作业: 每个方块代表1~13中的某一个数字,但不能重复. 比如: 6 + 7 = 13 9 - 8 = 1 3 * 4 = 12 10 ...
- 四阶幻方-蓝桥杯-DFS
答案:416 用next_permutation()全部排列的话会超时 所以用dfs搜索,只搜索前三行就好,前三行确定之后,第四行也就确定 #include<iostream> #incl ...
- 蓝桥杯---剪格子(DFS&BFS)(小总结)
问题描述 如下图所示,3 x 3 的格子中填写了一些整数. +--*--+--+ |10* 1|52| +--****--+ |20|30* 1| *******--+ | 1| 2| 3| +--+ ...
- java实现第六届蓝桥杯穿越雷区
穿越雷区 题目描述 X星的坦克战车很奇怪,它必须交替地穿越正能量辐射区和负能量辐射区才能保持正常运转,否则将报废. 某坦克需要从A区到B区去(A,B区本身是安全区,没有正能量或负能量特征),怎样走才能 ...
- 蓝桥杯---数独(模拟 || dfs)
[编程题](满分33分) "数独"是当下炙手可热的智力游戏.一般认为它的起源是"拉丁方块",是大数 学家欧拉于1783年发明的. 如图[1.jpg]所示:6x6 ...
- 蓝桥杯之大臣的旅费(两次dfs)
Description 很久以前,T王国空前繁荣.为了更好地管理国家,王国修建了大量的快速路,用于连接首都和王国内的各大城市. 为节省经费,T国的大臣们经过思考,制定了一套优秀的修建方案,使得任何一个 ...
- 蓝桥杯之剪格子(经典dfs)
如下图所示,3 x 3 的格子中填写了一些整数. +--*--+--+ |10* 1|52| +--****--+ |20|30* 1| *******--+ | 1| 2| 3| +--+--+-- ...
随机推荐
- C#系统库的源代码
.NET Core:https://github.com/dotnet/corefx .NET Framework:https://referencesource.microsoft.com
- 用Python给头像加上圣诞帽或圣诞老人小徽章
随着圣诞的到来,想给给自己的头像加上一顶圣诞帽.如果不是头像,就加一个圣诞老人陪伴. 用Python给头像加上圣诞帽,看了下大概也都是来自2017年大神的文章: https://zhuanlan.zh ...
- opencv:图像直方图均衡化
// 直方图均衡化 Mat gray, dst; cvtColor(src, gray, COLOR_BGR2GRAY); equalizeHist(gray, dst); imshow(" ...
- Could not set property of class with value There is no setter for property named
检查entity中类的属性与MAPPER中的resultMap属性是否一致
- ACL与OSPF综合实验
OSPF与ACL 综合实验 拓扑图如下: 分析: 配置基本配置: R1: R2: R3: 2.配置OSPF: R1: R2: R3: IT: 设置IT的ip 并划分到ospf2区域 3.配置ACL ...
- tensorflow按需分配GPU问题
使用tensorflow,如果不加设置,即使是很小的模型也会占用整块GPU,造成资源浪费. 所以我们需要设置,使程序按需使用GPU. 具体设置方法: gpu_options = tf.GPUOptio ...
- Jquery span标签的取值赋值
//赋值 $("#spanid").html(value) //取值 $("#spanid").text()
- 【PAT甲级】1083 List Grades (25 分)
题意: 输入一个正整数N(<=101),接着输入N个学生的姓名,id和成绩.接着输入两个正整数X,Y(0<=X,Y<=100),逆序输出成绩在x,y之间的学生的姓名和id. tric ...
- mysql中utf8字符集的设置及character_set_database属性修改
mariadb配置文件修改字符集: [mysqld] atadir=/usr/local/mysql/datasocket=/tmp/mysql.sock # Disabling symbolic-l ...
- C++标准库里面没有字符分割函数split,自己编写函数实现字符串分割功能
#include <vector> #include <string> #include <iostream> using namespace std; vecto ...