求助站外题
  • 板块题目总版
  • 楼主lighthouse
  • 当前回复2
  • 已保存回复2
  • 发布时间2021/7/19 15:12
  • 上次更新2023/11/4 14:11:04
查看原帖
求助站外题
218180
lighthouse楼主2021/7/19 15:12

openjudge:1818红与黑

#include <bits/stdc++.h>

using namespace std;

int w, h;

int sx, sy;

char a[25][25];

bool f[25][25];

int ans;

int t[410][2], head, tail;

int dx[4] = {1, -1, 0, 0};

int dy[4] = {0, 0, 1, -1};

void bfs(int x, int y){
	head = 0;
	tail = 1;
	t[1][0] = x;
	t[1][1] = y;
	while(head < tail){
		head++;
		for(int i = 0;i < 4;i++){
			int xx = t[head][0] + dx[i];
			int yy = t[head][1] + dy[i];
			if(xx >= 1 && xx <= w && yy >= 1 && yy <= h && a[xx][yy] == '.' && f[xx][yy] == 0){
				ans++;
				f[xx][yy] = 1;
				tail++;
				t[tail][0] = xx;
				t[tail][1] = yy;
			}
		}
	}
}

int main(){
	while(cin >> w >> h){
		if(w == 0 && h == 0) return 0;
		for(int i = 1;i <= h;i++){
			for(int j = 1;j <= w;j++){
				cin >> a[i][j];
				if(a[i][j] == '@'){
					sx = i;
					sy = j;
				}
			}
		}
		memset(f, 0, sizeof(f));
		ans = 1;
		f[sx][sy] = 1;
		bfs(sx, sy);
		cout << ans << endl;
	}
	return 0;
}

不知道怎么错的,求指教

2021/7/19 15:12
加载中...