UVALive-4793 Robots on Ice 搜索加剪枝
2017-11-17 17:04
477 查看
题意:
![](http://img.blog.csdn.net/20171117170353098?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvanVzdF9zb3J0/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20171117170407492?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvanVzdF9zb3J0/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
#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;
}
#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;
}
相关文章推荐
- Regionals 2009 >> Asia - Hsinchu UVALIVE, 4529 A Constrained Queen Game - 搜索剪枝
- 【UVALive 7364】Robots(逆向思维+搜索)
- UVALive 6432 Influence 搜索 剪枝
- Regionals 2009 :: Asia - Hsinchu UVALIVE, 4527 Vaccination Centers - 搜索剪枝
- UVALive 4527 Vaccination Centers 搜索+剪枝
- UVa 208 - Firetruck,双向搜索进行剪枝
- uvalive 3635 - Pie(二分搜索)
- UVALive-7354-Kitchen Measurements(BFS搜索)
- UVALive3135 UVA1203 POJ2051 ZOJ2212 Argus【优先队列+优先搜索】
- UVA 649 - You Who?(搜索+位运算+剪枝)
- UVA 690 Pipeline Scheduling (搜索+位运算+剪枝)
- UVALive-8072 Keeping On Track 树形dp 联通块之间缺失边的个数
- UVALive 6953 Digi Comp II(搜索)
- UVALive 3907 (LA 3907) Puzzle AC自动机 + 搜索DP 记录路径
- UVALive 5876 - Writings on the Wall 【KMP】
- UVALive - 4356 Fire-Control System 三角函数+暴力+剪枝
- UVALive - 3902 Network 无根树转为有跟树+搜索+贪心
- uvalive 3971 - Assemble(二分搜索 + 贪心)
- UVALive 7279 Sheldon Numbers 【位运算】【暴力】
- UVALive 6606 Meeting Room Arrangement 【搜索】