P2895 请问此代码样例为什么会输出1
  • 板块题目总版
  • 楼主代码小明
  • 当前回复1
  • 已保存回复1
  • 发布时间2021/8/3 18:05
  • 上次更新2023/11/4 12:07:23
查看原帖
P2895 请问此代码样例为什么会输出1
323785
代码小明楼主2021/8/3 18:05

P2895{\color{Green}P2895 }题目


请问此代码样例为什么会输出1

代码:

#include<bits/stdc++.h>
using namespace std;

struct lx{
	int x;
	int y;
	int time_;
};
struct nengzou{
	int x;
	int y;
	int ceng;
};
int m;
lx a[1001];
bool huibuhuizado[1001][1001]; //会不会砸到数组
int dx[4]={0,0,-1,1};
int dy[4]={1,-1,0,0};
queue <nengzou> q;
bool vis[1001][1001];
int main(){
//	freopen("英文小写文件名.in","r",stdin);
//	freopen("英文小写文件名.out","w",stdout);
	cin>>m;
	vis[0][0]=1;
	for(int i=0;i<m;i++){
		cin>>a[i].x>>a[i].y>>a[i].time_ ;
		huibuhuizado[a[i].x][a[i].y]=1;
	}
	q.push(nengzou{0,0,0});
	while(!q.empty()){
		//cout<<"next   \n"<<endl;
		nengzou f=q.front();
		if(huibuhuizado[f.x][f.y]==0){
			cout<<f.ceng;
			return 0;
		}
		for(int k=0;k<m;k++)
			if(a[k].time_==f.ceng)
				vis[a[k].x][a[k].y]=1;
		for(int i=0;i<4;i++){
			int fx=f.x+dx[i];
			int fy=f.y+dy[i];
			//cout<<"willpush\n"<<endl;
			if(fx>=0&&fx<=300&&fy>=0&&fy<=300&&vis[fx][fy]==0){
				//cout<<"push   \n"<<endl;
				q.push(nengzou{fx,fy,f.ceng+1}),vis[fx][fy]=1;
			}
		}
		q.pop();
	}	
	cout<<-1;
	return 0;
}
/*
4
0 0 2
2 1 2
1 1 2
0 3 5
*/

样例:

4
0 0 2
2 1 2
1 1 2
0 3 5


我的输出:1 ???

2021/8/3 18:05
加载中...