题目链接:http://acm.nyist.net/JudgeOnline/status.php?pid=284

特殊数据:

5 5

BBEEY
EEERB
SSERB
SSERB
SSETB
7
非优先队列:
 #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 坦克大战 (优先队列)的更多相关文章

  1. nyoj 284 坦克大战 简单搜索

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=284 题意:在一个给定图中,铁墙,河流不可走,砖墙走的话,多花费时间1,问从起点到终点至少 ...

  2. NYOJ 284 坦克大战 【BFS】+【优先队列】

    坦克大战 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描写叙述 Many of us had played the game "Battle city" ...

  3. NYOJ 284 坦克大战 bfs + 优先队列

    这类带权的边的图,直接广搜不行,要加上优先队列,这样得到的结果才是最优的,这样每次先找权值最小的,代码如下 #include <stdio.h> #include <iostream ...

  4. NYOJ 284 坦克大战 (广搜)

    题目链接 描述 Many of us had played the game "Battle city" in our childhood, and some people (li ...

  5. java制作简单的坦克大战

    坦克大战是我们小时候玩红白机时代的经典游戏,看到有不少小伙伴都使用各种语言实现了一下,手痒痒,也使用java做的一个比较简单的坦克大战,主要面向于学过Java的人群,与学了一段时间的人,有利于面向对象 ...

  6. 3D坦克大战游戏源码

    3D坦克大战游戏源码,该游戏是基于xcode 4.3,ios sdk 5.1开发.在xcode4.3.3上完美无报错.兼容ios4.3-ios6.0 ,一款ios平台上难得的3D坦克大战游戏源码,有2 ...

  7. 【blade04】用面向对象的方法写javascript坦克大战

    前言 javascript与程序的语言比如C#或者java不一样,他并没有“类”的概念,虽然最新的ECMAScript提出了Class的概念,我们却没有怎么用 就单以C#与Java来说,要到真正理解面 ...

  8. 3D坦克大战游戏iOS源码

    3D坦克大战游戏源码,该游戏是基于xcode 4.3,ios sdk 5.1开发.在xcode4.3.3上完美无报错.兼容ios4.3-ios6.0 ,一款ios平台上难得的3D坦克大战游戏源码,有2 ...

  9. 坦克大战,看你能坚持几秒 ~~Duang~~Duang

    闲来无事,写了一个坦克大战的小游戏,打开页面就能看到源码,代码还没有来得及整理.大家闲来玩玩吧,看谁玩的时间长! http://xiaohaibaomu.com/home/index

随机推荐

  1. ndk-build出错,错误以及解决办法如下

    用NDK编译jni目录下的文件了,我的系统是Ubuntu10.04,NDK版本是android-ndk-r7b. 切换到工程的jni目录下执行:ndk-build(ndk-build的路径已经添加到系 ...

  2. [POI 2008][BZOJ 1132]Tro

    这题我真是无能为力了 这题的做法还是挺简单的 枚举左下角的点做为原点,把其余点按极角排序    PS.是作为原点,如枚举到 k 时,对于所有 p[i] (包括p[k]) p[i]-=p[k] (此处为 ...

  3. 论文笔记之:Pedestrian Detection aided by Deep Learning Semantic Tasks

    Pedestrian Detection aided by Deep Learning Semantic Tasks CVPR 2015 本文考虑将语义任务(即:行人属性和场景属性)和行人检测相结合, ...

  4. JNI学习2:android 调用C语言方法与C语言调用android方法

    #include <jni.h> #include <stdio.h> #include <stdlib.h> #include <jni.h> #in ...

  5. __flash__removeCallback 未定义错误

    使用swfupload作为上传组件,artdialog作为弹出窗口,在关闭弹出窗口时,出现"__flash__removeCallback"未定义错误.而且是关了又出现.网上有些解 ...

  6. 【转】Asp.net中时间格式化的6种方法详细总结

    1. 数据控件绑定时格式化日期方法: 代码如下: <asp:BoundColumn DataField="AddTime" HeaderText="添加时间&quo ...

  7. linux -samba

    yum install samba samba-client samba-swat samba-common-3.6.9-151.el6.x86_64 //主要提供samba服务器的设置文件与设置文件 ...

  8. 使用 Knockout 扩展器扩展 observables

    原文地址:http://knockoutjs.com/documentation/extenders.html 原文名称:Using extenders to augment observables ...

  9. ASP.NET MV3 部署网站 报"Could not load file or assembly ' System.Web.Helpers “ 错的解决方法

    转自:http://www.cnblogs.com/taven/archive/2011/08/14/2138077.html 国内很多网站空间都只支持.NET 2.0 和 .NET 3.0 3.5, ...

  10. Android MVC模式

    Android MVC模式 下面是我对Android MVC模式的理解 Model 模型层 包括实体模型层,存放程序中调用的实体. 业务模型层,存放程序中调用的业务逻辑.   View 显示层  An ...