0806搜索专项
Done
IOI
Start at: 2024-8-6 9:00
3
hour(s)
Host:
29
#include <bits/stdc++.h>
using namespace std;
struct node{
int x,y,cost;
friend bool operator < (node a,node b){
return a.cost>b.cost;
}
};
int T,n,m,p,book[305][305],sx,sy,fx,fy;
int nxt[4][2]={-1,0,0,1,1,0,0,-1};
char mat[305][305];
int main()
{
cin>>T;
while(T--){
cin>>n>>m>>p;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>mat[i][j];
if(mat[i][j]=='S')sx=i,sy=j;
if(mat[i][j]=='T')fx=i,fy=j;
}
}
bfs();
}
return 0;
}
void bfs()
{
priority_queue<node>q;
q.push({sx,sy,0});
memset(book,0x3f,sizeof(book));
book[sx][sy] = 0;
while(!q.empty()){
node tmp = q.top();
q.pop();
for(int i=0;i<4;i++){
int nx = tmp.x+nxt[i][0];
int ny = tmp.y+nxt[i][1];
if(nx>=1 && nx<=n && ny>=1 && ny<=m){
int cost = tmp.cost;
if(mat[tmp.x][tmp.y]=='#' || mat[tmp.x][tmp.y]=='S'){
if(book[nx][ny]>cost){
book[nx][ny] = cost;
q.push({nx,ny,cost});
}
}
if(mat[tmp.x][tmp.y]=='.'){
if(book[nx][ny]>cost+p){
book[nx][ny] = cost+p;
q.push({nx,ny,cost+p});
}
}
if(mat[tmp.x][tmp.y]=='^'){
if(i==1)cost+=1;
if(i==2)cost+=2;
if(i==3)cost+=3;
if(book[nx][ny]>cost){
book[nx][ny] = cost;
q.push({nx,ny,cost});
}
}
if(mat[tmp.x][tmp.y]=='>'){
if(i==2)cost+=1;
if(i==3)cost+=2;
if(i==0)cost+=3;
if(book[nx][ny]>cost){
book[nx][ny] = cost;
q.push({nx,ny,cost});
}
}
if(mat[tmp.x][tmp.y]=='v'){
if(i==3)cost+=1;
if(i==0)cost+=2;
if(i==1)cost+=3;
if(book[nx][ny]>cost){
book[nx][ny] = cost;
q.push({nx,ny,cost});
}
}
if(mat[tmp.x][tmp.y]=='<'){
if(i==0)cost+=1;
if(i==1)cost+=2;
if(i==2)cost+=3;
if(book[nx][ny]>cost){
book[nx][ny] = cost;
q.push({nx,ny,cost});
}
}
}
}
}
if(book[fx][fy]!=0x3f3f3f3f){
cout<<book[fx][fy]<<endl;
}
}
- Status
- Done
- Rule
- IOI
- Problem
- 6
- Start at
- 2024-8-6 9:00
- End at
- 2024-8-6 12:00
- Duration
- 3 hour(s)
- Host
- Partic.
- 29