您的位置:首页 > 其它

UVALive-4793 Robots on Ice 搜索加剪枝

2017-11-17 17:04 477 查看
题意:





#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: