这题很基础 A的很辛苦

今天5月27日 在杭电oj上A了第一题 留念一下

都说是水题 我才做的 总算图论入门

题目描述:

          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.

一开始是用bfs 自我感觉太好, 以为和AOJ上的hero in maze 一样, 结果连交3次都是WA而告终。于是 再看题意,才发现。。。------- 当且仅当t=T 时, 可怜的小狗才能逃出 这样一来, 广搜就行不通了,因为广搜只能搜到最短路径。。。。这就体现了局限性。。而现在需要找到需要的时间,就要列出很多路径。。。果断深搜啊。。。

在解深搜的时候反复出错调试。。。终于Accepted。。。

广度优先代码:
int bfs(int x,int y)
{
	queue<point>q;
	memset(visited,false,49);
        //初始化
	point p;
	p.x=x;
	p.y=y;
	p.step=0;
	q.push(p);
	while(!q.empty())
	{
		point tempm;
		tempm=q.front();
		q.pop();
		//四个方向搜索
		int i;
		for(i=0;i<4;i++)
		{
		   point temp;
                     temp.x=tempm.x+dx[i];//广搜易错点
		   temp.y=tempm.y+dy[i];
		   if(inmap(temp.x,temp.y)&&!visited[temp.x][temp.y]&&map[temp.x][temp.y]!='X')
		   {
                             temp.step=tempm.step+1;
			   if(map[temp.x][temp.y]=='D')
			   {
				   return temp.step;
			   }  
			   q.push(temp);
			   visited[temp.x][temp.y]=true;
               
		   }
		}

		
	}
	return -1;
}
深度优先代码:
void dfs(int x,int y)
{
// cout<<x<<" "<<y<<endl;
 if(flag)return;
 if(time>t)
 {  
  return;
 }
 if(map[x][y]=='D')
 {
 // cout<<time<<endl;
  if(time==t)
  {
   flag=1;
   cout<<"YES"<<endl;
  }
  return;
 }
 int i;
 
 for(i=0;i<4;i++)
 {
  int x1,y1;
  x1=x+dx[i]; //深搜易错点
  y1=y+dy[i];
  //
  if(!visited[x1][y1]&&map[x1][y1]!='X'&&inmap(x1,y1))
  {
   visited[x1][y1]=true;
   time++;
   dfs(x1,y1);
   if(flag)return;
   visited[x1][y1]=false;
   time--;
  }
 }
 
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值