题意: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. ubuntu安装甲骨文最新jdk7

    1.下载jdk7(我下载的是Java SE Platform(jdk) 7u51): http://www.oracle.com/technetwork/java/javase/downloads/i ...

  2. Maven 常见知识点整理

    认识 Maven Maven 的作用? 1.添加第三方jar包 2.解决jar包之间的依赖关系 3.获取第三方jar包 4.将项目拆成多个工程模块 Maven 是什么? 是Apache软件基金会组织维 ...

  3. [PY3]——内置数据结构(8)——解构与封装

    ### 解构的理解与用法 ### 解构是python很有特色的一个功能,被很多语言借鉴(例如ES6) # 元素按照顺序赋值给变量 In [31]: lst=list(range(5)) In [32] ...

  4. [转].NET Core dotnet 命令大全

    本文转自:http://www.cnblogs.com/linezero/p/dotnet.html https://docs.microsoft.com/en-us/dotnet/articles/ ...

  5. JavaScript中Undefined 和 Null的区别

    Undefined 这个值表示变量不含有值. 可以通过将变量的值设置为 null 来清空变量. 例如: <script> var person; var car="Volvo&q ...

  6. [javaEE] 三层架构案例-用户模块(二)

    使用junit测试框架,测试查找用户和添加用户功能 com.tsh.test.xmlUserDaoTest package com.tsh.test; import org.junit.Test; i ...

  7. 使用itext导出pdf

    导出pdf这个功能是在工作中遇到的,写这个功能的时候遇到了不少的问题,比如中文乱码,不显示的问题,这些问题在我不断的测试,研究后都一一解决了. 第一步,先导入所需要的jar包 第一个jar包是用于解决 ...

  8. centos7安装java开发环境

    一. 安装jdk 1.进入oracle官网下载jdk-8u152-linux-x64.tar.gz,用WinScp将文件上传到/usr/local文件下 2.解压:执行命令 tar –xzvf jdk ...

  9. 二分查找——Python实现

    一.排序思想 二分(折半)查找思想请参见:https://www.cnblogs.com/luomeng/p/10585291.html 二.python实现 def binarySearchDemo ...

  10. Spring_Spring与IoC_第一个程序

    一.IoC IoC是一种概念,是一种思想,指将传统上由程序代码直接操控的对象调用权交给容器,通过容器来实现对象的装配和管理.控制反转是对对象控制权的转移,从程序代码本身反转到外部容器. 当前IoC比较 ...