社区讨论
84,qt,xx(新的)
P2802回家参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @mhjtyxa0
- 此快照首次捕获于
- 2025/11/04 08:28 4 个月前
- 此快照最后确认于
- 2025/11/04 08:28 4 个月前
CPP
#include<bits/stdc++.h>
using namespace std;
int m,n;
int sx,sy;
int ex,ey;
const int dx[]={0,1,-1,0,0};
const int dy[]={0,0,0,1,-1};
struct step{
int now;
int x;
int y;
int CurrentHP;
};
struct matrix_point{
bool stat;
bool mouse;
};
matrix_point _matrix[10][10];
bool vis[10][10];
int bfs(){
queue<step>q;
q.push({0,sx,sy,6});
vis[sx][sy]=true;
while(!q.empty()){
step cur=q.front();
int cur_x=cur.x;
int cur_y=cur.y;
int next_hp=cur.CurrentHP-1; //has no need to save current HP because however the next step's HP will be CurrentHP-1.
int next_step=cur.now+1; //stepping!
q.pop();
if(next_hp==0) continue;
for(int i=1;i<=4;i++){
int next_x=cur_x+dx[i];
int next_y=cur_y+dy[i];
if(next_x>0&&next_x<=n&&next_y>0&&next_y<=m){
if(_matrix[next_x][next_y].stat&&!vis[next_x][next_y]){
cerr<<"Current: (x:"<<next_x<<" , y:"<<next_y<<" , hp: "<<next_hp<<" , step: "<<next_step<<") -> \n";
if(_matrix[next_x][next_y].mouse){
//cerr<<"Mouse!\n";
next_hp=6;
}
if(next_x==ex&&next_y==ey){
/*ans=next_step;*/ //Nah!
//cerr<<"Reached!\n";
return next_step;
}
vis[next_x][next_y]=true;
q.push({next_step,next_x,next_y,next_hp});
//cerr<<"Can reach!\n";
}
//else cerr<<"Cannot reach...\n";
}
//else cerr<<"Cannot reach...\n";
}
}
return -1;
}
int main(){
cin>>n>>m; //input basic data
for(int i=1;i<=n;i++){ //input matrix data
for(int j=1;j<=m;j++){
int temp;
cin>>temp;
switch(temp){
case 0: //barrier
_matrix[i][j].stat=false; //can't walk throught
break;
case 1: //road
_matrix[i][j].stat=true; //can walk throught
break;
case 2: //start point
_matrix[i][j].stat=false; //can't walk throught for default
sx=i; //save start-x
sy=j; //save start-y
break;
case 3: //end point
_matrix[i][j].stat=true; //can acieve
ex=i; //save end-x
ey=j; //save end-y
break;
case 4: //"mouse point"
_matrix[i][j].stat=true; //can walk throught
_matrix[i][j].mouse=true; //set mouse->1
}
}
}
cout<<bfs()<<endl;
return 0;
}
回复
共 0 条回复,欢迎继续交流。
正在加载回复...