蒟蒻求助,感觉代码思路是对的,为什么还会WA?
查看原帖
蒟蒻求助,感觉代码思路是对的,为什么还会WA?
593753
LukeSu楼主2022/2/24 09:38
#include<bits/stdc++.h>
using namespace std;

const int N = 666;
int n, m;
char g[N][N];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
bool st[N][N];

	int dfs(int x, int y){
		st[x][y] = true;
		for(int i =0 ; i < 4; i++){
			int xx = dx[i] + x;
			int yy = dy[i] + y;
			if(xx >= 1 and xx <= n and yy >= 1 and yy <= m 
			and !st[xx][yy] and (g[xx][yy] == '.' or g[xx][yy] == 'g') and g[xx][yy] != '#'){
				st[xx][yy] = true;
				if(g[xx][yy] == 'g'){
					return 1;
				}
				dfs(xx, yy);
			}
		}
		return -1;
	}
int main(){
cin >> n >> m;
	for(int i = 1; i <= n; i++ )
		for(int j = 1; j <= m; j++)
			cin >> g[i][j];
	
	for(int i = 1; i <= n; i++ ){
		for(int j = 1; j <= m; j++){
			if(g[i][j] == 's'){
				if(dfs(i, j)){
					cout << "Yes" << endl;break;
				}
				else{
					cout << "No" << endl;break;
				}
			}
		}	
	}
	return 0;
}
2022/2/24 09:38
加载中...