您的位置:首页 > 其它

HDU 1010 Tempter of the Bone

2015-02-17 20:13 204 查看
一道搜索题,需要加奇偶剪枝,需要注意的地方是题目要求在T时刻时刚好到达D位置。

顺带吐槽一下,调试的时候遇到各种奇葩错误,例如去掉提示信息后,答案居然不一样,尼玛,这么会有这种错误啊啊啊啊。

G++提交WA,C++提交AC又是什么鬼啊

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
int N,M,T;
int sx,sy,ex,ey;
char maze[10][10];
bool flag[10][10];
int way[4][2]={1,0,-1,0,0,1,0,-1};
bool check(int x,int y){
if(x>=0&&x<N&&y>=0&&y<M){
//cout<<x<<" "<<y<<endl;
return true;
}
else
false;
}
bool dfs(int x,int y,int t){
// printf("%d %d %d\n",x,y,t);
bool pos;
flag[x][y]=true;
if(maze[x][y]=='D'&&t==T){
//cout<<"***"<<x<<" "<<y<<endl;
return true;
}
else if((T-(abs(sx-ex)+abs(sy-ey)))%2!=0)
return false;
for(int i=0;i<4;i++){
int dx=x+way[i][0];
int dy=y+way[i][1];
if(check(dx,dy)&&(maze[dx][dy]=='.'||maze[dx][dy]=='D')&&!flag[dx][dy]){
pos=dfs(dx,dy,t+1);
if(pos)
break;
else
flag[dx][dy]=false;
}
}
if(pos)
return true;
else
return false;
}
int main(){
while(scanf("%d%d%d",&N,&M,&T)!=EOF&&(N||M||T)){
// cout<<N<<" "<<M<<" "<<T<<endl;
memset(flag,false,sizeof(flag));
memset(maze,'\0',sizeof(maze));
for(int i=0;i<N;i++){
scanf("%s",maze[i]);
}
for(int i=0;i<N;i++){
for(int j=0;j<M;j++){
if(maze[i][j]=='S'){
sx=i;
sy=j;
}
else if(maze[i][j]=='D'){
ex=i;
ey=j;
}
}
}
bool flag1=dfs(sx,sy,0);
//cout<<flag1<<endl;
if(flag1)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: