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
随机推荐
- poj1456 结构体排序+贪心
题意:给出很多商品,每个商品有价值和出售期限,只能在期限内出售才能获取利润,每一个单位时间只能出售一种商品,问最多能获得多少利润. 只需要按照优先价值大的,其次时间长的排序所有物品,然后贪心选择,从它 ...
- Python如何规定对方输入的数字必须是整数?
可以使用字符串str的isdigit方法判断字符串是否是一个仅有数字组成,也就是整数.如果是整数退出while循环,否则继续请求输入. 1 2 3 4 5 6 while True: x = ...
- jQuery序列化后的表单值转换成Json
$.fn.serializeObject = function() { var o = {}; var a = this.serializeArray(); $.each(a, function() ...
- Java classes and class loading
JAVA类加载器概念与线程类加载器 http://www.cnblogs.com/pfxiong/p/4118445.html http://stackoverflow.com/questions/2 ...
- Oracle数据库——体系结构
一.涉及内容 1.了解数据库的物理存储结构和逻辑存储结构 二.具体操作 1.分别使用SQL 命令和OEM 图形化工具查看本地数据库的物理文件,并使用OEM 工具在现有的users 表空间中添加user ...
- Q3: Linked List Cycle II
问题描述 Given a linked list, return the node where the cycle begins. If there is no cycle, return null. ...
- angular模板加载 ----ng-template
Angularjs作为mvc(或者说mvvm)框架,同样具备模板这一基本概念. NG加载模板的顺序为 内存加载---AJAX加载. 如果排版乱掉,请查阅https://www.zybuluo.com/ ...
- html之块级标签h系列,div
两个名词含义: 块级标签:内容再少也会占满整行 内联标签:有多少内容点多少地方 注:块级标签和内联不签不是绝对的,可以通过CSS作任意转换 1.h1-h6 :块级标签 请仅仅把标题标签用作标题文本,如 ...
- CentOS快速搭建subversion服务器
环境:centos x86_64说明:使用daemon的方式来跑svn,不使用apache首先到:http://subversion.tigris.org/ 下载最新的subversion源码 我下的 ...
- 文件/目录部分处理工具类 DealDir.java
package com.util; import java.io.File; import java.util.StringTokenizer; /** * 文件/目录 部分处理 * @createT ...