坦克大战
2015-09-01 23:16
295 查看
坦克大战
时间限制: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
思路:
优先队列+bfs,OK。
AC代码:
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<string> #include<queue> using namespace std; typedef long long ll; int n,m,bo[305][305]; char s[305][305]; int fx[][2]={{1,0},{0,1},{-1,0},{0,-1}}; struct node { int x,y; int cnt; friend bool operator<(const node& a,const node& b){ return a.cnt>b.cnt; } }t,p; int bfs(int i,int j) { priority_queue<node> a; int k,tx,ty; t.x=i,t.cnt=0,t.y=j; bo[i][j]=1;a.push(t); while(!a.empty()){ t=a.top();a.pop(); for(k=0;k<4;++k){ tx = t.x + fx[k][0]; ty = t.y + fx[k][1]; if(!bo[tx][ty]&&tx>=0&&tx<n&&ty>=0&&ty<m&&s[tx][ty]!='S'&&s[tx][ty]!='R'){ if(s[tx][ty]=='T')return t.cnt+1; bo[tx][ty]=1; p.x=tx,p.y=ty; if(s[tx][ty]=='B') p.cnt = t.cnt+2; else p.cnt = t.cnt+1; a.push(p); } } } return 0; } int main() { /*freopen("input.txt","r",stdin);*/ int i,j,x,y; while(scanf("%d%d",&n,&m),n,m){ for(i=0;i<n;++i) for(j=0;j<m;++j){ scanf(" %c",&s[i][j]); if(s[i][j]=='Y')x=i,y=j; bo[i][j]=0; } int k=bfs(x,y); if(k) printf("%d\n",k); else printf("-1\n"); } return 0; }
优秀代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define CLR(arr,val) memset(arr,val,sizeof(arr))
const int MAX=310;
char mp[MAX][MAX];
int Len[MAX][MAX],source[2],target[2],row,col;
int Q[MAX*MAX][2];
bool InQ[MAX][MAX];
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int main()
{
// freopen("in.txt","r",stdin);
while(~scanf("%d%d",&row,&col) && row)
{
for(int i=0;i!=row;i++)
{
scanf("%s",mp[i]);
for(int j=0;j!=col;j++)
if(mp[i][j]=='T') {target[0]=i;target[1]=j;}
else if(mp[i][j]=='Y') {source[0]=i;source[1]=j;}
}
CLR(Len,0x3f);
Len[source[0]][source[1]]=0;
int head=0,tail=0;
Q[tail][0]=source[0];Q[tail][1]=source[1];
if(++tail>=MAX*MAX) tail=0;
while(head!=tail)
{
int currow=Q[head][0],curcol=Q[head][1];
if(++head>=MAX*MAX) head = 0;
for(int i=0;i!=4;i++)
{
int tr=currow+dir[i][0],tc=curcol+dir[i][1];
if(tr<0 || tr>=row || tc<0 || tc>=col || mp
int l=(mp
if(Len[currow][curcol]+l<Len
{
Len
if(!InQ
{
Q[tail][0]=tr;Q[tail][1]=tc;
if(++tail>=MAX*MAX) tail=0;
InQ
}
}
}
InQ[currow][curcol]=false;
}
if(Len[target[0]][target[1]]==0x3f3f3f3f) puts("-1");
else printf("%d\n",Len[target[0]][target[1]]);
}
}
相关文章推荐