问题代码,求改
查看原帖
问题代码,求改
1377278
Yao0_2012楼主2025/7/28 13:14
//死循环,求助大佬
#include<bits/stdc++.h>
using namespace std;
int a[10][10],n,sum=0;
bool vis[10][10] = {false};
struct Pos{
	int num;
	int x_pos;
	int y_pos;
};
void bfs(int x,int y){
	queue<Pos> q;
	Pos start;
	start.num = 0;
	start.x_pos=x;
	start.y_pos=y;
	q.push(start);
	vis[1][1] == true;
	while(!q.empty()){
		Pos head;
		head = q.front();
		q.pop();
		for(int i=1;i<=2;i++){
			int tx,ty;
			if(i){
				tx=head.x_pos + 1;
				ty=head.y_pos;
				if(tx > 0 and tx <=n and ty>0 and ty<=n and !(vis[tx][ty])){
					Pos tail;
					tail.x_pos=tx;
					tail.y_pos=ty;
					sum += a[tx][ty];
					tail.num = sum;
					q.push(tail);
					vis[tx][ty] = true;
				}
			}
		}
	}
}
bool stop(){
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			if(!vis[i][j])
				return false;
	return true;
}
int main(){
	cin>>n;
	while(1){
		int t1,t2,t3;
		cin>>t1>>t2>>t3;
		if(t1 == 0 and t2 == 0 and t3 == 0){
			break;
		}
		a[t1][t2]=t3;
	}
	int maxn =0; 
	while(!stop()){
		sum = 0;
		bfs(1,1);
		if(sum > maxn) maxn=sum;
	}
	cout<<maxn;
}
//
2025/7/28 13:14
加载中...