社区讨论
悬关!!qwq
P2937[USACO09JAN] Laserphones S参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @lywuzv1y
- 此快照首次捕获于
- 2024/07/22 18:43 2 年前
- 此快照最后确认于
- 2024/07/22 20:01 2 年前
最后的4个SubtaskWA掉
CPP#include<bits/stdc++.h>
using namespace std;
const int maxn=1000+10;
int sx,sy,ex,ey,n,m,a[maxn][maxn],ans=0x7f7f7f7f,t;
int d[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
bool vis[maxn][maxn];
struct node{
int x,y,fx,cnt;
node(int xx,int yy,int fxx,int cntt){
x=xx; y=yy; fx=fxx; cnt=cntt;
}
};
int bfs(int sx,int sy,int ex,int ey,int fx){
int dx,dy;
queue<node> q;
q.push(node(sx,sy,fx,0));
vis[sx][sy]=true;
while(!q.empty()){
node no=q.front(); q.pop();
dx=d[no.fx][0]+no.x; dy=d[no.fx][1]+no.y;
while((dx!=ex || dy!=ey) && a[dx][dy]==1 && !vis[dx][dy]){
q.push(node(dx,dy,no.fx,no.cnt));
vis[dx][dy]=true;
dx=dx+d[no.fx][0]; dy=dy+d[no.fx][1];
}
if(dx==ex && dy==ey) return no.cnt;
for(int i=0;i<4;i++){
if(i==no.fx) continue;
dx=d[i][0]+no.x; dy=d[i][1]+no.y;
if(dx==ex && dy==ey) return no.cnt+1;
else if(a[dx][dy]==1 && !vis[dx][dy]){
q.push(node(dx,dy,i,no.cnt+1));
vis[dx][dy]=true;
}
}
}
return -1;
}
signed main(){
cin>>n>>m;
char ch;
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
cin>>ch;
if(ch=='.') a[i][j]=1;
else if(ch=='*') a[i][j]=0;
else if(ch=='C' && t==0){
t++; sx=i; sy=j;
}if(ch=='C' && t==1) ex=i,ey=j;
}
}
if(sx==ex && sy==ey){
cout<<0; return 0;
}
for(int i=0;i<4;i++){
memset(vis,0,sizeof(vis));
ans=min(ans,bfs(sx,sy,ex,ey,i));
}
else cout<<ans;
return 0;
}
回复
共 0 条回复,欢迎继续交流。
正在加载回复...