hud1001
来源:互联网 发布:黑龙江广联达软件购买 编辑:程序博客网 时间:2024/06/08 13:32
Tempter of the Bone
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 173 Accepted Submission(s) : 64
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.
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.
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.
Input
The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following:
'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.
'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.
Output
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
Sample Input
4 4 5S.X...X...XD....3 4 5S.X...X....D0 0 0
Sample Output
NOYES
Author
Source
ZJCPC2004
这是一题dfs+剪枝=因为dfs的运算比较大=所以不剪枝会造成超时=
不加剪枝的代码如下红字为剪枝
#include<iostream>
#include<cmath>
using namespace std;
char mp[8][8];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int dx,dy,zx,zy;
int flag;
int n,m,t;
int cheak(int xx,int yy)
{
if(xx<0||xx>=n||yy<0||yy>=m||mp[xx][yy]=='X')
{return 0;}
return 1;
}
void dfs( int dx,int dy,int t)
{
if(dx==zx&&dy==zy&&t==0)
{
flag=1;
return;
}
int tem;
tem=t-abs(dx-zx)-abs(dy-zy);
if(tem<0||tem%2)
{
return;
}
for(int i=0;i<4;i++)
{
int xx=dx+dir[i][0];
int yy=dy+dir[i][1];
if(cheak(xx,yy))
{
mp[xx][yy]='X';
dfs(xx,yy,t-1);
if(flag)
return;
mp[dx+dir[i][0]][yy=dy+dir[i][1]]='.';
}
}
return;
}
int main()
{
while(cin>>n>>m>>t)
{ flag=0;
int zhangai=0;
if(n==0&&m==0&&t==0)
break;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
cin>>mp[i][j];
}
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(mp[i][j]=='S')
{
dx=i;
dy=j;
}
if(mp[i][j]=='D')
{
zx=i;
zy=j;
}
if(mp[i][j]=='X')
zhangai++;
}
if(m*n-zhangai<t)
{
cout<<"NO"<<endl;
continue;
}
mp[dx][dy]='X';
dfs(dx,dy,t);
if(flag==1)
cout<<"YES"<<endl;
if(flag==0)
cout<<"NO"<<endl;
}
return 0;
}
#include<cmath>
using namespace std;
char mp[8][8];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int dx,dy,zx,zy;
int flag;
int n,m,t;
int cheak(int xx,int yy)
{
if(xx<0||xx>=n||yy<0||yy>=m||mp[xx][yy]=='X')
{return 0;}
return 1;
}
void dfs( int dx,int dy,int t)
{
if(dx==zx&&dy==zy&&t==0)
{
flag=1;
return;
}
int tem;
tem=t-abs(dx-zx)-abs(dy-zy);
if(tem<0||tem%2)
{
return;
}
for(int i=0;i<4;i++)
{
int xx=dx+dir[i][0];
int yy=dy+dir[i][1];
if(cheak(xx,yy))
{
mp[xx][yy]='X';
dfs(xx,yy,t-1);
if(flag)
return;
mp[dx+dir[i][0]][yy=dy+dir[i][1]]='.';
}
}
return;
}
int main()
{
while(cin>>n>>m>>t)
{ flag=0;
int zhangai=0;
if(n==0&&m==0&&t==0)
break;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
cin>>mp[i][j];
}
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(mp[i][j]=='S')
{
dx=i;
dy=j;
}
if(mp[i][j]=='D')
{
zx=i;
zy=j;
}
if(mp[i][j]=='X')
zhangai++;
}
if(m*n-zhangai<t)
{
cout<<"NO"<<endl;
continue;
}
mp[dx][dy]='X';
dfs(dx,dy,t);
if(flag==1)
cout<<"YES"<<endl;
if(flag==0)
cout<<"NO"<<endl;
}
return 0;
}
0 0
- hud1001
- 微信小程序,危机了谁?
- 又拍云直播录制
- 每天一个linux命令(61):wget命令
- week2th、 Generate SW package for Simulator and create simulator
- 杀死后台程序
- hud1001
- 内网环境NTP服务及时间同步(CentOS6.x)配置和部署
- Struts2中配置全局拦截器的方法
- PASCAL VOC
- 微信小程序的界面介绍
- extjs 谷歌浏览器中 弹出窗口不显示左上角叉叉
- 避免 XML 属性?针对元数据的 XML 属性
- Bootstrap中菜单栏使用dropdown和dropdown-menu,菜单栏中文字链接失效
- BLE 协议栈之ibeacon广播包羞涩初探