zoukankan      html  css  js  c++  java
  • hdoj1180 诡异的楼梯(bfs+奇偶判断)

    手癌!日常手癌!被自己气死!

     1 #include<iostream>
     2 #include<cstring>
     3 #include<queue>
     4 #include<algorithm>
     5 #define maxn 25
     6 using namespace std;
     7 const int dx[4]={-1,1,0,0},dy[4]={0,0,-1,1};
     8 struct point{
     9     int x,y;
    10     int time;
    11 }start;
    12 char map[maxn][maxn];
    13 int v[maxn][maxn];
    14 int n,m,ans;
    15 int check(point next){
    16     if (map[next.x][next.y]=='*' || next.x<0 || next.x>=n || next.y<0 || next.y>=m || v[next.x][next.y]) return 1;
    17     else return 0;
    18 }
    19 int bfs(point start){
    20     queue<point> Q;
    21     Q.push(start);
    22     v[start.x][start.y]=1;
    23     while (!Q.empty()){
    24         point pre=Q.front();
    25         Q.pop();
    26         if (map[pre.x][pre.y]=='T'){
    27             return pre.time;
    28         }
    29         for (int i=0;i<4;i++){
    30             point next;
    31             next.x=pre.x+dx[i];
    32             next.y=pre.y+dy[i];
    33             next.time=pre.time+1;
    34             if (check(next)) continue;
    35             if (map[next.x][next.y]=='.' || map[next.x][next.y]=='T'){
    36                 Q.push(next);
    37                 v[next.x][next.y]=1;
    38             }
    39             else if (map[next.x][next.y]=='|' && !(pre.time&1) || map[next.x][next.y]=='-' && (pre.time&1)){  //表示横着走 
    40                 if (i>1){
    41                     next=pre;
    42                     next.time++;
    43                     Q.push(next);
    44                     continue; 
    45                 }
    46                 next.x+=dx[i];
    47                 if (check(next)) continue;
    48                 Q.push(next);
    49                 v[next.x][next.y]=1; 
    50             }
    51             else {  // 表示竖着走 
    52                 if (i<2){
    53                     next=pre;
    54                     next.time++;
    55                     Q.push(next);
    56                     continue;
    57                 }
    58                 next.y+=dy[i];
    59                 if (check(next)) continue;
    60                 Q.push(next);
    61                 v[next.x][next.y]=1;
    62             }
    63         }
    64     }
    65 }
    66 int main(){
    67     while (cin >> n >> m){
    68         for (int i=0;i<n;i++){
    69             for (int j=0;j<m;j++){
    70                 cin >> map[i][j];
    71                 if (map[i][j]=='S'){
    72                     start.x=i;
    73                     start.y=j;
    74                     start.time=0; 
    75                 }
    76             }
    77         }
    78         memset(v,0,sizeof(v));
    79         ans=bfs(start);
    80         cout << ans << endl;
    81     }
    82     return 0;
    83 }
  • 相关阅读:
    基于MQTT协议进行应用开发
    shell ss命令
    组播基本概念、IGMP、IGMP监听学习笔记
    关于Android的HAL的一些理解
    Android HAL层与Linux Kernel层驱动开发简介
    YPBPR_PC下图像有毛刺或者水纹干扰的处理办法
    CHAKRA3 UART2
    实际用户ID和有效用户ID (三) *****
    svn使用svnsync实现双机热备
    pyspider介绍及安装
  • 原文地址:https://www.cnblogs.com/changer-qyz/p/8557433.html
Copyright © 2011-2022 走看看