nyoj 284 坦克大战 (优先队列)
题目链接:http://acm.nyist.net/JudgeOnline/status.php?pid=284
特殊数据:
5 5
BBEEYEEERBSSERBSSERBSSETB#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<queue>
using namespace std;
int e1,e2,m,n;
int mapp[][];
int vis[][];
int dir[][]={-,,,,,,,-};
int ans;
struct T
{
int x,y;
}now,eed;
int bfs(int s1,int s2)
{
queue< T >aa;
while(!aa.empty()) aa.pop();
now.x=s1;now.y=s2;
aa.push(now);
while(!aa.empty())
{
eed=aa.front();
aa.pop();
if(eed.x==e1 && eed.y==e2)
{
if(ans>vis[eed.x][eed.y])
ans=vis[eed.x][eed.y];
}
for(int i=;i<;i++)
{
now.x=eed.x+dir[i][];now.y=eed.y+dir[i][];
//cout<<now.x<<" "<<now.y<<" "<<endl;
if( mapp[now.x][now.y]!= )
{
if(vis[now.x][now.y]==)
{ vis[now.x][now.y]=mapp[now.x][now.y]+vis[eed.x][eed.y]; aa.push(now); }
else if(mapp[now.x][now.y]+vis[eed.x][eed.y]<vis[now.x][now.y])
{
vis[now.x][now.y]= mapp[now.x][now.y]+vis[eed.x][eed.y] ;
aa.push(now);
}
}
}
}
return ans;
//5 5
//BBEEY
//EEERB
//SSERB
//SSERB
//SSETB
}
int main()
{
int i,j,k,t,s1,s2;
char s;
while(cin>>m>>n && m+n)
{ans=;
memset(mapp,,sizeof(mapp));
memset(vis,,sizeof(vis));
for(int i=;i<=m;i++)
for(int j=;j<=n;j++)
{
cin>>s;
if(s=='Y')
{s1=i;s2=j;}
else if(s=='T')
{e1=i;e2=j; mapp[i][j]=;}
else if(s=='B')
mapp[i][j]=;
else if(s=='E')
mapp[i][j]=;
else mapp[i][j]=;
}
int mm=bfs(s1,s2);
if(mm<)
cout<<ans<<endl;
else cout<<-<<endl;
}
return ;
}
优先队列:
#include<queue>
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char map[][];
int vis[][],m,n;
int dir[][] = {,,-,,,-,,};
int sx,sy,ex,ey;
struct T
{
int x,y,step; }now,eed;
bool operator<( T a, T b ){
return a.step>b.step;
}
priority_queue<T>pp;
bool judge(int x,int y)
{
if(x< || x>m || y< || y>n || vis[x][y]== || map[x][y]=='S' || map[x][y]=='R')
return false; return true;
}
int bfs()
{
while(!pp.empty()) pp.pop();
now.x = sx; now.y = sy; now.step = ;
vis[now.x][now.y]=;
pp.push(now);
while(!pp.empty())
{
eed = pp.top();
pp.pop();
// printf("%d %d \n",eed.x,eed.y);
if(eed.x == ex && eed.y==ey)
return eed.step; for(int i=;i<;i++)
{
now.x = eed.x+dir[i][];
now.y = eed.y+dir[i][];
if(judge(now.x ,now.y))
{
if(map[now.x][now.y] == 'B')
now.step = eed.step+;
else
now.step = eed.step+;
vis[now.x][now.y]= ;
pp.push(now);
}
}
}
return -; }
int main()
{
while(scanf("%d %d",&m,&n) && m+n)
{
memset(vis,,sizeof(vis));
for(int i =;i<=m;i++)
for(int j =;j<=n; j++)
{
scanf(" %c",&map[i][j]);
if(map[i][j] == 'Y')
{
sx = i;sy = j;
}
if(map[i][j] == 'T')
{
ex = i;ey = j;
}
}
int ans = bfs();
printf("%d\n",ans);
}
return ;
}
nyoj 284 坦克大战 (优先队列)的更多相关文章
- nyoj 284 坦克大战 简单搜索
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=284 题意:在一个给定图中,铁墙,河流不可走,砖墙走的话,多花费时间1,问从起点到终点至少 ...
- NYOJ 284 坦克大战 【BFS】+【优先队列】
坦克大战 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描写叙述 Many of us had played the game "Battle city" ...
- NYOJ 284 坦克大战 bfs + 优先队列
这类带权的边的图,直接广搜不行,要加上优先队列,这样得到的结果才是最优的,这样每次先找权值最小的,代码如下 #include <stdio.h> #include <iostream ...
- NYOJ 284 坦克大战 (广搜)
题目链接 描述 Many of us had played the game "Battle city" in our childhood, and some people (li ...
- java制作简单的坦克大战
坦克大战是我们小时候玩红白机时代的经典游戏,看到有不少小伙伴都使用各种语言实现了一下,手痒痒,也使用java做的一个比较简单的坦克大战,主要面向于学过Java的人群,与学了一段时间的人,有利于面向对象 ...
- 3D坦克大战游戏源码
3D坦克大战游戏源码,该游戏是基于xcode 4.3,ios sdk 5.1开发.在xcode4.3.3上完美无报错.兼容ios4.3-ios6.0 ,一款ios平台上难得的3D坦克大战游戏源码,有2 ...
- 【blade04】用面向对象的方法写javascript坦克大战
前言 javascript与程序的语言比如C#或者java不一样,他并没有“类”的概念,虽然最新的ECMAScript提出了Class的概念,我们却没有怎么用 就单以C#与Java来说,要到真正理解面 ...
- 3D坦克大战游戏iOS源码
3D坦克大战游戏源码,该游戏是基于xcode 4.3,ios sdk 5.1开发.在xcode4.3.3上完美无报错.兼容ios4.3-ios6.0 ,一款ios平台上难得的3D坦克大战游戏源码,有2 ...
- 坦克大战,看你能坚持几秒 ~~Duang~~Duang
闲来无事,写了一个坦克大战的小游戏,打开页面就能看到源码,代码还没有来得及整理.大家闲来玩玩吧,看谁玩的时间长! http://xiaohaibaomu.com/home/index
随机推荐
- socket模块
1 1.1 server: #!/use/local/env python# -*- coding:utf-8 -*- import socket ip_port = ('127.0.0.1', 99 ...
- 学习笔记之 初试Linux遇到的问题 2015-10-13
1. 安装.deb文件,用sudo gdebi XXX.deb sudo apt-get install xxx 2. 需要配置系统路径: LD_LIBRARY_PATH=.../lib:LD_LIB ...
- note: declarations in dependent base ‘std::basic_ios<char>’ are not found by unqualified lookup
错误信息如题所示. 修改前: template< typename _CharT, typename _Traits = std::char_traits<_CharT> > ...
- CentOS下添加新硬盘
1.查看新硬盘 #fdisk –l 新添加的硬盘的编号为/dev/sdb 2.硬盘分区 1)进入fdisk模式 #/sbin/fdisk /dev/sdb 2)输入n进行分 ...
- JS页面间传值
一:JavaScript静态页面值传递之URL篇 能过URL进行传值.把要传递的信息接在URL上. 例子: 参数传出页面Post.htm—> <input type="tex ...
- ABBYY FineReader 12最新官方版下载
ABBYY FineReader是市场领先的文字识别(OCR)软件,可快速方便地将扫描纸质文档.PDF文件和数码相机的图像转换成可编辑.可搜索的信息,ABBYY FineReader 12是目前最新版 ...
- SQLServer中临时表与表变量的区别分析
临时表 临时表与永久表相似,只是它的创建是在Tempdb中,它只有在一个数据库连接结束后或者由SQL命令DROP掉,才会消失,否则就会一直存在.临时表在创建的时候都会产生SQL Server的系统日志 ...
- xfs文件系统
引用http://blog.chinaunix.net/uid-522675-id-4665059.html xfs文件系统使用总结 1.3 xfs相关常用命令xfs_admin: 调整 xfs 文件 ...
- linux node&& npm 安装方式
1. 编译好的文件 简单说就是解压后,在bin文件夹中已经存在node以及npm,如果你进入到对应文件的中执行命令行一点问题都没有,不过不是全局的,所以将这个设置为全局就好了. cd node-v0 ...
- mysql四种事务隔离级的说明
·未提交读(Read Uncommitted):允许脏读,也就是可能读取到其他会话中未提交事务修改的数据 ·提交读(Read Committed):只能读取到已经提交的数据.Oracle等多数数据库默 ...