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 }

 

posted on 2015-04-30 12:40  细雨微光  阅读(401)  评论(0编辑  收藏  举报