zoukankan      html  css  js  c++  java
  • hdu 1728 bfs **

    简单bfs,记录好状态即可

      1 #include<cstdio>
      2 #include<iostream>
      3 #include<algorithm>
      4 #include<cstring>
      5 #include<cmath>
      6 #include<queue>
      7 #include<map>
      8 using namespace std;
      9 #define MOD 1000000007
     10 const int INF=0x3f3f3f3f;
     11 const double eps=1e-5;
     12 typedef long long ll;
     13 #define cl(a) memset(a,0,sizeof(a))
     14 #define ts printf("*****
    ");
     15 const int MAXN=105;
     16 int n,m,tt,k;
     17 int dir[4][2]={1,0,-1,0,0,1,0,-1};
     18 char s[MAXN][MAXN];
     19 struct Node
     20 {
     21     int x,y;
     22     int st;
     23     int t;
     24 }node[MAXN],st,ed;
     25 bool vis[MAXN][MAXN][5][15];
     26 bool bfs()
     27 {
     28     queue<Node> q;
     29     cl(vis);
     30     Node now,next;
     31     st.t=0;
     32     st.st=0;
     33     vis[st.x][st.y][st.st][st.t]=1;
     34     q.push(st);
     35     st.st=1;
     36     vis[st.x][st.y][st.st][st.t]=1;
     37     q.push(st);
     38     st.st=2;
     39     vis[st.x][st.y][st.st][st.t]=1;
     40     q.push(st);
     41     st.st=3;
     42     vis[st.x][st.y][st.st][st.t]=1;
     43     q.push(st);
     44     while(!q.empty())
     45     {
     46         now=q.front();
     47         q.pop();
     48         if(now.x==ed.x&&now.y==ed.y)
     49         {
     50             return 1;
     51         }
     52         for(int i=0;i<4;i++)
     53         {
     54             next.x=now.x+dir[i][0];
     55             next.y=now.y+dir[i][1];
     56             next.t=now.t;
     57             next.st=i;
     58             if(i!=now.st)   next.t+=1,next.st=i;
     59             if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&!vis[next.x][next.y][i][next.t]&&s[next.x][next.y]!='*'&&next.t<=k)
     60             {
     61                 if(now.st==i)
     62                 {
     63                     q.push(next);
     64                     vis[next.x][next.y][i][next.t]=1;
     65                 }
     66                 else
     67                 {
     68                     q.push(next);
     69                     vis[next.x][next.y][i][next.t]=1;
     70                 }
     71             }
     72         }
     73     }
     74     return 0;
     75 }
     76 int main()
     77 {
     78     int i,j;
     79     #ifndef ONLINE_JUDGE
     80     freopen("1.in","r",stdin);
     81     #endif
     82     scanf("%d",&tt);
     83     while(tt--)
     84     {
     85         scanf("%d%d",&n,&m);
     86         for(i=0;i<n;i++)
     87         {
     88             scanf("%s",&s[i]);
     89         }
     90         scanf("%d%d%d%d%d",&k,&st.y,&st.x,&ed.y,&ed.x);
     91         st.x-=1;
     92         st.y-=1;
     93         ed.x-=1;
     94         ed.y-=1;
     95         if(bfs())
     96         {
     97             printf("yes
    ");
     98         }
     99         else    printf("no
    ");
    100     }
    101 }
  • 相关阅读:
    idea项目maven配置
    IDEA配置maven项目启动
    IDEA导入本地项目教程
    EasyUi前端js禁止短时间内多次点击按钮方法
    EasyUi的form验证框位置修改
    div中的div如何居中
    Java导出Excel使用POI之SXSSFWorkbook方式
    java.lang.NoSuchMethodError
    eclipse导出项目时报错:problems were encountered during export
    直线检测方法—LSD论文翻译
  • 原文地址:https://www.cnblogs.com/cnblogs321114287/p/4675019.html
Copyright © 2011-2022 走看看