nyoj 284 坦克大战 (优先队列)

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

特殊数据:

5 5

BBEEY

EEERB

SSERB

SSERB

SSETB

7

非优先队列:

 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 }
时间: 2024-08-26 18:09:40

nyoj 284 坦克大战 (优先队列)的相关文章

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

坦克大战 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that co

nyoj 284 坦克大战【bfs】

坦克大战 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that co

NYOJ 284 坦克大战 &amp;&amp; POJ 2312 Battle City (广搜+优先队列)

链接:click here~~ 题意: 描述 Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that consists of empty space

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

这类带权的边的图,直接广搜不行,要加上优先队列,这样得到的结果才是最优的,这样每次先找权值最小的,代码如下 1 #include <stdio.h> 2 #include <iostream> 3 #include <queue> 4 #include <string.h> 5 using namespace std; 6 typedef struct Node{ 7 int x, y; 8 int step; 9 friend bool operator

nyoj 坦克大战 (优先队列)

BFS题目.输入完图之后可以先预处理一下,然后用优先队列解决. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<string> 6 #include<queue> 7 #include<algorithm> 8 #include<map> 9 #include<iomani

nyoj284 坦克大战(dijkstra(dfs+优先队列))

题目284 题目信息 运行结果 本题排行 讨论区 坦克大战 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this

坦克大战(版本2.5-版本2.9)

版本2.5 功能:添加“血块”步骤:        1)添加blood类        2)添加必要的方法:eat方法等        3)让blood对象固定轨迹运动, 并在一定时间后消失 具体代码实现: 新增的blood类: 1 import java.awt.Color; 2 import java.awt.Graphics; 3 import java.awt.Rectangle; 4 5 //模拟血块,坦克吃了可以补血 6 public class Blood { 7 int x, y

Java__线程---基础知识全面实战---坦克大战系列为例

今天想将自己去年自己编写的坦克大战的代码与大家分享一下,主要面向学习过java但对java运用并不是很熟悉的同学,该编程代码基本上涉及了java基础知识的各个方面,大家可以通过练习该程序对自己的java进行一下实战. 每个程序版本代码中,都附有相关注释,看完注释大家就可以对本程序设计有个很明显的思路.真的很有趣,想对java重新温习的同学完全不必再对厚厚的java基础书籍进行阅读了,在跟着本次代码练习并分析后,大家一定会对java各方面基础知识 尤其是线程的知识有更深一步的了解!!! 本次坦克大

nyoj 284

坦克大战 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that co