思路:

bfs求最短路径。

 #include<stdio.h>
#include<iostream>
#include<queue>
#include<cstring>
#define maxn 105
using namespace std;
int sx, sy, ex, ey;
char map[][];
char vis[][];
int dic[][] = { { -, },{ ,- },{ , },{ , } };//4个方向
int res;
struct node {
int x, y, step;
};
int n;
bool check(int x, int y)
{
if (x >= && x < n&&y >= && y < n&&map[x][y] != '#'&&vis[x][y] !=)
return true;
//printf("x=%d y=%d c=%c\n", x, y,map[x][y]);
return false;
} void bfs()
{
queue<node> q;
node a;
node next;
a.x = sx;
a.y = sy;
a.step = ;
vis[a.x][a.y] = ;
q.push(a);
while (!q.empty())
{
a = q.front();
q.pop();
for (int i = ; i<; i++)
{
next = a;
next.x += dic[i][];
next.y += dic[i][];
next.step = a.step + ;
if (next.x == ex&&next.y == ey)//找到出口
{
res = next.step;
// printf("res=%d\n", res);
return;
}
if (check(next.x, next.y))//检查合法性
{
vis[next.x][next.y] = ;
//printf("vis[%d][%d]=1\n", next.x, next.y);
q.push(next);
}
}
}
res = -;
}
int main()
{
while (scanf("%d", &n) == )
{
for (int i = ; i<n; i++)
scanf("%s", &map[i]);
memset(vis, , sizeof(vis));
//for (int i = 0; i < n; i++)
//printf("%s\n", map[i]);
for (int i = ; i<n; i++)
{
for (int j = ; j<n; j++)
{
if (map[i][j] == 'S')
{
sx = i;
sy = j;
}
if (map[i][j] == 'E')
{
ex = i;
ey = j;
}
}
}
//printf("sx=%d sy=%d ex=%d ey=%d\n", sx, sy, ex, ey);
bfs();
printf("%d\n", res);
}
return ;
}

FZU 2285 迷宫寻宝的更多相关文章

  1. Problem 2285 迷宫寻宝 (BFS)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=2285 Problem 2285 迷宫寻宝 Accept: 323    Submit: 1247Time Li ...

  2. Problem 2285 迷宫寻宝

    http://acm.fzu.edu.cn/problem.php?pid=2285 Problem Description 洪尼玛今天准备去寻宝,在一个n*n (n行, n列)的迷宫中,存在着一个入 ...

  3. nyoj 82 迷宫寻宝(一)

    点击打开链接 迷宫寻宝(一) 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 一个叫ACM的寻宝者找到了一个藏宝图,它根据藏宝图找到了一个迷宫,这是一个很特别的迷宫,迷宫 ...

  4. Unity3D实现立体迷宫寻宝

    Unity3D实现立体迷宫寻宝 这个小游戏是一个白痴在一个昏暗的房间走动找到关键得分点,然后通关游戏.入门Unity3D做的第一款游戏,比较无聊,但实现了一般的游戏功能.如,人物控制,碰撞检测,主控制 ...

  5. 迷宫寻宝(一)(bfs)

    迷宫寻宝(一) 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 一个叫ACM的寻宝者找到了一个藏宝图,它根据藏宝图找到了一个迷宫,这是一个很特别的迷宫,迷宫里有N个编 ...

  6. 福州大学第十五届程序设计竞赛_重现赛B题迷宫寻宝

    Problem B 迷宫寻宝 Accept: 52    Submit: 183Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem De ...

  7. NYOJ82 迷宫寻宝(一)【BFS】

    迷宫寻宝(一) 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描写叙述 一个叫ACM的寻宝者找到了一个藏宝图.它依据藏宝图找到了一个迷宫,这是一个非常特别的迷宫,迷宫里有N个 ...

  8. nyoj 82 迷宫寻宝(二)

    http://acm.nyist.net/JudgeOnline/problem.php?pid=83 题目解法主要在于判断两线段是否相交,思路是穷举所有地图四周的点,其中每一个边界上的点和终点构成一 ...

  9. 【DFS】NYOJ-82 迷宫寻宝(一)-条件迷宫问题

    [题目链接:NYOJ-82] #include<iostream> #include<cstring> using namespace std; struct node{ in ...

随机推荐

  1. ACM-ICPC 2018 沈阳赛区网络预赛 K Supreme Number(规律)

    https://nanti.jisuanke.com/t/31452 题意 给出一个n (2 ≤ N ≤ 10100 ),找到最接近且小于n的一个数,这个数需要满足每位上的数字构成的集合的每个非空子集 ...

  2. [Android] Android 锁屏实现与总结 (二)

    上接: [Android] Android 锁屏实现与总结 (一) 系列文章链接如下: [Android] Android 锁屏实现与总结 (一) [Android] Android 锁屏实现与总结 ...

  3. Shiro 系列 - 基本知识

    和 Spring Security 项目一样, Apache Shiro 也是一个被广泛使用安全框架, 它们都能完成认证.授权.会话管理等. 简单对比一下 Apache Shiro 和 Spring ...

  4. JS 正则中的命名捕获分组

    假设你在一段陌生的代码中看到这样一个函数: function toLocalDate(date) { return date.replace(/(\d{2})-(\d{2})-(\d{4})/, &q ...

  5. python学习03

    字符串的基本使用 1.字符编码集 ASCII编码:外国人常用的大小写英文字母.数字和一些符号,一共127个字符,用1个字节(byte)可以涵盖完,也就是8个位,它将序列中的每个字节理解为一个字符. U ...

  6. KNN和Kmeans聚类有什么不同?

    这两种算法之间的根本区别是,Kmeans本质上是无监督学习而KNN是监督学习.Kmeans是聚类算法,KNN是分类(或回归)算法. Kmeans算法把一个数据集分割成簇,使得形成的簇是同构的,每个簇里 ...

  7. 🍓 移动端调试工具之vconsole的使用~ 🍓

    这里以在vue项目中的使用为例⬇️ 嗯模块化的. 不消多说,先cnpm install vconsole -S 然后在mian.js中配置之- ok啦-- 开发混合app的筒子,使用mac的话也有别的 ...

  8. Arduino语言介绍

    Arduino语言介绍 Arduino语言是建立在C/C++基础上的,其基础是C语言,Arduino语言只不过把AVR单片机(微控制器)相关的一些参数设置都函数化,不用我们去了解他的底层,让不了解AV ...

  9. Find K Closest Elements

    Given a sorted array, two integers k and x, find the k closest elements to x in the array. The resul ...

  10. MVC中一般为什么用IQueryable而不是用IList?

    IList(IList<T>)会立即在内存里创建持久数据,这就没有实现“延期执行(deferred execution)”,如果被加载的实体有关联实体(associations),此关联实 ...