bfs(双向bfs加三维数组)
http://acm.hdu.edu.cn/showproblem.php?pid=2612
Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 32208 Accepted Submission(s): 10316
a year learning in Hangzhou, yifenfei arrival hometown Ningbo at
finally. Leave Ningbo one year, yifenfei have many people to meet.
Especially a good friend Merceki.
Yifenfei’s home is at the
countryside, but Merceki’s home is in the center of city. So yifenfei
made arrangements with Merceki to meet at a KFC. There are many KFC in
Ningbo, they want to choose one that let the total time to it be most
smallest.
Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes.
Each test case include, first two integers n, m. (2<=n,m<=200).
Next n lines, each line included m character.
‘Y’ express yifenfei initial position.
‘M’ express Merceki initial position.
‘#’ forbid road;
‘.’ Road.
‘@’ KCF
each test case output the minimum total time that both yifenfei and
Merceki to arrival one of KFC.You may sure there is always have a KFC
that can let them meet.
Y.#@
....
.#..
@..M
4 4
Y.#@
....
.#..
@#.M
5 5
Y..@.
.#...
.#...
@..M.
#...#
88
66
3 3
Y#@
.M#
..@
output: 66
//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdio.h>
#include <queue>
#include <stack>;
#include <map>
#include <set>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;
typedef long long ll ;
char a[][];
int n , m ;
int index[] , indey[];
int mx , my ;
int vis[][];
int dis[][] = {{ , } , {- , } , { , -} , { , }};
int step1[][][]; struct node
{
int x , y , step;
node(int x , int y , int step):x(x),y(y),step(step){}
node(){};
}; bool check(int x , int y)
{
if(x <= || y <= || x > n || y > m)
return false ;
if(!vis[x][y] && a[x][y] != '#')
return true ;
return false ;
} int bfs(int x , int y , int p)
{
queue<node>q;
node now , next ;
memset(vis , , sizeof(vis));
q.push(node(x , y , ));
vis[x][y] = ;
step1[p][x][y] = ;
while(!q.empty())
{
now = q.front();
q.pop();
for(int i = ; i < ; i++)
{
next.x = now.x + dis[i][];
next.y = now.y + dis[i][];
next.step = now.step + ;
if(check(next.x , next.y))
{
vis[next.x][next.y] = ;
step1[p][next.x][next.y] = next.step ;
q.push(next);
}
}
}
return ; } int main()
{
while(~scanf("%d%d" , &n , &m))
{
getchar();
for(int i = ; i <= n ; i++)
{
for(int j = ; j <= m ; j++)
{
scanf("%c" , &a[i][j]);
if(a[i][j] == 'Y')
{
index[] = i;
indey[] = j;
}
else if(a[i][j] == 'M')
{
index[] = i;
indey[] = j;
}
}
getchar();
}
int ans = INF;
bfs(index[] , indey[] , );
bfs(index[] , indey[] , ); for(int i = ; i <= n ; i++)
{
for(int j = ; j <= m ; j++)
{
if(a[i][j] == '@')
{
ans = min(ans , step1[][i][j] + step1[][i][j]);
}
}
}
printf("%d\n" , ans*); } return ;
}
比赛后码:
//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <string>
#include <stdio.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF 0x3f3f3f3f
#define mod 1000000007
#define PI acos(-1)
using namespace std;
typedef long long ll ;
const int N = 1e7 + ;
char s[][];
int vis[][];
int dir[][] = {{ , } , {- , } , { , } , { , -}};
int n , m ;
int ans[][][]; struct node{
int x , y , w;
}; void bfs(int t , int x , int y)
{
node now , next , last;
queue<node>q;
now.x = x , now.y = y , now.w = ;
q.push(now);
vis[x][y] = ;
while(!q.empty())
{
next = q.front();
q.pop();
if(s[next.x][next.y] == '@')
{
ans[t][next.x][next.y] = next.w ;
}
for(int i = ; i < ; i++)
{
int xx = next.x + dir[i][];
int yy = next.y + dir[i][];
int nw = next.w + ;
if(xx < || xx >= n || yy < || yy >= m)
{
continue ;
}
if(vis[xx][yy] || s[xx][yy] == '#')
{
continue ;
}
vis[xx][yy] = ;
last.x = xx , last.y = yy , last.w = nw ;
q.push(last);
}
}
} int main()
{ while(~scanf("%d%d" , &n , &m))
{
int x , y , x1 , y1 ;
memset(vis , , sizeof(vis));
memset(ans , INF , sizeof(ans));//没有考虑到达不了的@(但最少有一个@可达)
//到达不了的@应该为无穷大,否则为0则答案错误
for(int i = ; i < n ; i++)
{
for(int j = ; j < m ; j++)
{
cin >> s[i][j] ;
if(s[i][j] == 'Y')
{
x = i , y = j ;
}
if(s[i][j] == 'M')
{
x1 = i , y1 = j ;
}
}
}
bfs( , x , y);
memset(vis , , sizeof(vis));
bfs( , x1 , y1);
int mi = INF ;
for(int i = ; i < n ; i++)
{
for(int j = ; j < m ; j++)
{
if(s[i][j] == '@')
{
mi = min(mi , ans[][i][j] + ans[][i][j]);
}
}
}
cout << mi * << endl ; } return ;
}
bfs(双向bfs加三维数组)的更多相关文章
- UVa 1601 || POJ 3523 The Morning after Halloween (BFS || 双向BFS && 降维 && 状压)
题意 :w*h(w,h≤16)网格上有n(n≤3)个小写字母(代表鬼).要求把它们分别移动到对应的大写字母里.每步可以有多个鬼同时移动(均为往上下左右4个方向之一移动),但每步结束之后任何两个鬼不能占 ...
- UVA - 1601 The Morning after Halloween (BFS/双向BFS/A*)
题目链接 挺有意思但是代码巨恶心的一道最短路搜索题. 因为图中的结点太多,应当首先考虑把隐式图转化成显式图,即对地图中可以相互连通的点之间连边,建立一个新图(由于每步不需要每个鬼都移动,所以每个点需要 ...
- POJ1915Knight Moves(单向BFS + 双向BFS)
题目链接 单向bfs就是水题 #include <iostream> #include <cstring> #include <cstdio> #include & ...
- POJ 3126 Prime Path 解题报告(BFS & 双向BFS)
题目大意:给定一个4位素数,一个目标4位素数.每次变换一位,保证变换后依然是素数,求变换到目标素数的最小步数. 解题报告:直接用最短路. 枚举1000-10000所有素数,如果素数A交换一位可以得到素 ...
- POJ1915 BFS&双向BFS
俩月前写的普通BFS #include <cstdio> #include <iostream> #include <cstring> #include <q ...
- 双向BFS和启发式搜索的应用
题目链接 P5507 机关 题意简述 有12个旋钮,每个旋钮开始时处于状态 \(1\) ~ \(4\) ,每次操作可以往规定方向转动一个旋钮 (\(1\Rightarrow2\Rightarrow ...
- 洛谷 P1379 八数码难题(map && 双向bfs)
题目传送门 解题思路: 一道bfs,本题最难的一点就是如何储存已经被访问过的状态,如果直接开一个bool数组,空间肯定会炸,所以我们要用另一个数据结构存,STL大法好,用map来存,直接AC. AC代 ...
- BFS、双向BFS和A*
BFS.双向BFS和A* Table of Contents 1. BFS 2. 双向BFS 3. A*算法 光说不练是无用的.我们从广为人知的POJ 2243这道题谈起:题目大意:给定一个起点和一个 ...
- HDOJ2579,BFS(三维数组标记)
三维数组对于我这个萌萌的新手来说真是酷酷的,帅到不行,,, 三维数组前面还看到空间的哪一题HDOJ1240,这一题时间上的标记,更酷了!!! 不说废话了,贴一发代码: #include <std ...
随机推荐
- python __str__repr__ 区别
__str__ __repr__ 两个内置函数都是调试常用的函数, 对象直接调用时会调用 __repr__的内容, __str__需要print一下对象才可以 两个函数的内容有时会写成相同内容 _ ...
- python os 常用命令
转载:http://www.cnblogs.com/kaituorensheng/archive/2013/03/18/2965766.html python编程时,经常和文件.目录打交道,这是就离不 ...
- OGG-01161
Bad column index (35) specified for table user.table_name, max columns = 35. 原因:源端表结构发生了变更 解决办法:1.如果 ...
- git 操作遇到的问题与解决方法
一.使用git在本地创建一个项目的过程,Git 上传本地文件到github $ makdir ~/hello-world //创建一个项目hello-world $ cd ~/hello-world ...
- 网络安全意识有多重要?SamSam勒索软件敲诈了近600万美元
近年来,对于网络犯罪分子来说,勒索软件已成为数百万美元的黑市业务,SamSam就是一个很好的例子. 中国信息安全新研究显示,自2015年12月以来,SamSam勒索软件从受害者手中敲诈了近600万美元 ...
- zabbix agentd错误日志解决办法
公司新上了一台服务器,我安装了zabbix_agents软件包,并复制了zabbix server端的zabbix_agentd.conf到/etc/zabbix里面并修改了相关的参数,并启动了zab ...
- pandas.Series函数用法
class pandas.Series(data=None, index=None, dtype=None, name=None, copy=False, fastpath=False) e.g., ...
- 【leetcode】402. Remove K Digits
题目如下: 解题思路:我的方法是从头开始遍历num,对于任意一个num[i],在[i+1~len(num)-1]区间内找出离num[i]最近并且小于num[i]的数num[j],如果j-i <= ...
- MainRun
package Testlink; import java.io.IOException; public class MainRun { public static void main(String[ ...
- ThreadLocal学习资料
下面的这一段代码运行起来,就会发生线程安全问题: 启动两个线程,同时去修改 name 属性值. package com.liwei.thread; /** * 下面的代码演示了线程安全发生的由来 * ...