csp 201604-4 游戏

bfs

虽然可以重复到某个点

但是同一时间维度上不能到达同一个点(因为重复时间访问同一个点不可能得到最优解)

用vis[maxn][maxn][maxt] 来记录

 #include<bits/stdc++.h>
 using namespace std;
 #define maxn 110
 struct node
 {
     int tp;
     int t1;
     int t2;
     node(int _tp=0,int _t1=0,int _t2=0):tp(_tp),t1(_t1),t2(_t2) {};
 };
 struct point
 {
     int x;
     int y;
     int h;
 };
 int dx[4]={0,1,0,-1};
 int dy[4]={1,0,-1,0};
 node mp[maxn][maxn];
 bool vis[maxn][maxn][10010];
 int n,m,t;

 int bfs()
 {
     memset(vis,false,sizeof vis);
     point st;
     st.x=1;
     st.y=1;
     st.h=0;
     vis[1][1][0]=true;
     queue<point> q;
     q.push(st);
     point t;
     while(!q.empty())
     {
         t=q.front();
         q.pop();
         int sx=t.x;
         int sy=t.y;
         int h=t.h;
         for(int i=0;i<4;i++)
         {
             int ex=sx+dx[i];
             int ey=sy+dy[i];
             int eh=h+1;
             if(!vis[ex][ey][eh]&&ex>=1&&ex<=n&&ey>=1&&ey<=m)
             {
                 if(mp[ex][ey].tp==1)
                 {
                     int t1=mp[ex][ey].t1;
                     int t2=mp[ex][ey].t2;
                     if(eh>=t1&&eh<=t2)
                        continue;
                 }
                 if(ex==n&&ey==m)
                 {
                     return eh;
                 }
                 point tmp;
                 tmp.x=ex;
                 tmp.y=ey;
                 tmp.h=eh;
                 q.push(tmp);
                 vis[ex][ey][eh]=true;
             }
         }
     }
}

 int main()
 {
     cin>>n>>m>>t;
     for(int i=1;i<=t;i++)
     {
         int r,c,a,b;
         cin>>r>>c>>a>>b;
         mp[r][c].tp=1;
         mp[r][c].t1=a;
         mp[r][c].t2=b;
     }
     cout<<bfs()<<endl;
     return 0;
 }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值