书山有径勤为路>>>>>>>>

<<<<<<<<学海无涯苦作舟!

奇偶剪枝

这个奇偶剪枝相当牛B呀,佩服。

HDU 1010 http://acm.hdu.edu.cn/showproblem.php?pid=1010

题目大意:就是让你来找一下,能否在限定的时间内从S到达D.

Sample Input:

4 4 5

S.X.

..X.

..XD

.... 

Sample Output:

NO 


复制代码
View Code
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;

char Map[8][8];
int Used[8][8];
int Dir[4][2]={{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
int H, W, T, Done;
int Sx, Sy, Ex, Ey;

int DFS(int h, int w, int Step)
{
int k;
if(Done==1) return 0;
if(Step==T)
{
if(h==Ex && w==Ey) Done=1;
return 0;
}

if(Step > T) return 0; //剪枝1

if(abs(Ex-h)+abs(Ey-w) > T-Step) return 0; //剪枝2

if((abs(Ex-h)+abs(Ey-w))%2 != (T-Step)%2) return 0; //剪枝3 这个就是奇偶性剪枝了,相当牛B

for(k=0; k<4; k++)
{
int tx = h+Dir[k][0];
int ty = w+Dir[k][1];
if(tx>=0 && tx<H && ty>=0 && ty<W && Used[tx][ty]==0 && Map[tx][ty]!='X')
{
Used[tx][ty] = 1;
DFS(tx, ty, Step+1);
Used[tx][ty] = 0;
}
}
}

int main()
{
int i, j;
while(cin>>H>>W>>T && (H+W+T))
{
for(i=0; i<H; i++)
for(j=0; j<W; j++)
{
cin>>Map[i][j];
if(Map[i][j]=='S')
{
Sx = i;
Sy = j;
}
if(Map[i][j]=='D')
{
Ex = i;
Ey = j;
}
}
Done = 0;
memset(Used, 0, sizeof(Used));
Used[Sx][Sy] = 1;
DFS(Sx, Sy, 0);
if(Done==1) cout<<"YES"<<endl;
else cout<<"NO"<<endl;

}
}
复制代码



posted on   More study needed.  阅读(205)  评论(0编辑  收藏  举报

编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
< 2012年3月 >
26 27 28 29 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
1 2 3 4 5 6 7

导航

统计

书山有径勤为路>>>>>>>>

<<<<<<<<学海无涯苦作舟!

点击右上角即可分享
微信分享提示