lanqiao OJ 234 大胖子走迷宫

简介: lanqiao OJ 234 大胖子走迷宫

1.大胖子走迷宫 - 蓝桥云课 (lanqiao.cn)

bfs搜索

每个时间段胖子的体型不同 , 分为三种 ,所以我们要分三种判别的方式

因为样例保证能找到出路,所以在胖子很胖的时候被可能被恰住,但是变成1的时候一定不会被卡住,所以我们直接把体积t!=1的时候胖子的位置再加入队列中,但是时间加1 ,这样的话就可以实现胖子原地呆1步;

#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std ;
 
const int N = 310 ;
char mp[N][N] ;
int d[4][2] = {{0,1},{0,-1},{1,0},{-1,0}} ;
int n , k ,t; 
bool v[N][N] ;
struct node{
  int x, y ,dis ;
  node(int xx, int yy , int diss){
    x = xx , y = yy , dis = diss ;
  }
};
queue<node> q;
 
 
bool check_5(int x,int y){
  if(v[x][y]) return false ;
  for(int i = x-2; i <= x +2 ; i ++){
    for(int j = y-2; j <= y +2 ; j ++){
      if(i < 0 || i >= n|| j < 0|| j >= n ) return false ;
      if(mp[i][j] == '*') return false ;
    }
  }
  return true ;
}
bool check_3(int x,int y){
  if(v[x][y]) return false ;
  for(int i = x-1; i <= x +1 ; i ++){
    for(int j = y-1; j <= y +1 ; j ++){
      if(i < 0 || i >= n|| j < 0|| j >= n ) return false ;
      if( mp[i][j] == '*') return false ;
    }
  }
  return true ;
}
bool check_1(int x,int y){
    if(v[x][y]) return false ;
    if(x < 0 || x >= n|| y < 0|| y >= n ) return false ;
    if( mp[x][y] == '*') return false ;
    return true ;
}
void bfs(){
  q.push(node(2,2,0)) ;
  while(!q.empty()){
    node now = q.front() ;
    q.pop() ;
    int x = now.x  , y = now.y  ;
    if(x == n-3 && y == n-3) {
      cout << now.dis << endl ; return ;
    }
    if(now.dis < k) t = 5 ;
    else if(now.dis >= k&& now.dis < 2*k) t = 3 ;
    else if(now.dis >= 2*k) t = 1; 
    
    if(t!=1) q.push(node(x,y,now.dis+1)) ;
    for(int i = 0 ; i < 4 ; i ++){
      int tx = x +d[i][0], ty = y + d[i][1] ;
      if(t==5){
        if(check_5(tx,ty) ) {
          v[tx][ty] = 1 ;
          q.push(node(tx,ty,now.dis+1)) ;
        }
        
      }
      else if(t==3) {
        if(check_3(tx,ty)){
          v[tx][ty] = 1 ;
          q.push(node(tx,ty,now.dis+1)) ;
        }
      }
      else if(t == 1){
        if(check_1(tx,ty)){
          v[tx][ty] = 1 ;
          q.push(node(tx,ty,now.dis+1)) ;
        }
      }
    }
  }
}
 
int main(){
  cin >> n >> k ;
  for(int i = 0; i < n ; i ++) cin >> mp[i] ;
  v[2][2] = 1 ;
  bfs();
  return 0 ;
}


相关文章
lanqiao OJ 364 跳石头
lanqiao OJ 364 跳石头
lanqiao OJ 22年省赛 扫雷
lanqiao OJ 22年省赛 扫雷
lanqiao OJ 229 迷宫与陷阱
lanqiao OJ 229 迷宫与陷阱
lanqiao OJ 102 青蛙跳杯子
lanqiao OJ 102 青蛙跳杯子
|
3月前
【洛谷】P2004 领地选择
洛谷 P2004 领地选择
36 2
【洛谷】P2004 领地选择
|
3月前
【洛谷】P1163 银行贷款
洛谷P1163 银行贷款
32 0
【洛谷】P1163 银行贷款
洛谷1102 A-B 暴力法
判断第 i 个数和 i 之后的每一个数的绝对值是否等于目标结果
|
存储 算法
『牛客|每日一题』走迷宫
基础算法无论在研究生面试还是求职面试都是十分重要的一环,这里推荐一款算法面试神器:牛客网-面试神器;算法题只有多刷勤刷才能保持思路与手感,大家赶紧行动起来吧(温馨提示:常见的面试问答题库也很nice哦 https://www.nowcoder.com/link/pc_csdncpt_ll_sf
117 0