好理解的bfs
查看原帖
好理解的bfs
169070
明朗编程楼主2024/10/26 16:57
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N =1005,M = N*N;
char le[N][N];
int dx[] = {0,1,0,-1};
int dy[] = {1,0,-1,0};
int n,m,k;
bool st[N][N];
int bfs(int x,int y,int d){
	st[x][y] = 1;
	int ans = 1;
	while(k--){
		
		int a = x+dx[d];
		int b = y+dy[d];
		if(a<0||a>=n||b<0||b>=m||le[a][b]=='x'){
			d =(d+1)%4;
			//printf("i = %d j=%d zhuan\n",a,b);
			continue;
		}
		//printf("i = %d j=%d\n",a,b);
		if(!st[a][b]) ans++;
		x = a;
		y = b; 
		st[a][b] = 1;
		
	} 
	return ans;
		
}
int main(){
	//freopen("4.in","r",stdin);
	//freopen("1.out","w",stdout);
	int T;
	int x,y,d;
	cin>>T;
	while(T--){
		memset(le,0,sizeof(le));
		memset(st,0,sizeof(st));
		scanf("%d%d%d",&n,&m,&k);
		scanf("%d%d%d",&x,&y,&d);
		for(int i = 0;i<n;i++)scanf("%s",le[i]);
		printf("%d\n",bfs(x-1,y-1,d));
	}
	return 0;	
}
2024/10/26 16:57
加载中...