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 ...
随机推荐
- SQL分页常用的两个存储过程
在做数据绑定时,我们常常会遇到分页事件,下面记录一下分页常用到SQL的两个存储过程 ①/****分页数据总数****//****** 对象: StoredProcedure [dbo].[GetRe ...
- PYTHON学习之路_PYTHON基础(6)
学习内容: Python模块介绍 1.time &datetime模块 2.random 3.shutil 4.shelve 5.xml处理 6.configparser 7.hashlib ...
- mongodb安装及基础命令
安装mongodb(mongodb-linux-x86_64-3.2.4.tgz)1 export PATH=$PATH:/usr/local/mongodb/bin2 /usr/local/mong ...
- C/C++头文件一览
C.传统 C++ #include <assert.h> //设定插入点 #include <ctype.h> //字符处理 #include <errno.h> ...
- 2.使用JDK开发webService
使用jdk开发webService需要注意:jdk版本必须1.6以及1.6以上! 以下webService的组成部分: server端和client端,通过服务器端(server)webService ...
- java - Stack栈和Heap堆的区别
首先分清楚Stack,Heap的中文翻译:Stack—栈,Heap—堆. 在中文里,Stack可以翻译为“堆栈”,所以我直接查找了计算机术语里面堆和栈开头的词语: 堆存储 ...
- SSH 动态端口forwarding是如何工作的
好久没有来了,实在是太懒. 经常用SSH的动态端口forwarding 来FQ,使用像这样的命令: ssh -D 9999 -f -C -q -N sshHost.somewhere.com 这个命令 ...
- [ZigBee] 7、ZigBee之UART剖析(ONLY串口发送)
综述:USART0和USART1是串行通信接口,它们能够分别运行于异步UART模式或者同步SPI 模式.两个USART具有同样的功能,可以设置在单独的I/O 引脚. 1.UART 模式 UART 模式 ...
- undefined function openssl_x509_read
打开php.ini,找到这一行 ;extension=php_openssl.dll,将前面的";"去掉 再重启apache或者iis即可
- Office 2016 正式发布——新特性预览
今天微软又发生了一件大事!Windows Office 2016正式发布,这标志着Windows Office 又达到了一个新的里程碑! 全新的Office 发布为Office 365 用户带来了新的 ...