Tempter of the Bone
来源:互联网 发布:汕头宠物店软件 编辑:程序博客网 时间:2024/05/09 08:17
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 47139 Accepted Submission(s): 12660
The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.
'X': a block of wall, which the doggie cannot enter;
'S': the start point of the doggie;
'D': the Door; or
'.': an empty block.
The input is terminated with three 0's. This test case is not to be processed.
4 4 5S.X...X...XD....3 4 5S.X...X....D0 0 0
NOYES
深搜(DFS)
注意剪枝+奇偶剪枝《重点》
#include<iostream>
using namespace std;
char s[10][10];
int s_b,s_e,d_b,d_e;
int vi[10][10];
int move[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
int min1;
int n,m,t;
void DFS(int a,int b,int num)
{
int i;
/* if(a<0||a>=n||b<0||b>=m)
return ;*/
if(min1==t)
return ;
if(a==d_b&&b==d_e)
{
if(num==t)
{
min1=num;
return ;
}
else
return ;
}
int temp=abs(a-d_b)+abs(b-d_e); //奇偶剪枝
temp=t-temp-num;
if(temp&1||temp<0) return;
if(num>t)
return ;
else
{
for(i=0;i<4;i++){
if((a+move[i][0])>=0&&(a+move[i][0])<n&&(b+move[i][1])>=0&&(b+move[i][1])<m&&s[a+move[i][0]][b+move[i][1]]!='X')
if(!vi[a+move[i][0]][b+move[i][1]]&&s[a+move[i][0]][b+move[i][1]]!='X'){
vi[a+move[i][0]][b+move[i][1]]=1;
DFS(a+move[i][0],b+move[i][1],num+1);
vi[a+move[i][0]][b+move[i][1]]=0;
}
}
}
}
int main()
{
while(cin>>n>>m>>t&&(m&&n&&t)){
int i,j;
min1=100000000;
memset(vi,0,sizeof(vi));
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
cin>>s[i][j];
if(s[i][j]=='S')
{
s_b=i;
s_e=j;
}
if(s[i][j]=='D')
{
d_b=i;
d_e=j;
}
}
int num=0;
vi[s_b][s_e]=1;
DFS(s_b,s_e,num);
if(min1!=t)
cout<<"NO"<<endl;
else
cout<<"YES"<<endl;
}
return 0;
}
- Tempter of the Bone
- Tempter of the Bone
- Tempter of the Bone
- Tempter of the Bone
- Tempter of the Bone
- Tempter of the Bone
- Tempter of the Bone
- Tempter of the Bone
- Tempter of the bone
- Tempter of the Bone
- Tempter of the Bone
- Tempter of the Bone
- Tempter of the Bone
- Tempter of the Bone
- Tempter of the Bone
- Tempter of the Bone
- Tempter of the Bone
- Tempter of the Bone
- CWinApp
- RAM与Nand/Nor flash之间的区别
- 树状数组
- 【程序39】数组,插入数据
- TCP打洞和UDP打洞的区别
- Tempter of the Bone
- Android之百度地图 前期实现
- 标准C++中有四个类型转换符
- java笔记 String类中的方法总结
- cocos2d-iphone-2.0-rc2 API文档在Xcode中查阅显示
- [Ubuntu]安装jdk7
- android4.0.3 修改启动动画和开机声音
- 云计算技术的产生、概念、原理、应用和前景
- POJ 1062