poj2312 Battle City 【暴力 或 优先队列+BFS 或 BFS】
题意: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】的更多相关文章
- B - Battle City bfs+优先队列
来源poj2312 Many of us had played the game "Battle city" in our childhood, and some people ( ...
- POJ - 2312 Battle City BFS+优先队列
Battle City Many of us had played the game "Battle city" in our childhood, and some people ...
- poj 2312 Battle City【bfs+优先队列】
Battle City Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7579 Accepted: 2544 Des ...
- Battle City 优先队列+bfs
Many of us had played the game "Battle city" in our childhood, and some people (like me) e ...
- C - Battle City BFS+优先队列
Many of us had played the game "Battle city" in our childhood, and some people (like me) e ...
- POJ 2312:Battle City(BFS)
Battle City Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9885 Accepted: 3285 Descr ...
- poj 2312 Battle City
题目连接 http://poj.org/problem?id=1840 Battle City Description Many of us had played the game "Bat ...
- Battle City
Battle City Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7208 Accepted: 2427 Descr ...
- UVA - 1601 The Morning after Halloween (BFS/双向BFS/A*)
题目链接 挺有意思但是代码巨恶心的一道最短路搜索题. 因为图中的结点太多,应当首先考虑把隐式图转化成显式图,即对地图中可以相互连通的点之间连边,建立一个新图(由于每步不需要每个鬼都移动,所以每个点需要 ...
随机推荐
- 九度oj 1004 Median 2011年浙江大学计算机及软件工程研究生机试真题
题目1004:Median 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:14162 解决:3887 题目描述: Given an increasing sequence S of N i ...
- SpringMVC入门(二)
使用注解的方式进行Handler的开发 注意:此处只介绍和方式一不同的地方 1.注解的处理器适配器 在spring3.1之前使用org.springframework.web.servlet.m ...
- java并发编程(3)避免活跃性危险
活跃性危险 一.死锁 发生:每个人都不愿意放弃自己的锁,确想要别人的锁,这就会导致死锁 1.锁顺序死锁:如果每个线程以固定的顺序获取锁,那么至少在程序中不会出现锁顺序导致的死锁: 因为顺序固定如:所 ...
- bzoj 3064: Tyvj 1518 CPU监控
Description 1.区间加 \(z\) 2.区间覆盖为 \(z\) 3.查询区间最大值 4.查询区间历史最大值 Solution 线段树维护历史最值,思想大致是维护标记出现过的最大值 考虑这种 ...
- Hadoop实战之二~ hadoop作业调度详解(1)
对Hadoop的最感兴趣的地方,也就在于Hadoop的作业调度了,在正式介绍如何搭建Hadoop之前,深入理解一下Hadoop的作业调度很有必要.我们不一定能用得上Hadoop,但是如果理通顺Hado ...
- JavaScript数组循环遍历之forEach
1. js 数组循环遍历. 数组循环变量,最先想到的就是 for(var i=0;i<count;i++)这样的方式了. 除此之外,也可以使用较简便的forEach 方式 2. forEac ...
- VBA将指定Excel表数据批量生成到另一个Excel表中,每个sheet表一行数据
Sub AutoInputValNewExcel() Dim sh1, sh2 As Worksheet Dim ws1, ws2 As Workbook ) ) ).Sheets() iRows = ...
- 解决Openwrt安装插件提示一下错误的办法
解决Openwrt安装插件提示一下错误的办法 Openwrt安装17ce插件,提示一下错误: Collected errors: * check_data_file_clashes: Package ...
- 深入理解jQuery插件开发总结(四)
jQuery插件开发模式 软件开发过程中是需要一定的设计模式来指导开发的,有了模式,我们就能更好地组织我们的代码,并且从这些前人总结出来的模式中学到很多好的实践. 根据<jQuery高级编程&g ...
- 第8章 CSS3中的变形与动画(上)
变形--旋转 rotate() 旋转rotate()函数通过指定的角度参数使元素相对原点进行旋转.它主要在二维空间内进行操作,设置一个角度值,用来指定旋转的幅度.如果这个值为正值,元素相对原点中心顺时 ...