FZU 2285 迷宫寻宝
思路:
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 迷宫寻宝的更多相关文章
- Problem 2285 迷宫寻宝 (BFS)
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2285 Problem 2285 迷宫寻宝 Accept: 323 Submit: 1247Time Li ...
- Problem 2285 迷宫寻宝
http://acm.fzu.edu.cn/problem.php?pid=2285 Problem Description 洪尼玛今天准备去寻宝,在一个n*n (n行, n列)的迷宫中,存在着一个入 ...
- nyoj 82 迷宫寻宝(一)
点击打开链接 迷宫寻宝(一) 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 一个叫ACM的寻宝者找到了一个藏宝图,它根据藏宝图找到了一个迷宫,这是一个很特别的迷宫,迷宫 ...
- Unity3D实现立体迷宫寻宝
Unity3D实现立体迷宫寻宝 这个小游戏是一个白痴在一个昏暗的房间走动找到关键得分点,然后通关游戏.入门Unity3D做的第一款游戏,比较无聊,但实现了一般的游戏功能.如,人物控制,碰撞检测,主控制 ...
- 迷宫寻宝(一)(bfs)
迷宫寻宝(一) 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 一个叫ACM的寻宝者找到了一个藏宝图,它根据藏宝图找到了一个迷宫,这是一个很特别的迷宫,迷宫里有N个编 ...
- 福州大学第十五届程序设计竞赛_重现赛B题迷宫寻宝
Problem B 迷宫寻宝 Accept: 52 Submit: 183Time Limit: 1000 mSec Memory Limit : 32768 KB Problem De ...
- NYOJ82 迷宫寻宝(一)【BFS】
迷宫寻宝(一) 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描写叙述 一个叫ACM的寻宝者找到了一个藏宝图.它依据藏宝图找到了一个迷宫,这是一个非常特别的迷宫,迷宫里有N个 ...
- nyoj 82 迷宫寻宝(二)
http://acm.nyist.net/JudgeOnline/problem.php?pid=83 题目解法主要在于判断两线段是否相交,思路是穷举所有地图四周的点,其中每一个边界上的点和终点构成一 ...
- 【DFS】NYOJ-82 迷宫寻宝(一)-条件迷宫问题
[题目链接:NYOJ-82] #include<iostream> #include<cstring> using namespace std; struct node{ in ...
随机推荐
- pyspider框架学习
一.crawl()方法学习: 1.url:爬去是的url,可以定义单个,可以定义为url列表. 2.callback:回调函数,指定该url使用哪个方法来解析. 3.age:任务的有效时间. 4.pr ...
- IDEA破解
1. 官网下载 idea激活 进入 C:\Windows\System32\drivers\etc 编辑host文件 加入0.0.0.0 account.jetbrains.com 2.打开ID ...
- DirectX11 With Windows SDK--14 深度测试
前言 当使用加法/减法/乘法颜色混合,或者使用透明混合的时候,在经过深度测试时可能会引发一些问题.例如现在我们需要使用加法混合来绘制一系列对象,而这些对象彼此之间不会相互阻挡.若我们仍使用原来的深度测 ...
- Sobel 边缘检测算子
转自:http://blog.csdn.net/xiaqunfeng123/article/details/17302003 Sobel 算子是一个离散微分算子 (discrete different ...
- Vim使用技巧:vimtutor
一 写在开头1.1 本文内容本文内容为vim快速入门——vimtutor. 二 第一讲2.1 移动光标(命令模式下)上 - k下 - j左 - h右 - l 2.2 进入和退出vim进入vim - v ...
- django - 总结 - cnblog 知识点
1.图像预览 点击头像------>点击input img和input重合; img在label,input-->display:none $("#avatar").c ...
- Beamer制作索引
\documentclass{beamer} \usepackage{multicol} \usepackage{makeidx} \newenvironment{theindex}{% \let\i ...
- [数学笔记Mathematical Notes]2-一个带对数的积分不等式
定理. $$\bex \int_0^1\frac{\ln^2x}{x^x}\rd x<2\int_0^1 \frac{\rd x}{x^x}. \eex$$ 证明: 由分部积分及 Fubini ...
- win10安装VMware v14.1.1.28517
一.下载 VMware v14.1.1.28517 下载地址(包含安装说明):http://www.downza.cn/soft/74728.html 二.VMware Workstation 14 ...
- 2018-2019-1 20189208《Linux内核原理与分析》第八周作业
学习笔记 1.ELF目标文件格式 编译器生成目标文件,目标文件与目标平台二进制兼容. ELF:可执行或可链接的格式,是目标文件格式标准. ELF类型: 可重定位文件:编译器汇编器创建的 .o 文件,最 ...