zoukankan      html  css  js  c++  java
  • poj 1376 bfs

    题很简单....但是好多字啊...

    唉,,,题意没看清...竟然忘了这个机器人很肥,不能走边界.....

    还有就是有个track,起始点与目的地如果有障碍,直接输出-1..

    太水了,,简单的题还是错...

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <algorithm>
      5 #include <queue>
      6 
      7 using namespace std;
      8 
      9 #define MAXN 55
     10 
     11 struct point
     12 {
     13     int x,y;
     14     int step;
     15     int face;
     16 };
     17 int n,m;
     18 int map[MAXN][MAXN];
     19 point st,en;
     20 bool vis[MAXN][MAXN][4];
     21 int bfs(point s)
     22 {
     23     memset(vis,0,sizeof(vis));
     24     point cur,tmp;
     25     queue<point>q;
     26     while(!q.empty())
     27         q.pop();
     28     q.push(s);
     29     vis[s.x][s.y][s.face]=1;
     30     while(!q.empty())
     31     {
     32         cur=q.front();
     33         q.pop();
     34         if(cur.x==en.x && cur.y==en.y)
     35             return cur.step;
     36         if(cur.face==0)
     37         {
     38             tmp.x=cur.x;
     39             tmp.y=cur.y;
     40             tmp.face=cur.face;
     41             tmp.step=cur.step+1;
     42             for(int i=1;i<=3;i++)
     43             {
     44                 tmp.y=cur.y-i;
     45                 if(tmp.y<1)//机器人比较肥
     46                     break;
     47                 if(map[tmp.x][tmp.y])
     48                     break;
     49                 if(vis[tmp.x][tmp.y][tmp.face])
     50                     continue;
     51                 vis[tmp.x][tmp.y][tmp.face]=1;
     52                 q.push(tmp);
     53             }
     54             tmp.x=cur.x;
     55             tmp.y=cur.y;
     56             tmp.step=cur.step+1;
     57             tmp.face=1;
     58             if(!vis[tmp.x][tmp.y][tmp.face])
     59             {    
     60                 vis[tmp.x][tmp.y][tmp.face]=1;
     61                 q.push(tmp);
     62             }
     63             tmp.x=cur.x;
     64             tmp.y=cur.y;
     65             tmp.step=cur.step+1;
     66             tmp.face=3;
     67             if(!vis[tmp.x][tmp.y][tmp.face])
     68             {    
     69                 vis[tmp.x][tmp.y][tmp.face]=1;
     70                 q.push(tmp);
     71             }
     72         }
     73         else if(cur.face==1)
     74         {
     75             tmp.x=cur.x;
     76             tmp.y=cur.y;
     77             tmp.face=cur.face;
     78             tmp.step=cur.step+1;
     79             for(int i=1;i<=3;i++)
     80             {
     81                 tmp.x=cur.x-i;
     82                 if(tmp.x<1)
     83                     break;
     84                 if(map[tmp.x][tmp.y])
     85                     break;
     86                 if(vis[tmp.x][tmp.y][tmp.face])
     87                     continue;
     88                 vis[tmp.x][tmp.y][tmp.face]=1;
     89                 q.push(tmp);
     90             }
     91             tmp.x=cur.x;
     92             tmp.y=cur.y;
     93             tmp.step=cur.step+1;
     94             tmp.face=0;
     95             if(!vis[tmp.x][tmp.y][tmp.face])
     96             {    
     97                 vis[tmp.x][tmp.y][tmp.face]=1;
     98                 q.push(tmp);
     99             }
    100             tmp.x=cur.x;
    101             tmp.y=cur.y;
    102             tmp.step=cur.step+1;
    103             tmp.face=2;
    104             if(!vis[tmp.x][tmp.y][tmp.face])
    105             {    
    106                 vis[tmp.x][tmp.y][tmp.face]=1;
    107                 q.push(tmp);
    108             }
    109         }
    110         else if(cur.face==2)
    111         {
    112             tmp.x=cur.x;
    113             tmp.y=cur.y;
    114             tmp.face=cur.face;
    115             tmp.step=cur.step+1;
    116             for(int i=1;i<=3;i++)
    117             {
    118                 tmp.y=cur.y+i;
    119                 if(tmp.y>m-1)
    120                     break;
    121                 if(map[tmp.x][tmp.y])
    122                     break;
    123                 if(vis[tmp.x][tmp.y][tmp.face])
    124                     continue;
    125                 vis[tmp.x][tmp.y][tmp.face]=1;
    126                 q.push(tmp);
    127             }
    128             tmp.x=cur.x;
    129             tmp.y=cur.y;
    130             tmp.step=cur.step+1;
    131             tmp.face=1;
    132             if(!vis[tmp.x][tmp.y][tmp.face])
    133             {    
    134                 vis[tmp.x][tmp.y][tmp.face]=1;
    135                 q.push(tmp);
    136             }
    137             tmp.x=cur.x;
    138             tmp.y=cur.y;
    139             tmp.step=cur.step+1;
    140             tmp.face=3;
    141             if(!vis[tmp.x][tmp.y][tmp.face])
    142             {    
    143                 vis[tmp.x][tmp.y][tmp.face]=1;
    144                 q.push(tmp);
    145             }
    146         }
    147         else if(cur.face==3)
    148         {
    149             tmp.x=cur.x;
    150             tmp.y=cur.y;
    151             tmp.face=cur.face;
    152             tmp.step=cur.step+1;
    153             for(int i=1;i<=3;i++)
    154             {
    155                 tmp.x=cur.x+i;
    156                 if(tmp.x>n-1)
    157                     break;
    158                 if(map[tmp.x][tmp.y])
    159                     break;
    160                 if(vis[tmp.x][tmp.y][tmp.face])
    161                     continue;
    162                 vis[tmp.x][tmp.y][tmp.face]=1;
    163                 q.push(tmp);
    164             }
    165             tmp.x=cur.x;
    166             tmp.y=cur.y;
    167             tmp.step=cur.step+1;
    168             tmp.face=0;
    169             if(!vis[tmp.x][tmp.y][tmp.face])
    170             {    
    171                 vis[tmp.x][tmp.y][tmp.face]=1;
    172                 q.push(tmp);
    173             }
    174             tmp.x=cur.x;
    175             tmp.y=cur.y;
    176             tmp.step=cur.step+1;
    177             tmp.face=2;
    178             if(!vis[tmp.x][tmp.y][tmp.face])
    179             {    
    180                 vis[tmp.x][tmp.y][tmp.face]=1;
    181                 q.push(tmp);
    182             }
    183         }
    184     }
    185     return -1;
    186 }
    187 
    188 int main()
    189 {
    190     char c[10];
    191     while(scanf("%d%d",&n,&m) && (n+m))
    192     {
    193         memset(map,0,sizeof(map));
    194         for(int i=0;i<n;i++)
    195             for(int j=0;j<m;j++)
    196             {
    197                 int d;
    198                 scanf("%d",&d);
    199                 if(d)
    200                 {
    201                     map[i][j]=1;
    202                     map[i+1][j]=1;
    203                     map[i+1][j+1]=1;
    204                     map[i][j+1]=1;
    205                 }
    206             }
    207         scanf("%d%d",&st.x,&st.y);
    208         scanf("%d%d",&en.x,&en.y);
    209         /*for(int i=0;i<=n;i++)
    210         {
    211             for(int j=0;j<=m;j++)
    212                 printf("%d ",map[i][j]);
    213             printf("\n");
    214         }*/
    215         scanf("%s",&c);
    216         //cout<<"face="<<c<<endl;
    217         if(c[0]=='w')
    218             st.face=0;
    219         else if(c[0]=='n')
    220             st.face=1;
    221         else if(c[0]=='e')
    222             st.face=2;
    223         else if(c[0]=='s')
    224             st.face=3;
    225         st.step=0;
    226         if(map[st.x][st.y] || map[en.x][en.y])//track
    227             puts("-1");
    228         else
    229             printf("%d\n",bfs(st));
    230     }
    231     return 0;
    232 }
  • 相关阅读:
    redis主从同步机制
    聊聊Redis持久化
    Redis集群架构中主节点选举机制
    命令注入
    《小狗钱钱》
    nginx---反向代理缓存
    nginx ---IP地址透传
    实现前端调度器nginx收到请求,调度到后端Apache、实现动静分离
    nginx---防止盗链
    nginx--当用户访问到公司网站的时输入了一个错误的URL,可以将用户重定向至官网首页
  • 原文地址:https://www.cnblogs.com/Missa/p/2709511.html
Copyright © 2011-2022 走看看