POJ 2312Battle City(BFS-priority_queue 或者是建图spfa)
/*
bfs搜索!要注意的是点与点的权值是不一样的哦!
空地到空地的步数是1, 空地到墙的步数是2(轰一炮+移过去)
所以用到优先队列进行对当前节点步数的更新!
*/
#include<iostream>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std; int n, m;
char map[][]; struct node{
int x, y;
int step;
node(){}
node(int x, int y, int step){
this->x=x;
this->y=y;
this->step=step;
}
};
int dir[][]={, , , , -, , , -}; bool operator >(node a, node b){
return a.step > b.step;
} priority_queue<node, vector<node>, greater<node> >q; bool bfs(){
while(!q.empty()){
node cur=q.top();
q.pop();
if(map[cur.x][cur.y]=='T'){
cout<<cur.step<<endl;
return true;
}
int xx, yy;
for(int i=; i<; ++i){
xx=cur.x+dir[i][];
yy=cur.y+dir[i][];
if(map[xx][yy]=='R' || map[xx][yy]=='S') continue;
else if(map[xx][yy]=='T'){
cout<<cur.step+<<endl;
return true;
}
else if(map[xx][yy]=='B')
q.push(node(xx, yy, cur.step+));
else
q.push(node(xx, yy, cur.step+)); map[xx][yy]='R';
}
}
return false;
} int main(){
while(cin>>n>>m && (n || m)){
for(int i=; i<=n; ++i){
cin>>(map[i]+);
map[i][]=map[i][m+]='R';
for(int j=; j<=m; ++j){
if(map[i][j]=='Y'){
q.push(node(i, j, ));
map[i][j]='R';
}
map[][j]=map[n+][j]='R';
}
}
if(!bfs())
cout<<"-1"<<endl;
while(!q.empty()) q.pop();
}
return ;
}
/*
将map[i][j]映射到 i*m+j的节点上,建立节点与节点之间的权值的关系!
B->B的权值为1, E->B的权值为2, S<->... R<->... 的权值为INF(也就是没有边存在)
在注意一点就是B->E的权值是 1,因为如果到B了,说明炮弹已经将墙轰掉了! 建立好图之后,那么就是求源点到终点的最短的距离了!
这里采用的spfa算法!
*/ #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#define N 90010
#define INF 0x3f3f3f3f
using namespace std;
struct node{
int to;
int dist;
node(){} node(int to, int dist){
this->to=to;
this->dist=dist;
}
};
vector<node>g[N];
int vis[N], d[N];
char map[][];
int dir[][]={, , , , -, , , -};
int ss, tt;
int n, m;
queue<int>q;
bool spfa(){
q.push(ss);
memset(vis, , sizeof(vis));
vis[ss]=;
memset(d, 0x3f, sizeof(d));
d[ss]=;
while(!q.empty()){
int u=q.front(); q.pop();
vis[u]=;
int len=g[u].size();
for(int i=; i<len; ++i){
int v=g[u][i].to;
if(d[v] > d[u] + g[u][i].dist){
d[v] = d[u] + g[u][i].dist; if(!vis[v]){
q.push(v);
vis[v]=;
}
}
}
}
if(d[tt]==INF) return false;
return true;
} int main(){
while(cin>>n>>m && (n||m)){
for(int i=; i<n; ++i)
cin>>map[i];
for(int i=; i<n; ++i)
for(int j=; j<m; ++j){
int from=i*m+j;
if(map[i][j]=='Y') ss=from;
else if(map[i][j]=='T') tt=from;
else if(map[i][j]=='R' || map[i][j]=='S') continue;
for(int k=; k<; ++k){
int x=i+dir[k][];
int y=j+dir[k][];
if(x< || x>=n || y< || y>=m) continue;
if(map[x][y]=='R' || map[x][y]=='S') continue; int to = x*m+y, dist=;
if(map[i][j]=='B' || map[x][y]=='B') dist=;
if(map[i][j]=='B' && map[x][y]!='B') dist=;
g[from].push_back(node(to, dist)); }
}
if(!spfa())
cout<<"-1"<<endl;
else cout<<d[tt]<<endl;
for(int i=; i<n*m; ++i)
g[i].clear();
}
return ;
}
POJ 2312Battle City(BFS-priority_queue 或者是建图spfa)的更多相关文章
- poj 1149 PIGS【最大流经典建图】
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18727 Accepted: 8508 Description ...
- POJ 1386 Play on Words(单词建图+欧拉通(回)路路判断)
题目链接:http://poj.org/problem?id=1386 题目大意:给你若干个字符串,一个单词的尾部和一个单词的头部相同那么这两个单词就可以相连,判断给出的n个单词是否能够一个接着一个全 ...
- POJ A Plug for UNIX (最大流 建图)
Description You are in charge of setting up the press room for the inaugural meeting of the United N ...
- POJ 2226 Muddy Fields 二分图(难点在于建图)
题意:给定一个矩阵和它的N行M列,其中有一些地方有水,现在有一些长度任意,宽为1的木板,要求在板不跨越草,用一些木板盖住这些有水的地方,问至少需要几块板子? 思路:首先想到如果没有不准跨越草的条件则跟 ...
- Antenna Placement POJ - 3020 二分图匹配 匈牙利 拆点建图 最小路径覆盖
题意:图没什么用 给出一个地图 地图上有 点 一次可以覆盖2个连续 的点( 左右 或者 上下表示连续)问最少几条边可以使得每个点都被覆盖 最小路径覆盖 最小路径覆盖=|G|-最大匹配数 ...
- TTTTTTTTTTTTTTTTT POJ 2226 草地覆木板 二分匹配 建图
Muddy Fields Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9754 Accepted: 3618 Desc ...
- TTTTTTTTTTTTTTTTTT POJ 2724 奶酪消毒机 二分匹配 建图 比较难想
Purifying Machine Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5004 Accepted: 1444 ...
- 图论--差分约束--POJ 3169 Layout(超级源汇建图)
Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 < ...
- NOIP2013 华容道 (棋盘建图+spfa最短路)
#include <cstdio> #include <algorithm> #include <cstring> #include <queue> # ...
- Invitation Cards(邻接表+逆向建图+SPFA)
Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 17538 Accepted: 5721 Description In ...
随机推荐
- 第55讲:Scala中Infix Type实战详解
今天学习了Infix type的知识,来看看实战代码: def main(args:Array[String]){ object log { def >>:(data:String) ...
- day12---python mysql pymsql sqlalchemy ORM
RDBMS 术语 在我们开始学习MySQL 数据库前,让我们先了解下RDBMS的一些术语: 数据库: 数据库是一些关联表的集合.. 数据表: 表是数据的矩阵.在一个数据库中的表看起来像一个简单的电子表 ...
- PeerConnection
Example(摘) /*When two peers decide they are going to set up a connection to each other, they both go ...
- wireshark常用过滤规则
wireshark常用过滤规则:(Filter中输入过滤规则)1.源ip过滤:ip.src==1.1.1.1 (过滤源ip为1.1.1.1的包) 2.目的ip过滤:ip.d ...
- 基于java代码的Spring-mvc框架配置
Spring 版本 4.3.2 maven项目 1.首先上项目目录图,主要用到的配置文件,略去css和js的文件 引包: 2.主要代码: (1)NetpageWebAppInitializer类 ...
- 使用开源库MAGICODES.WECHAT.SDK进行微信公众号支付开发
概要 博客使用Word发博,发布后,排版会出现很多问题,敬请谅解.可加群获取原始文档. 本篇主要讲解微信支付的开发流程,相关业务基于MAGICODES.WECHAT.SDK实现.通过本篇教程,您可以很 ...
- HTML5之Canvas时钟(网页效果--每日一更)
今天,带来的是使用HTML5中Canvas标签实现的动态时钟效果. 话不多说,先看效果:亲,请点击这里 众所周知,Canvas标签是HTML5中的灵魂,HTML5 Canvas是屏幕上的一个由Java ...
- Programming Entity Framework CodeFirst -- 约定和属性配置
以下是EF中Data Annotation和 Fluenlt API的不同属性约定的对照. Length Data Annotation MinLength(nn) MaxLength(nn) ...
- mac下apache配置,解决It is not safe to rely on the system's timezone settings.
之前一直转windows平台下做php,很少遇到问题.现在有了macbook,还在慢慢的熟悉中,搭建php开发环境,熟悉mac系统文档组织还有命令,颇费功夫. 今天我在mac下做一个php的练习,用到 ...
- nanoTime对volatile 测试的一种写法
今天脑筋有点搭牢,想了半天才看出为什么以下两段代码效果是相同的... 第一种好处是可以直接批量复制黏贴system.out, 不用改什么东西 private static long i; priv ...