阿尔吉侬的花束 (典bfs)

0326重写,一个小时终于成功了

#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>

using namespace std;

int t;
int r,c;
char mp[210][210];
bool vis[210][210];
struct node{
	int x,y;
};
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
int sx,sy;
int path[210][210] = {-1};

bool bfs(int xx,int yy){
	queue<node> q;
	q.push({xx,yy});
	vis[xx][yy] = true;
	path[xx][yy] = 0;
	
	while(!q.empty()){
		node temp = q.front();
		q.pop();
		
		for(int i=0;i<4;i++){
			int nx = dx[i] + temp.x;
			int ny = dy[i] + temp.y;
			
			if(mp[nx][ny] == 'E'){
					数据更新时要看清变量nx和ny!!!!path[nx][ny] = path[temp.x][temp.y] + 1;!!!
					printf("%d\n",path[nx][ny]);
					return true;
			}
			
			if(path[nx][ny] == -1 && mp[nx][ny] == '#' || nx < 0 || nx >= r || ny < 0 || ny >= c){
				continue;
			}	
			
			if(!vis[nx][ny] && mp[nx][ny] == '.'){
				path[nx][ny] = path[temp.x][temp.y] + 1;
				vis[nx][ny] = true;	
				q.push({nx,ny});
			}
		}
	}
	
	return false;
	
}
int main()
{
	scanf("%d",&t);
	char s[210];
	while(t--){
	
		memset(mp,'#',sizeof(mp));
		memset(vis,false,sizeof (vis));
		memset(path,-1,sizeof (path));
		scanf("%d%d",&r,&c);
		for(int i=0;i<r;i++){
			scanf("%s",&s);
			for(int j=0;j<c;j++){
				mp[i][j] = s[j];
				
				if(mp[i][j] == 'S'){
					sx = i;
					sy = j;
				}
			}
		}
		
		bool ans = bfs(sx,sy);
		if(!ans) printf("oop!\n");
	
	}
	return 0;
} 

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-26 22:12:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-26 22:12:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-26 22:12:03       87 阅读
  4. Python语言-面向对象

    2024-03-26 22:12:03       96 阅读

热门阅读

  1. ChatGPT引领论文写作新潮流

    2024-03-26 22:12:03       44 阅读
  2. Web日志分析

    2024-03-26 22:12:03       29 阅读
  3. ARM IHI0069F GIC architecture specification (2)

    2024-03-26 22:12:03       32 阅读
  4. day8 ARM

    day8 ARM

    2024-03-26 22:12:03      39 阅读
  5. vue js金额转中文

    2024-03-26 22:12:03       43 阅读
  6. 逻辑回归的详解及应用

    2024-03-26 22:12:03       38 阅读
  7. 第二十七章 TypeScript TS进阶用法infer

    2024-03-26 22:12:03       36 阅读
  8. ubuntu 搭建 sonic v2.6.4 平台记录

    2024-03-26 22:12:03       33 阅读
  9. 【C++】6-2 交换函数2 分数 10

    2024-03-26 22:12:03       37 阅读
  10. ChatGPT秘籍:让ChatGPT帮你打造出色论文

    2024-03-26 22:12:03       39 阅读
  11. 解释器模式

    2024-03-26 22:12:03       36 阅读