zoukankan      html  css  js  c++  java
  • POJ3322Bloxorz I

    POJ3322 Bloxorz I 

    暴搜,next数组与处理一下(小技巧)

      1 #include <cstdio>
      2 #include <iostream>
      3 #include <cstring>
      4 #include <cmath>
      5 #include <queue>
      6 #include <algorithm>
      7 using namespace std;
      8 
      9 #define res register int
     10 const int N=510;
     11 char s[N][N];
     12 int n,m,d[N][N][3];
     13 struct node{int x,y,lie;};//lie=0 立 lie=1 横向躺 lie=2 纵向躺 
     14 node st,ed;
     15 queue<node> q;
     16 const int dx[4]={0,0,-1,1},dy[4]={-1,1,0,0};
     17 
     18 inline bool valid(int x,int y){
     19     return x>0&&x<=n&&y>0&&y<=m;
     20 }
     21 
     22 inline void read()
     23 {
     24     for(res i=1 ; i<=n ; i++)
     25         for(res j=1 ; j<=m ; j++) cin>>s[i][j];
     26     for(res i=1 ; i<=n ; i++)
     27         for(res j=1 ; j<=m ; j++)
     28         {
     29             if(s[i][j]=='O') ed.x=i,ed.y=j,ed.lie=0,s[i][j]='.';
     30             else if(s[i][j]=='X')
     31             {
     32                 for(res k=0 ; k<4 ; k++)
     33                 {
     34                     int x=i+dx[k],y=j+dy[k];
     35                     if(valid(x,y) && s[x][y] == 'X') {
     36                         st.x=min(i,x),st.y=min(j,y),st.lie=k<2?1:2;
     37                         s[i][j]=s[x][y]='.';
     38                         break;
     39                     } 
     40                     if(s[i][j]=='X') st.x=i,st.y=j,st.lie=0,s[i][j]='.';
     41                 } 
     42             }
     43         }
     44 }
     45 
     46 const int next_x[3][4]={{0,0,-2,1},{0,0,-1,1},{0,0,-1,2}};
     47 const int next_y[3][4]={{-2,1,0,0},{-1,2,0,0},{-1,1,0,0}};
     48 const int next_lie[3][4]={{1,1,2,2},{0,0,1,1},{2,2,0,0}};
     49 
     50 inline bool valid(node t)
     51 {
     52     if(!valid(t.x,t.y)) return false;
     53     int nx=t.x,ny=t.y;
     54     if(s[nx][ny]=='#') return false;
     55     if(t.lie==0 && s[nx][ny]!='.') return false;
     56     if(t.lie==1 && s[nx][ny+1]=='#') return false;
     57     if(t.lie==2 && s[nx+1][ny]=='#') return false;
     58     return true;
     59 }
     60 
     61 inline int bfs()
     62 {
     63     for(res i=1 ; i<=n ; i++)
     64         for(res j=1 ; j<=m ; j++) 
     65             for(res k=0 ; k<3 ; k++)    d[i][j][k]=-1;
     66     while(q.size()) q.pop();
     67     d[st.x][st.y][st.lie]=0; q.push(st);
     68     while(q.size())
     69     {
     70         node now=q.front(); q.pop();
     71         for(res i=0 ; i<4 ; i++)
     72         {
     73             node t;
     74             t.x=now.x+next_x[now.lie][i];
     75             t.y=now.y+next_y[now.lie][i];
     76             t.lie=next_lie[now.lie][i];
     77             if(!valid(t)) continue;
     78             if(d[t.x][t.y][t.lie]==-1)
     79             {
     80                 d[t.x][t.y][t.lie]=d[now.x][now.y][now.lie]+1;
     81                 q.push(t);
     82                 if(t.x==ed.x && t.y==ed.y && t.lie==ed.lie) 
     83                     return d[t.x][t.y][t.lie];
     84             }
     85         }
     86     }
     87     return -1;    
     88 }
     89 
     90 int main()
     91 {
     92     while(scanf("%d %d",&n,&m)==2 && n && m)
     93     {
     94         read();
     95         int ans=bfs();
     96         if(ans==-1) puts("Impossible");
     97         else printf("%d
    ",ans);
     98     }
     99     
    100     return 0;
    101 }
    View Code
  • 相关阅读:
    Android studio的基本使用
    Java web项目基本介绍
    关于缓存的问题
    tomcat的基本使用
    https的页面内嵌入http页面报错的问题
    RTTHREAD使用sscanf遇到的问题
    Supervisor安装与配置(Linux/Unix进程管理工具)
    MYSQL 检查表的元数据是否有异常
    多表select count语句生成 带格式化
    MYSQL压缩表测试
  • 原文地址:https://www.cnblogs.com/wmq12138/p/10381583.html
Copyright © 2011-2022 走看看