社区讨论
18分求调
P1825[USACO11OPEN] Corn Maze S参与者 3已保存回复 8
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 8 条
- 当前快照
- 1 份
- 快照标识符
- @mhjdke7b
- 此快照首次捕获于
- 2025/11/04 00:49 4 个月前
- 此快照最后确认于
- 2025/11/04 00:49 4 个月前
小的样例是可以过的
(毕竟就一个样例,但输出x,y,t即位置和步数和题目提示一样)
谢谢大佬
CPP#include<bits/stdc++.h>
using namespace std;
char ch[205][305];
int n,m,r,c;
bool vis[205][305],cnt[26];
int zl[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
struct Node{
int x,y,t;
};
queue<Node> q;
struct F{
int a,b;
};
F o[26],p[26];
int bfs(){
while(!q.empty()){
int x,y,t;
x=q.front().x;
y=q.front().y;
t=q.front().t;
q.pop();
//cout<<"--"<<x<<" "<<y<<" "<<t<<endl;
if(x==r&&y==c){
return t;
}
for(int i=0;i<4;i++){
int dx,dy;
dx=x+zl[i][0];
dy=y+zl[i][1];
if(dx<1||dx>n||dy<1||dy>m){
continue;
}
if((ch[dx][dy]=='.'||ch[dx][dy]=='=')&&!vis[dx][dy]){
vis[dx][dy]=true;
q.push({dx,dy,t+1});
}
if('A'<=ch[dx][dy]&&'Z'>=ch[dx][dy]){
int num=(int)(ch[dx][dy]-'A');
vis[dx][dy]=true;
ch[dx][dy]='.';
if(dx==p[num].a&&dy==p[num].b){
dx=o[num].a;
dy=o[num].b;
}
if(dx==o[num].a&&dy==o[num].b){
dx=p[num].a;
dy=p[num].b;
}
vis[dx][dy]=true;
ch[dx][dy]='.';
q.push({dx,dy,t+1});
}
}
}
return -1;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
scanf(" %c",&ch[i][j]);
if(ch[i][j]=='@'){
vis[i][j]=true;
q.push({i,j,0});
}
if(ch[i][j]=='='){
r=i;
c=j;
}
if(ch[i][j]>='A'&&ch[i][j]<='Z'){//符号别打错了(T^T)
int num=(int)(ch[i][j]-'A');
if(!cnt[num]){
p[num].a=i;
p[num].b=j;
cnt[num]=true;
}
else{
o[num].a=i;
o[num].b=j;
}
}
}
}
for(int i=0;i<26;i++){
cnt[i]=false;
}
printf("%d",bfs());
return 0;
}
回复
共 8 条回复,欢迎继续交流。
正在加载回复...