题意:M行N列的矩阵。Y:起点,T:终点。S、R不能走,走B花费2,走E花费1.求Y到T的最短时间。

三种解法。♪(^∇^*)

//解法一:暴力

//157MS
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<cmath>
#include<queue>
#include<limits.h>
#define CLR(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+;
const int N = ;
int a[N][N], b[N][N];
char s[N][N];
int m, n; int main() {
int i, j;
while(~scanf("%d%d", &m, &n), n || m) {
getchar();
int sx, sy, ex, ey;
for(i = ; i < m; ++i) scanf("%s", s[i]);
for(i = ; i < m; ++i) {
for(j = ; j < n; ++j) {
if(s[i][j] == 'R' || s[i][j] == 'S')
a[i][j] = inf;
else if(s[i][j] == 'B')
a[i][j] = ;
else a[i][j] = ;
if(s[i][j] == 'Y') {sx = i; sy = j;}
else if(s[i][j] == 'T') {ex = i; ey = j;}
}
}
CLR(b, inf);
b[sx][sy] = ;
while() {
bool f = ;
for(i = ; i < m; ++i) {
for(j = ; j < n; ++j) {
if(b[i][j] == inf) continue;
if(i > && b[i-][j] > b[i][j] + a[i-][j]) {
b[i-][j] = b[i][j] + a[i-][j];
f = ;
}
if(i < m- && b[i+][j] > b[i][j] + a[i+][j]) {
b[i+][j] = b[i][j] + a[i+][j];
f = ;
}
if(j > && b[i][j-] > b[i][j] + a[i][j-]) {
b[i][j-] = b[i][j] + a[i][j-];
f = ;
}
if(j < n- && b[i][j+] > b[i][j] + a[i][j+]) {
b[i][j+] = b[i][j] + a[i][j+];
f = ;
}
}
}
if(!f) break;
}
if(b[ex][ey] != inf) printf("%d\n", b[ex][ey]);
else printf("-1\n");
}
return ;
}

//解法二:bfs+优先队列

//16MS
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<cmath>
#include<queue>
#include<limits.h>
#define CLR(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+;
const int N = ;
struct node {
int x, y;
int w;
node(int x = , int y = , int w = ):x(x),y(y),w(w){}
bool operator < (const node&r) const {
return w > r.w;
}
};
char s[N][N];
bool vis[N][N];
int dx[] = {,,-,};
int dy[] = {,,,-};
int m, n;
int ans;
int sx, sy;
bool check(int x, int y) {
if(x < || x >= m || y < || y >= n)
return false;
if(s[x][y] == 'S' || s[x][y] == 'R' || vis[x][y])
return false;
return true;
}
int bfs() {
CLR(vis, );
priority_queue<node> q;
q.push(node(sx, sy, ));
vis[sx][sy] = ;
while(!q.empty()) {
node t = q.top(); q.pop();
//vis[t.x][t.y] = 0;
if(s[t.x][t.y] == 'T') {
ans = t.w;
return true;
}
for(int i = ; i < ; ++i) {
int x = t.x + dx[i];
int y = t.y + dy[i];
if(check(x, y)) {
int w = t.w + ;
if(s[x][y] == 'B') w++;
vis[x][y] = ;
q.push(node(x, y, w));
}
}
}
return false;
}
int main() {
int i, j;
while(~scanf("%d%d", &m, &n), n || m) {
getchar();
for(i = ; i < m; ++i) scanf("%s", s[i]);
for(i = ; i < m; ++i) {
for(j = ; j < n; ++j) {
if(s[i][j] == 'Y') {sx = i; sy = j; break;}
}
}
if(bfs()) printf("%d\n", ans);
else printf("-1\n");
}
return ;
}

//解法三:bfs,不用优先队列,搜到B时,把它变换成E再放入队列,重新取下一个队首继续搜索,这样就不用优先队列,用普通队列也能做啦

//32MS
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<cmath>
#include<queue>
#include<limits.h>
#define CLR(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+;
const int N = ;
struct node {
int x, y;
int w;
node(int x = , int y = , int w = ):x(x),y(y),w(w){}
};
char s[N][N];
bool vis[N][N];
int dx[] = {,,-,};
int dy[] = {,,,-};
int m, n;
int ans;
int sx, sy;
bool check(int x, int y) {
if(x < || x >= m || y < || y >= n)
return false;
if(s[x][y] == 'S' || s[x][y] == 'R' || vis[x][y])
return false;
return true;
}
int bfs() {
CLR(vis, );
queue<node> q;
q.push(node(sx, sy, ));
vis[sx][sy] = ;
while(!q.empty()) {
node t = q.front(); q.pop();
if(s[t.x][t.y] == 'T') {
ans = t.w;
return true;
}
if(s[t.x][t.y] == 'B') {
t.w++;
s[t.x][t.y] = 'E';
q.push(t);
continue;
}
for(int i = ; i < ; ++i) {
int x = t.x + dx[i];
int y = t.y + dy[i];
if(check(x, y)) {
int w = t.w + ;
vis[x][y] = ;
q.push(node(x, y, w));
}
}
}
return false;
}
int main() {
int i, j;
while(~scanf("%d%d", &m, &n), n || m) {
getchar();
for(i = ; i < m; ++i) scanf("%s", s[i]);
for(i = ; i < m; ++i) {
for(j = ; j < n; ++j) {
if(s[i][j] == 'Y') {sx = i; sy = j; break;}
}
}
if(bfs()) printf("%d\n", ans);
else printf("-1\n");
}
return ;
}

