BFS+贪心 HDOJ 5335 Walk Out
/*
题意:求从(1, 1)走到(n, m)的二进制路径值最小
BFS+贪心:按照标程的作法,首先BFS搜索所有相邻0的位置,直到1出现。接下去从最靠近终点的1开始,
每一次走一步,不走回头路,只往下或往右走。因为满足i = j + (i - j)的坐标(j, i - j)可能不止一个,选择最小的访问
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
using namespace std; const int MAXN = 1e3 + ;
const int INF = 0x3f3f3f3f;
int dx[] = {, , , -};
int dy[] = {, , -, };
char maze[MAXN][MAXN];
bool vis[MAXN][MAXN];
int n, m; bool judge(int x, int y) {
if (x < || x > n || y < || y > m || vis[x][y]) return false;
return true;
} void BFS(void) {
memset (vis, false, sizeof (vis)); int mx = ;
queue<pair<int, int> > Q; Q.push (make_pair (, )); vis[][] = true;
while (!Q.empty ()) {
int x = Q.front ().first, y = Q.front ().second; Q.pop ();
if (maze[x][y] == '') {
for (int i=; i<; ++i) {
int tx = x + dx[i], ty = y + dy[i];
if (!judge (tx, ty)) continue;
mx = max (mx, tx + ty);
Q.push(make_pair (tx, ty)); vis[tx][ty] = true;
}
}
} if (vis[n][m] && maze[n][m] == '') {
puts (""); return ;
} printf ("");
for (int i=mx; i<n+m; ++i) {
char mn = '';
for (int j=; j<=n; ++j) {
if ( <= i - j && i - j <= m && vis[j][i-j]) {
mn = min (mn, maze[j+][i-j]);
mn = min (mn, maze[j][i-j+]);
}
}
printf ("%c", mn);
for (int j=; j<=n; ++j) {
if ( <= i - j && i - j <= m && vis[j][i-j]) {
if (maze[j+][i-j] == mn) vis[j+][i-j] = true;
if (maze[j][i-j+] == mn) vis[j][i-j+] = true;
}
}
}
puts ("");
} int main(void) { //HDOJ 5335 Walk Out
//freopen ("I.in", "r", stdin);
int T; scanf ("%d", &T);
while (T--) {
scanf ("%d%d", &n, &m);
for (int i=; i<=n; ++i) scanf ("%s", maze[i] + );
for (int i=; i<=n; ++i) maze[i][] = maze[i][m+] = '';
for (int i=; i<=m+; ++i) maze[][i] = maze[n+][i] = '';
BFS ();
} return ;
}
BFS+贪心 HDOJ 5335 Walk Out的更多相关文章
- hdoj 5335 Walk Out
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5335 #include<stdio.h> #include<cstring> ...
- 离散化+BFS HDOJ 4444 Walk
题目传送门 /* 题意:问一个点到另一个点的最少转向次数. 坐标离散化+BFS:因为数据很大,先对坐标离散化后,三维(有方向的)BFS 关键理解坐标离散化,BFS部分可参考HDOJ_1728 */ # ...
- HDU-1072 Nightmare (bfs+贪心)
Nightmare Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Sub ...
- hdu 5335 Walk Out (搜索)
题目链接: hdu 5335 Walk Out 题目描述: 有一个n*m由0 or 1组成的矩形,探险家要从(1,1)走到(n, m),可以向上下左右四个方向走,但是探险家就是不走寻常路,他想让他所走 ...
- hdu 5335 Walk Out (2015 Multi-University Training Contest 4)
Walk Out Time Limit: 2000/10 ...
- UVA12130 Summits(BFS + 贪心)
UVA12130 Summits(BFS + 贪心) 题目链接 题目大意: 给你一个h ∗ w 的矩阵,矩阵的每一个元素都有一个值,代表这个位置的高度. 题目要求你找出这个图中有多少个位置是峰值点.从 ...
- HDU 5335——Walk Out——————【贪心】
Walk Out Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- hdu 5335 Walk Out 搜索+贪心
Walk Out Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total S ...
- HDU 5335 Walk Out BFS 比较坑
H - H Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status ...
随机推荐
- Go append方法
append用来将元素添加到切片末尾并返回结果.看代码: package main import "fmt" func main() { x := [],,} y := [],,} ...
- js贪吃蛇
function init() { w = 40; m = 20; d = w * m / 2; food = null; dm = new ht.DataModel(); g3d = new ht. ...
- [小技巧]让你的GridView支持IQueryable,并自动实现真分页
众所周知,asp.net自带的GridView在自带分页方面设计得很2,因为它是假分页,即内存分页.而且它不智能支持强大的Iqueryable. 但这表明微软忽略了现实中的分页需求吗?答案应该不是,我 ...
- 42.JTAG接口使用注意
无论是客户反馈,还是自己亲身经历,USB-Blaster不能下载配置FPGA的情况时有出现.究其原因,大致有如下几条: 1. FPGA器件上的JTAG相关引脚出现故障: 2. USB-Blaster坏 ...
- log4j打印参数
%m 输出代码中指定的消息 %p 输出优先级,即DEBUG,INFO,WARN,ERROR,FATAL %r 输出自应用启动到输出该log信息耗费的毫秒数 %c 输出所属的类目,通 ...
- 国内最快的jquery cdn
cdnjs.cn是cdnjs.com在国内的镜像服务,项目托管与著名的又拍云存储,目前又拍云在全国有几十个cdn节点,并且还在增加中. cdnjs.cn 托管的jquery相信会成为国内最快的jque ...
- 【Binary Tree Post order Traversal】cpp
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...
- ASP.NET本质论阅读----线程与异步
线程 概要:操作系统通过线程对程序的执行进行管理 线程的数据结构:1.线程的核心对象(包含线程的当前寄存器状态),调用线程时-寄存器状态被加载到CPU中. 2.线程环境块TEB:一块用户模式下的内存 ...
- 1565: [NOI2009]植物大战僵尸 - BZOJ
Description Input Output仅包含一个整数,表示可以获得的最大能源收入.注意,你也可以选择不进行任何攻击,这样能源收入为0.Sample Input3 210 020 0-10 0 ...
- foj 2044 1 M possible 二进制压缩
题目链接: http://acm.fzu.edu.cn/problem.php?pid=2044 题意: 给出 一大堆数,找出2个出现次数模3 为1 的两个数字 题解: 把一个数分为几位拆开统计 ...