大佬快帮忙看看为什么死循环
  • 板块P1683 入门
  • 楼主tenneidp
  • 当前回复3
  • 已保存回复3
  • 发布时间2023/2/22 08:29
  • 上次更新2023/10/24 00:07:50
查看原帖
大佬快帮忙看看为什么死循环
788737
tenneidp楼主2023/2/22 08:29
#include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;

int W,H;
int x2,y2;
char park[10000001][22];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};

struct node{
	int x,y;
};

bool check(int x,int y){
	if(x>=0&&y>=0&&x<W&&y<H) return true;
	else return false;
}

void dfs(int n,int m){
	int num=1;//次数 
	queue <node>brick;
	node start,next;
	start.x=n;
	start.y=m;
	brick.push(start);
	while(!brick.empty()){
		start=brick.front();
		for(int i=0;i<4;i++){
		 	next.x=start.x+dir[i][0];
		 	next.y=start.y+dir[i][1];
		 	if(check(next.x,next.y)&&park[next.x][next.y]=='.'){
		 		num++;
		 		park[next.x][next.y]=='#';
		 		brick.push(next);
			}
		}
		brick.pop();
	}
	printf("%d",num);
} 

int main(){
	scanf("%d %d",&W,&H);
	for(int i=0;i<W;i++){
		for(int j=0;j<H;j++){
			scanf("%c",&park[i][j]);
			if (park[i][j]=='@') x2=i,y2=j;
		}
	}
	dfs(x2,y2);
	return 0;
}

DFS函数的num死循环了,朋友说我while里面的if判错了,队列总是在往里进。。。

2023/2/22 08:29
加载中...