nyoj 284 poj 2312 搜索之坦克大战

 

坦克大战

时间限制: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 consists of empty spaces, rivers, steel walls and brick walls only. Your task is to get a bonus as soon as possible suppose that no enemies will disturb you (See the following picture).

Your tank can't move through rivers or walls, but it can destroy brick walls by shooting. A brick wall will be turned into empty spaces when you hit it, however, if your shot hit a steel wall, there will be no damage to the wall. In each of your turns, you can choose to move to a neighboring (4 directions, not 8) empty space, or shoot in one of the four directions without a move. The shot will go ahead in that direction, until it go out of the map or hit a wall. If the shot hits a brick wall, the wall will disappear (i.e., in this turn). Well, given the description of a map, the positions of your tank and the target, how many turns will you take at least to arrive there?
 
输入
The input consists of several test cases. The first line of each test case contains two integers M and N (2 <= M, N <= 300). Each of the following M lines contains N uppercase letters, each of which is one of 'Y' (you), 'T' (target), 'S' (steel wall), 'B' (brick wall), 'R' (river) and 'E' (empty space). Both 'Y' and 'T' appear only once. A test case of M = N = 0 indicates the end of input, and should not be processed.
输出
For each test case, please output the turns you take at least in a separate line. If you can't arrive at the target, output "-1" instead.
样例输入
3 4
YBEB
EERE
SSTE
0 0
样例输出
8
来源
POJ
上传者
sadsad
 
这题目真的没有看懂 看了别人的解题报告才知道原来和大一的时候屠老师讲的题目像类似,以前老是貌似是用堆栈做的 
看懂题意后马上就有思路了 不过还是没有一次AC 贡献了一次CE 只用了string 而不是 cstring  因为VC就是这样的 ,没办法
题意就是空地的话就需要一分钟  如果是墙就需要2分钟 只要用优先队列枚举最小值遍历就能过了 因为开始学了优先队列 nuoj里面 STL的题目差不多AK了 留下最后一道 呵呵 留点纪念
View Code
 1 #include <iostream>
 2 #include <queue>
 3 #include <cstdio>
 4 #include <cstring>
 5 using namespace std;
 6 #define  max 301
 7 char map[max][max];
 8 int n,m;
 9 bool vist[max][max];
10 int d[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
11 
12 struct node 
13 {
14     int x,y,c;
15     friend bool operator <(const node &s1,const node &s2)
16     {
17         return s1.c>s2.c;
18     }
19 };
20 node s,e;
21 
22 int bfs()
23 {
24     priority_queue < node > q;
25     memset(vist,0,sizeof(vist));
26     node now,t;
27     s.c=0;
28     q.push(s);
29     vist[s.x][s.y]=1;
30     while (!q.empty())
31     {
32         now=q.top();
33         q.pop();
34         if (now.x==e.x&&now.y==e.y)
35         {
36             return now.c;
37         }    
38         for (int i=0;i<4;i++)
39         {
40             t.x=now.x+d[i][0];
41             t.y=now.y+d[i][1];
42             if (t.x>=0&&t.x<n&&t.y>=0&&t.y<m&&!vist[t.x][t.y])
43             {        
44                 vist[t.x][t.y]=1;    
45                 if (map[t.x][t.y]=='B')
46                 {
47                     t.c=now.c+2;
48                     q.push(t);
49                 }
50                 else 
51                     if (map[t.x][t.y]=='E'||map[t.x][t.y]=='T')
52                     {
53                         t.c=now.c+1;
54                         q.push(t);
55                     }
56             }
57         }
58     }
59     return -1;
60 }
61 
62 int main()
63 {
64     int i,j;
65     while (cin>>n>>m,m+n)
66     {
67         for (i=0;i<n;i++)
68         {
69             for (j=0;j<m;j++)
70             {
71                 cin>>map[i][j];
72                 if (map[i][j]=='Y')                  
73                     s.x=i,s.y=j;                  
74                 if (map[i][j]=='T')                  
75                     e.x=i,e.y=j;                  
76             }
77         }
78         int sum=bfs();
79      cout<<sum<<endl;
80     }
81     
82     return 0;
83 }


 

原文地址:https://www.cnblogs.com/wujianwei/p/2608911.html