nyoj 284 坦克大战 (优先队列)
题目链接:http://acm.nyist.net/JudgeOnline/status.php?pid=284
特殊数据:
5 5
BBEEY
EEERB
SSERB
SSERB
SSETB
1 2 #include<iostream> 3 #include<cstdio> 4 #include<algorithm> 5 #include<cstring> 6 #include<string> 7 #include<cmath> 8 #include<map> 9 #include<queue> 10 using namespace std; 11 int e1,e2,m,n; 12 int mapp[310][310]; 13 int vis[310][310]; 14 int dir[4][2]={-1,0,1,0,0,1,0,-1}; 15 int ans; 16 struct T 17 { 18 int x,y; 19 }now,eed; 20 int bfs(int s1,int s2) 21 { 22 queue< T >aa; 23 while(!aa.empty()) aa.pop(); 24 now.x=s1;now.y=s2; 25 aa.push(now); 26 while(!aa.empty()) 27 { 28 eed=aa.front(); 29 aa.pop(); 30 if(eed.x==e1 && eed.y==e2) 31 { 32 if(ans>vis[eed.x][eed.y]) 33 ans=vis[eed.x][eed.y]; 34 } 35 for(int i=0;i<4;i++) 36 { 37 now.x=eed.x+dir[i][0];now.y=eed.y+dir[i][1]; 38 //cout<<now.x<<" "<<now.y<<" "<<endl; 39 if( mapp[now.x][now.y]!=0 ) 40 { 41 if(vis[now.x][now.y]==0) 42 { vis[now.x][now.y]=mapp[now.x][now.y]+vis[eed.x][eed.y]; aa.push(now); } 43 else if(mapp[now.x][now.y]+vis[eed.x][eed.y]<vis[now.x][now.y]) 44 { 45 vis[now.x][now.y]= mapp[now.x][now.y]+vis[eed.x][eed.y] ; 46 aa.push(now); 47 } 48 } 49 } 50 } 51 return ans; 52 //5 5 53 //BBEEY 54 //EEERB 55 //SSERB 56 //SSERB 57 //SSETB 58 } 59 int main() 60 { 61 int i,j,k,t,s1,s2; 62 char s; 63 while(cin>>m>>n && m+n) 64 {ans=1000; 65 memset(mapp,0,sizeof(mapp)); 66 memset(vis,0,sizeof(vis)); 67 for(int i=1;i<=m;i++) 68 for(int j=1;j<=n;j++) 69 { 70 cin>>s; 71 if(s=='Y') 72 {s1=i;s2=j;} 73 else if(s=='T') 74 {e1=i;e2=j; mapp[i][j]=1;} 75 else if(s=='B') 76 mapp[i][j]=2; 77 else if(s=='E') 78 mapp[i][j]=1; 79 else mapp[i][j]=0; 80 } 81 int mm=bfs(s1,s2); 82 if(mm<1000) 83 cout<<ans<<endl; 84 else cout<<-1<<endl; 85 } 86 return 0; 87 } 88
优先队列:
1 #include<queue> 2 #include<algorithm> 3 #include<iostream> 4 #include<cstdio> 5 #include<cstring> 6 using namespace std; 7 char map[310][310]; 8 int vis[310][310],m,n; 9 int dir[4][2] = {1,0,-1,0,0,-1,0,1}; 10 int sx,sy,ex,ey; 11 struct T 12 { 13 int x,y,step; 14 15 }now,eed; 16 bool operator<( T a, T b ){ 17 return a.step>b.step; 18 } 19 priority_queue<T>pp; 20 bool judge(int x,int y) 21 { 22 if(x<1 || x>m || y<1 || y>n || vis[x][y]==1 || map[x][y]=='S' || map[x][y]=='R') 23 return false; 24 25 return true; 26 } 27 int bfs() 28 { 29 while(!pp.empty()) pp.pop(); 30 now.x = sx; now.y = sy; now.step = 0; 31 vis[now.x][now.y]=1; 32 pp.push(now); 33 while(!pp.empty()) 34 { 35 eed = pp.top(); 36 pp.pop(); 37 // printf("%d %d \n",eed.x,eed.y); 38 if(eed.x == ex && eed.y==ey) 39 return eed.step; 40 41 for(int i=0;i<4;i++) 42 { 43 now.x = eed.x+dir[i][0]; 44 now.y = eed.y+dir[i][1]; 45 if(judge(now.x ,now.y)) 46 { 47 if(map[now.x][now.y] == 'B') 48 now.step = eed.step+2; 49 else 50 now.step = eed.step+1; 51 vis[now.x][now.y]= 1; 52 pp.push(now); 53 } 54 } 55 } 56 return -1; 57 58 } 59 int main() 60 { 61 while(scanf("%d %d",&m,&n) && m+n) 62 { 63 memset(vis,0,sizeof(vis)); 64 for(int i =1;i<=m;i++) 65 for(int j =1;j<=n; j++) 66 { 67 scanf(" %c",&map[i][j]); 68 if(map[i][j] == 'Y') 69 { 70 sx = i;sy = j; 71 } 72 if(map[i][j] == 'T') 73 { 74 ex = i;ey = j; 75 } 76 } 77 int ans = bfs(); 78 printf("%d\n",ans); 79 } 80 return 0; 81 }
nyoj 284 坦克大战 (优先队列)相关推荐
- nyoj 284 坦克大战【bfs】
坦克大战 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 Many of us had played the game "Battle city" i ...
- nyoj 284 坦克大战
典型的广度优先搜索, 题意:图中Y代表起点,T代表终点,S代表铁墙,不可被击毁也不可被通过,B代表木墙,可以被击毁,击毁后可以通过,R代表河流,不可击毁也不可通过,E代表可通过. 击毁木墙会消耗一步, ...
- HYOJ 284 坦克大战
描述 Many of us had played the game "Battle city" in our childhood, and some people (like me ...
- nyoj284 坦克大战(dijkstra(bfs+优先队列))
题目284 题目信息 运行结果 本题排行 讨论区 坦克大战 时间限制: 1000 ms | 内存限制: 65535 KB 难度: 3 描述 Many of us had played the ga ...
- NYOJ-284 坦克大战
坦克大战 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 Many of us had played the game "Battle city" i ...
- 基于Netty的联机版坦克大战
基于Netty的联机版坦克大战 项目介绍 项目github地址:基于Netty的联机版坦克大战 该项目实现了联机版坦克大战,项目包括客户端与服务端 项目使用技术: 使用Netty实现客户端和服务端之间 ...
- 《HTML5经典坦克大战》游戏(代码)
前几天粗略地学了HTML5,然后就用它写了一个<经典坦克大战>游戏. 现在想分享一下我写的代码,写得不好请大家多多指教. 给大家推荐一个网站,这个网站是为大学生而做,为方便学习编程的同学而 ...
- 对战坦克大战(vc++)
对战坦克大战 转载请注明出处 本文章的下载地址,请单击此链接 本节将介绍一个和FC(FamilyComputer)上的经典游戏<坦克大战>类似的游戏--对战坦克 大战.这是一个4 人对战 ...
- java简单坦克大战制作代码
转载自:http://www.jb51.net/article/88317.htm 利用Java语言中的集合.Swing.线程等知识点编写一个坦克大战游戏. (1) 画出敌我坦克的原理: 在坦克类里面 ...
最新文章
- 在测试集上训练,还能中CVPR?这篇IEEE批判论文是否合理?
- Java FTP下载文件以及编码问题小结
- Linux按键输入实验(体验一下输入驱动,实际开发使用input子系统处理)
- JS中的基本和引用类型传递的比较
- struct lnode是什么结构类型_【自考】数据结构第三章,队列,期末不挂科指南,第4篇
- 通俗易懂!视觉slam第十一部分——线性系统和卡尔曼滤波
- 万创帮逆向解析,让你也能体验技术变现【Python爬虫实战系列之万创帮闲置资源整合逆向】
- sublime基本命令和使用
- rust游戏亮度怎么调亮点_之前用Rust写的扫雷小游戏
- 中国34个省级行政区2000年-2021年逐月NDVI统计分析结果
- 明源售楼系统技术解析(一)搭建MVC3框架
- python词云生成与设计实现_基于python的词云生成(二)
- 弥散张量成像之DTI简介
- 数学与计算机学院校友会,福州大学数学与计算机科学学院厦门校友会成立
- Baxter工作站建立及简单使用
- Linux常见命令 24 - RPM命名管理-包命名与依赖性
- android sina 微博表情功能的实现
- Linux程序设计(Linux shell编程的例子:总结)
- html输出doc,Word转PDF,PNG,HTML神器XDOC
- 使用tcpdump探测TCP/IP三次握手
热门文章
- Matlab Robotic Toolbox V9.10工具箱(五):动力学简介
- liunx(3)-内核模块编写与系统调用
- Markdown写作中的图床解决方案(基于七牛云、PicGo)
- 【ZooKeeper Notes 30】ZooKeeper与Diamond有什么不一样
- 哈哈,netbeans5.5的Visual Web Pack终于出来了
- Chrome Extension in CLJS —— 搭建开发环境
- 过程控制系统模拟信号标准
- 关于IE6,奇数宽高的BUG
- .jsp与servlet之间页面跳转及参数传递实例
- Linux下Minigui开发环境的搭建(PC+S3C2440