UVALive-4793 Robots on Ice 搜索加剪枝

题意:


#include <bits/stdc++.h>
using namespace std;
const int maxn = 11;
int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
int n, m, t;
int x[5], y[5], c[5];//第i个检查点的坐标为(x[i],y[i]),访问时间为c[i]
int mp[maxn][maxn];//存储网格中的检查点序号
bool check[maxn][maxn];//格子的访问标记
int ans;
int b[4];
bool cut(int x, int y)//判断四周未走过的格子是否被截成2部分
{
    for(int i=0; i<4; i++){
        int tx = x+dir[i][0];
        int ty = y+dir[i][1];
        b[i] = ((tx>=0)&&tx<n&&ty>=0&&ty<m&&!check[tx][ty]);
    }
    return (b[0]&&b[2]&&!b[1]&&!b[3])||(!b[0]&&!b[2]&&b[1]&&b[3]);
}
void dfs(int x1, int y1, int c1, int dep){//递归计算可行的路径数(当前格为(x1,y1),步数为dep,待检查的点为c1)
    if(dep==c[c1]){//走完了可达c1检查点的时间。若到达c1检查点,则准备访问下一个检查点,否则剪枝
        if(mp[x1][y1]==c1) ++c1;
        else return;
    }else if(mp[x1][y1]>0) return;//没到时间就走到了检查点上就剪枝
    if(dep == t){
        ++ans;
        return;
    }
    if(abs(x[c1]-x1)+abs(y[c1]-y1)>c[c1]-dep) return;
    for(int i=0; i<4; i++){
        int tx = x1+dir[i][0];
        int ty = y1+dir[i][1];
        if(tx>=0&&tx<n&&ty>=0&&ty<m&&!check[tx][ty]&&!cut(tx,ty)){//若(tx,ty)在界内且未被访问,且4周未走过的格子未被其截成2部分,则访问该格子
            check[tx][ty] = 1;
            dfs(tx, ty, c1, dep+1);
            check[tx][ty] = 0;
        }
    }
}
int main(){
    int ks = 0;
    while(~scanf("%d %d", &n,&m))
    {
        if(n==0 && m==0) break;
        memset(mp, 0, sizeof(mp));
        t = n*m;
        for(int i=1; i<=3; i++){
            scanf("%d %d", &x[i], &y[i]);
            c[i] = i*t/4;
            mp[x[i]][y[i]] = i;
        }
        x[4] = 0, y[4] = 1, c[4] = t, mp[0][1] = 4;
        ans = 0;
        memset(check, 0, sizeof(check));
        check[0][0] = 1;
        dfs(0, 0, 1, 1);
        printf("Case %d: %d\n", ++ks, ans);
    }
    return 0;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值