题意: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. 利用Map解决复杂业务

    遍历出题库表的题库名称和题库id,根据题目id即questionBankId获取 分组,即该题库题目总数,该题库题目正确数,该题库已回答题目数. <sqltemplate id="co ...

  2. HDU 5694——BD String——————【递归求解】

    BD String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  3. C#操作Redis String字符串

    /// <summary> /// Redis String 操作 /// </summary> public static void Redis_String() { Red ...

  4. Windows Server 2008系统中IE8启用和禁用JS

    Windows Server 2008系统中IE8默认是启用IE ESC(ie 增强)的,这样会导致该IE不支持JS,开启方法: 1.开始->管理工具->服务器管理器 2.点击服务器管理- ...

  5. 编写DBCP连接池

    #配置数据库数据源package com.itang.utils; import java.io.InputStream; import java.sql.Connection; import jav ...

  6. django基础一:web、wsgi、mvc、mtv

    一.web框架 web框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以快速开发特定的系统.他山之石,可以攻玉.python的所有web框架,都是对so ...

  7. Python 爬虫的集中简单方式

  8. 获取页面z-index最大值

    getMaxZIndex = function () { var maxZ = Math.max.apply(null, $.map($('body *'), function(e,n) { if ( ...

  9. 什么是APP???APP的开发类型又分哪几种???

    开发者们都知道在高端智能手机系统中有两种应用程序: 一种是基于本地(操作系统)运行的APP —-Native App: 一种是基于高端机的浏览器运行的App —-WebApp因为这些高端智能手机(Ip ...

  10. iview中upload组件上传图片,跨域

    前提:先前开发了一个A项目,A项目中有一套上传图片的接口,现在开发B项目. B项目开发中用iview中的upload组件上传图片,用到了A项目中上传接口,爬坑经历 1.涉及到了跨域解决:后台配置一下文 ...