奇偶剪枝
这个奇偶剪枝相当牛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

#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 2012-03-27 23:33 More study needed. 阅读(205) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架