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
随机推荐
- kuangbin_UnionFind C (HDU 1213)
过程模板 扫一下一共有几棵树 输出 #include <iostream> #include <string> #include <cstdio> #include ...
- compiler
http://www.lingcc.com/2012/05/16/12048/ a list of compiler books — 汗牛充栋的编译器参考资料 Posted on 2012年5月16日 ...
- webbrowser在不同的.netframework版本差异
这几在做一个浏览器的自动化下载的工具,发现自己做的demo和做的项目代码运行不一致,代码就那么几行,拷贝过去为什么有些行为就不一样呢?经过分析发现原来有.net4.0和.net2.0中的webbrow ...
- php 错误 Strict Standards: PHP Strict Standards: Declaration of .... should be compatible with that of 解决办法
错误原因:这是由于 php 5.3版本后.要求继承类必须在父类之后定义.否则就会出现Strict Standards: PHP Strict Standards: Declaration of ... ...
- SignalR的安装
介绍 SignalR 是 ASP.NET 团队正在开发的一个 Microsoft .NET Framework 库和 jQuery 插件,可能包括在以后版本的 ASP.NET 平台中. 它提供了一些前 ...
- C数据类型
结构体 因为数组中各元素的类型和长度都必须一致,以便于编译系统处理.为了解决这个问题,C语言中给出了另一种构造数据类型——“结构(structure)”或叫“结构体”.它相当于其它高级语言中的记录.“ ...
- 如何在asp.net中获取GridView隐藏列的值?
在阅读本文之前,我获取gridview某行某列的值一般做法是这样的:row.Cells[3].Text.ToString().有点傻瓜呵呵 在Asp.net 2.0中增加了一个新的数据绑定控件:Gri ...
- python之chardet库
chardet库是python的字符编码检测器,能够检测出各种编码的类型,例如: import chardet import urllib.request testdata = urllib.requ ...
- VB的if和elseif
VB中if和elseif的用法是: if...then...elseif...then...else...endif 切记在then的后面不要加冒号,加了冒号出现else没有if的错误,因为加了冒号表 ...
- Hibernate3回顾-2-相关概念
2.几个概念 HIbernate简要的体系结构如下图所示 通过上图能够发现HIbernate需要一个hibernate.properties文件,该文件用于配置Hibernate和数据库连接的信息.还 ...