题目链接
题目如下:
公主被恶人抓走,被关押在牢房的某个地方。牢房用 N × M ( N , M ≤ 200 ) N \times M (N, M \le 200) N×M(N,M≤200) 的矩阵来表示。矩阵中的每项可以代表道路(@)、墙壁(#)、和守卫(x)。
英勇的骑士(r)决定孤身一人去拯救公主(a)。我们假设拯救成功的表示是 “骑士到达了公主所在的位置”。由于在通往公主所在位置的道路中可能遇到守卫,骑士一旦遇到守卫,必须杀死守卫才能继续前进。
现假设骑士可以向上、下、左、右四个方向移动,每移动一个位置需要 1 1 1 个单位时间,杀死一个守卫需要花费额外的 1 1 1 个单位时间。同时假设骑士足够强壮,有能力杀死所有的守卫。
给定牢房矩阵,公主、骑士和守卫在矩阵中的位置,请你计算拯救行动成功需要花费最短时间。
输入格式:
1、两个整数代表 N N N 和 M , ( N , M ≤ 200 ) M, (N, M \le 200) M,(N,M≤200).
2、随后 N N N 行,每行有 M M M 个字符。"@" 代表道路,“a” 代表公主,“r” 代表骑士,“x” 代表守卫, “#” 代表墙壁。
输出格式:
如果拯救行动成功,输出一个整数,表示行动的最短时间。
如果不可能成功,输出 “Impossible”。
Sample Input:
7 8
#@#####@
#@a#@@r@
#@@#x@@@
@@#@@#@#
#@@@##@@
@#@@@@@@
@@@@@@@@
Sample Output
13
Sample Input 2
13 40
@x@@##x@#x@x#xxxx##@#x@x@@#x#@#x#@@x@#@x
xx###x@x#@@##xx@@@#@x@@#x@xxx@@#x@#x@@x@
#@x#@x#x#@@##@@x#@xx#xxx@@x##@@@#@x@@x@x
@##x@@@x#xx#@@#xxxx#@@x@x@#@x@@@x@#@#x@#
@#xxxxx##@@x##x@xxx@@#x@x####@@@x#x##@#@
#xxx#@#x##xxxx@@#xx@@@x@xxx#@#xxx@x#####
#x@xxxx#@x@@@@##@x#xx#xxx@#xx#@#####x#@x
xx##@#@x##x##x#@x#@a#xx@##@#@##xx@#@@x@x
x#x#@x@#x#@##@xrx@x#xxxx@##x##xx#@#x@xx@
#x@@#@###x##x@x#@@#@@x@x@@xx@@@@##@@x@@x
x#xx@x###@xxx#@#x#@@###@#@##@x#@x@#@@#@@
#@#x@x#x#x###@x@@xxx####x@x##@x####xx#@x
#x#@x#x######@@#x@#xxxx#xx@@@#xx#x#####@
Sample Output 2
7
这个题要使用快排或者优先队列去写,我先上传的代码是坐标形式的,每从一个点向外延深结束就要根据时间进行一次排序。
快排代码如下:
#include<iostream>
#include<algorithm>
using namespace std;
struct muban
{
int x;
int y;
int time;
}que[50000];
char imap[205][205];
int book[205][205];
int next[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
bool cmp(muban a,muban b)
{
return a.time<b.time;
}
int main()
{
int n,m;
cin>>n>>m;
int stax,stay,endx,endy;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>imap[i][j];
if(imap[i][j]=='r')
{
stax=i;stay=j;
}
else if(imap[i][j]=='a')
{
endx=i;endy=j;
imap[i][j]='@';
}
}
}
int head,tail,tx,ty;
head=1;
tail=2;
que[head].x=stax;
que[head].y=stay;
que[head].time=0;
int flag=0;
while(head<tail)
{
for(int i=0;i<4;i++)
{
tx=next[i][0]+que[head].x;
ty=next[i][1]+que[head].y;
if(tx>=0&&tx<n&&ty>=0&&ty<m)
{
if(imap[tx][ty]=='@'&&book[tx][ty]==0)
{
book[tx][ty]=1;
que[tail].x=tx;
que[tail].y=ty;
que[tail].time=que[head].time+1;
tail++;
}
else if(imap[tx][ty]=='x'&&book[tx][ty]==0)
{
book[tx][ty]=1;
que[tail].x=tx;
que[tail].y=ty;
que[tail].time=que[head].time+2;
tail++;
}
}
if(que[tail-1].x==endx&&que[tail-1].y==endy)
{
flag=1;
break;
}
}
if(flag==1)
break;
head++;
sort(que+head,que+tail,cmp);
}
if(flag==0)
cout<<"Impossible"<<endl;
else
cout<<que[tail-1].time<<endl;
return 0;
}
优先队列代码如下:
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
struct muban
{
int x;
int y;
int time;
muban (int xx,int yy,int ttime)
{
x=xx;
y=yy;
time=ttime;
}
friend bool operator <(muban a,muban b)
{
return a.time>b.time;
}
};
char imap[205][205];
int book[205][205];
int next[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
int main()
{
int n,m;
int stax,stay,endx,endy;
cin>>n>>m;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>imap[i][j];
if(imap[i][j]=='r')
{
stax=i;
stay=j;
}
if(imap[i][j]=='a')
{
endx=i;
endy=j;
}
}
}
priority_queue<muban> q;
muban temp(stax,stay,0);
book[stax][stay]=1;
q.push(temp);
int tx,ty,flag;
flag=0;
int sumtime;
while(q.empty()==0)
{
temp=q.top();
q.pop();
for(int i=0;i<4;i++)
{
tx=temp.x+next[i][0];
ty=temp.y+next[i][1];
if(tx>=0&&tx<n&&ty>=0&&ty<m)
{
if(book[tx][ty]==0&&imap[tx][ty]=='@')
{
q.push(muban(tx,ty,temp.time+1));
book[tx][ty]=1;
}
if(book[tx][ty]==0&&imap[tx][ty]=='x')
{
q.push(muban(tx,ty,temp.time+2));
book[tx][ty]=1;
}
if(book[tx][ty]==0&&imap[tx][ty]=='a')
{
flag=1;
sumtime=temp.time+1;
break;
}
}
}
if(flag==1)
break;
}
if(flag==0)
cout<<"Impossible"<<endl;
else
cout<<sumtime<<endl;
return 0;
}