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*)
题目链接 挺有意思但是代码巨恶心的一道最短路搜索题. 因为图中的结点太多,应当首先考虑把隐式图转化成显式图,即对地图中可以相互连通的点之间连边,建立一个新图(由于每步不需要每个鬼都移动,所以每个点需要 ...
随机推荐
- [shell基础]——变量
变量的赋值 #定义变量,注意等号两边没有任何空格 variable=#定义环境变量export variable= #双引号:可含空格.可转义特殊字符 variable=" " # ...
- 判断img图片是否加载成功
上班之余,记录一下工作中遇到的有趣问题... 事情是这样的...在做一个内嵌H5的app时,有一个“个人名片”页面...要求:如果后台接口有给头像的图片链接就用他们给的,如果没给,前端给个默认头像.. ...
- chmod修改文件的权限/chown修改文件和目录的所有者(转)
ll指令的显示的信息为(当前目录下只有nameservice1一个目录): drwxr-xr-x 3 hdfs hdfs 4096 4月 14 16:19 nameservice1 上述信息分别表示: ...
- 实例化geoserver wms服务
var vectorWmsJHdataLayer = new ol.layer.Tile({ source: new ol.source.TileWMS({ //地址 url:'http://loca ...
- canal —— 阿里巴巴mysql数据库binlog的增量订阅&消费组件
阿里巴巴mysql数据库binlog的增量订阅&消费组件canal ,转载自 https://github.com/alibaba/canal 最新更新 canal QQ讨论群已经建立,群号 ...
- svn 创建本地仓库
1. svnadmin create ~/repository 2. svnserve -d -r ~/repository 3. svn checkout file://~/repository $ ...
- C Primer Plus note5
error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token| 遇到这种情况,不要看这里显示了三个错误,就很着急.静 ...
- mysql 去除重复数据
1. 问题描述 有时load或者insert操作导致 表数据有重复 2. 解决方案 通过临时表.主键id.倒腾去重 示例 2.1 create table student( name varchar ...
- FFT板子
woc......FFT这玩意儿真坑...... 一上午除了打了几遍板子什么也没干......真是废了...... 你要加油啊...... #include<cstdio> #includ ...
- ajax方法XHR.readyState五种状态与示例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...