站外题
  • 板块学术版
  • 楼主NLYAKIOI
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/7/19 16:48
  • 上次更新2024/7/19 17:43:43
查看原帖
站外题
765488
NLYAKIOI楼主2024/7/19 16:48

http://noi.openjudge.cn/ch0205/4980/

这是题

#include<bits/stdc++.h>
using namespace std;
char mg[300][300];
int n,m,s,jh,jl;
struct node{
	int h,l,b;
};
queue<node> q;
int bfs(){
	while(!q.empty()){
		node t;
		t=q.front();
		q.pop();
		if(t.l==jl&&t.h==jh){
			return t.b;
		}
		t.b++;
		if(mg[t.h][t.l]=='*'){
			q.push(t);
			mg[t.h][t.l]='#';
			continue;
		}
		if(t.l+1<m&&mg[t.h][t.l+1]=='@'||mg[t.h][t.l+1]=='x'){
			t.l++;
			q.push(t);
			if(mg[t.h][t.l]=='x') mg[t.h][t.l]='*';
			else mg[t.h][t.l]='#';
			t.l--;
		}
		if(t.h+1<n&&mg[t.h+1][t.l]=='@'||mg[t.h+1][t.l]=='x'){
			t.h++;
			q.push(t);
			if(mg[t.h][t.l]=='x') mg[t.h][t.l]='*';
			else mg[t.h][t.l]='#';
			t.h--;
		}
		if(t.h-1>=0&&mg[t.h-1][t.l]=='@'||mg[t.h-1][t.l]=='x'){
			t.h--;
			q.push(t);
			if(mg[t.h][t.l]=='x') mg[t.h][t.l]='*';
			else mg[t.h][t.l]='#';
			t.h++;
		}
		if(t.l-1>=0&&mg[t.h][t.l-1]=='@'||mg[t.h][t.l-1]=='x'){
			t.l--;
			q.push(t);
			if(mg[t.h][t.l]=='x') mg[t.h][t.l]='*';
			else mg[t.h][t.l]='#';
			t.l++;
		}
	}
	return -1;
}
int main(){
	cin>>s;
	for(int i=0;i<s;i++){
		cin>>n>>m;
		for(int j=0;j<n;j++){
			for(int o=0;o<m;o++){
				cin>>mg[j][o];
				if(mg[j][o]=='r'){
					node t;
					t.h=j;
					t.l=o;
					t.b=0;
					q.push(t);
					mg[j][o]='#';
				}
				if(mg[j][o]=='a'){
					jh=j;
					jl=o;
					mg[j][o]='@';
				}
			}
		}
		int u=bfs();
		if(u<0){
			cout<<"Impossible"<<endl;
		}else{
			cout<<u<<endl;
		}
		while(!q.empty()) q.pop();
	}
	return 0;
}

这是代码

求调

感谢各位dalao

2024/7/19 16:48
加载中...