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 ...
随机推荐
- javascript练习-扑克牌
下面用枚举类型来实现一副扑克牌的类: //定义一个玩牌的类 function Card(suit,rank){ function inherit(p){ if(p==null) throw TypeE ...
- Windows 10 for phone 离我们不远了
今天登录Windows Insider终于看到更多机型可以更新预览版了,看来Windows phone 10离我们不远了!
- datax+hadoop2.X兼容性调试
以hdfsreader到hdfswriter为例进行说明: 1.datax的任务配置文件里需要指明使用的hadoop的配置文件,在datax+hadoop1.X的时候,可以直接使用hadoop1.X/ ...
- Javac 手动编译时,出现乱码或编码格式问题
使用Javac进行手动编译时,出现乱码或编码格式问题,原因如下:现象:编译时出现乱码或编译错误 即使改成UTF-8仍然会出错 原因如下:某些编辑器会往utf8文件中添加utf8标记(editplus称 ...
- C2第十次解题报告
看过题解后如果觉得还算有用,请帮忙加点我所在团队博客访问量 http://www.cnblogs.com/newbe/ http://www.cnblogs.com/ne走迷宫wbe/p/406983 ...
- debian/ubuntu 下ISE安装
1. planAhead无法打开的问题 原因: debian中使用dash,跟planAhead使用的bash略有不同 解决: 将/bin/sh 的链接从dash改为bash 2. FPGA Edit ...
- UIWebView和Js交互
在日常的ios项目开发中,我们经常会在原生应用中嵌入web页面,通常我们只是进行一个展示,没有其它的一些功能.但是也有一些项目中需要web页面中的html和native进行交互.但是ios sdk 并 ...
- linux安装oracle 11g rac
安装oracle 11gR2 RAC 一.网络规划及安装虚拟主机 主机名 主机版本 Ip rac1.localdomain Redhat 6.5 RAC节点1 192.168.100.11 rac2. ...
- php+redis window
http://download.csdn.net/detail/qwfy326/6572443 wampserver2.2e-php5.3.13-httpd2.2.22-mysql5.5.24-x64
- sql 优化 链接提示 查询提示 标提示
SQL Server的查询优化器在select查询执行的时候产生一个高效的查询执行计划.如果优化器不能选择最优的计划,那么就需要检查查询计划.统计信息.支持的索引等,而通过使用提示可以改变优化器选择查 ...