poj2312 Battle City 【暴力 或 优先队列+BFS 或 BFS】的更多相关文章

  1. B - Battle City bfs+优先队列

    来源poj2312 Many of us had played the game "Battle city" in our childhood, and some people ( ...

  2. POJ - 2312 Battle City BFS+优先队列

    Battle City Many of us had played the game "Battle city" in our childhood, and some people ...

  3. poj 2312 Battle City【bfs+优先队列】

      Battle City Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7579   Accepted: 2544 Des ...

  4. Battle City 优先队列+bfs

    Many of us had played the game "Battle city" in our childhood, and some people (like me) e ...

  5. C - Battle City BFS+优先队列

    Many of us had played the game "Battle city" in our childhood, and some people (like me) e ...

  6. POJ 2312:Battle City(BFS)

    Battle City Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9885   Accepted: 3285 Descr ...

  7. poj 2312 Battle City

    题目连接 http://poj.org/problem?id=1840 Battle City Description Many of us had played the game "Bat ...

  8. Battle City

    Battle City Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7208   Accepted: 2427 Descr ...

  9. UVA - 1601 The Morning after Halloween (BFS/双向BFS/A*)

    题目链接 挺有意思但是代码巨恶心的一道最短路搜索题. 因为图中的结点太多,应当首先考虑把隐式图转化成显式图,即对地图中可以相互连通的点之间连边,建立一个新图(由于每步不需要每个鬼都移动,所以每个点需要 ...

随机推荐

  1. android studio Cannot resolve symbol '@drawable/XXX'等问题解决办法

    方法1."Build " -> "Clean project" 方法 2."Tools" -> "Android&qu ...

  2. vue中添加favicon.ico

    1.首先将favicon.ico图片放在根目录下 2.修改webpack配置文件 1)找到build下的webpack.dev.conf.js文件 new HtmlWebpackPlugin({ fi ...

  3. C#泛型List的介绍

    一.List<T>描述 1).表示可通过索引访问的对象的强类型列表:提供用于对列表进行搜索.排序和操作的方法.2).是ArrayList类的泛型等效类.3).可以使用一个整数索引访问此集合 ...

  4. thinkjs crud练习

    今天看了下thinkjs(v2.1.7),做了一个简单的入门demo,基于mysql数据库增删改查,详细源码如下: 页面整体展示: 会员新增: 删除: 查询: 主页面: <!DOCTYPE ht ...

  5. 简单来看看JavaBean

    1.什么是JavaBean? JavaBean是一个遵循特定写法的java类. 用作JavaBean的类必须有一个公共的,无参数的构造方法. JavaBean的属性与普通的Java类的属性的概念一样, ...

  6. springMVC基于注解的控制器

    springMVC基于注解的控制器 springMVC基于注解的控制器的优点有两个: 1.控制器可以处理多个动作,这就允许将相关操作写在一个类中. 2.控制器的请求映射不需要存储在配置文件中.使用re ...

  7. Python入门-初始函数

    今天让我们来初步认识一个在python中非常重要的组成部分:函数 首先,让我们来幻想这样一个场景: 比如说我们现在想要通过社交软件约一个妹子,步骤都有什么? print('打开手机 ') print( ...

  8. 2d动画开发之PIXI开发

    简单的移动小游戏只要引入pixi.min.js就可以, 如果要用spine动画(龙骨也支持导出spine格式的)就要引入pixi-spine.js 如果还有声音的支持引入pixi-sound.js 学 ...

  9. awk日志分析

    前言 今天我们来讲讲如何用awk进行网站日志分析,得到页面平均耗时排行 文件 [xingxing.dxx@30_28_6_20 ~]$ cat logs /Oct/::: +] GET /pages/ ...

  10. ANN神经网络——实现异或XOR (Python实现)

    一.Introduction Perceptron can represent AND,OR,NOT 用初中的线性规划问题理解 异或的里程碑意义 想学的通透,先学历史! 据说在人工神经网络(artif ...