全部TLE,求各位dalao指点指点
  • 板块P1683 入门
  • 楼主Dream_World
  • 当前回复14
  • 已保存回复14
  • 发布时间2023/5/25 16:12
  • 上次更新2023/10/23 14:48:35
查看原帖
全部TLE,求各位dalao指点指点
677149
Dream_World楼主2023/5/25 16:12

源码如下:

#include <iostream>
#include <cstring>
using namespace std;

int w, h;
int vis[50][50];
char ch;
int sx, sy;
int cnt = 0;

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

void dfs(int x, int y){
	if (vis[x][y] == 0)
		cnt++;
	for (int i = 0; i < 4; i++){
		int tx = x + dx[i];
		int ty = y + dy[i];
		if (tx >= 0 && tx < w && ty >= 0 && ty < h && (vis[tx][ty] == 0))
			dfs(tx, ty);
	}
	return ;
}

int main(){
	cin >> w >> h;
	for (int i = 0; i < h; i++){
		for (int j = 0; i < w; j++){
			cin >> ch;
			if (ch == '@')
				sx = i, sy = j;
			else if (ch == '#')
				vis[i][j] = 1;
		}
	}
	dfs(sx, sy);
	cout << cnt;
	return 0;
}
2023/5/25 16:12
加载中...