杭电oj 1010

先看题目

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 94609    Accepted Submission(s): 25643


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.
 

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.
 

Output
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
 

Sample Input
  
  
4 4 5 S.X. ..X. ..XD .... 3 4 5 S.X. ..X. ...D 0 0 0
 

Sample Output
  
  
NO YES
 

说多了都是泪,我把这道题目的NO看成了FALSE,然后提交的时候都是wa,我就一直在找特殊的测试用例,但是找了一晚上没有找到,第二天随便看了一下,就发现错误了,哎,以后做事要仔细一点。

言归正传,这道题目的意思就是找迷宫内找到出口,找出口,大家都会想到用DFS,我也是用DFS深度搜索,需要注意的是,找到出口的时间也就是步数是一个规定值,不能多也不能少,而且走过的地方不可以返回。下面是我的代码:

#include <iostream>
using namespace std;
char maze[10][10];
int N,M,T;
bool visit[10][10];
bool flag;
bool canVisit(int x,int y)
{
	if(!visit[x][y])
		return true;
	return false;
}
void dfs(int number,int x,int y)
{
	if(number>T||flag)
	{
		return ;
	}
	
	else if(number==T)
	{
		if(maze[x][y]=='D')
			flag=true;
		return;
	}

	else if(maze[x][y]=='.'||maze[x][y]=='S')
	{
		visit[x][y]=true;
		//down 
		if(x<N-1&&canVisit(x+1,y))
		{
			dfs(number+1,x+1,y);
		}
		//up 
		if(x>0&&canVisit(x-1,y))
		{
			
			dfs(number+1,x-1,y);
		}
		if(y<M-1&&canVisit(x,y+1))
		{
			dfs(number+1,x,y+1);
		}
		//left
		if(y>0&&canVisit(x,y-1))
		{
			dfs(number+1,x,y-1); 
		} 
		visit[x][y]=false;
	}
	

}
int main()
{
	int startX,startY;
	int i,j;
	while(cin>>N>>M>>T&&N&&M&&T)
	{
		for(i=0;i<N;i++)
		{
			for(j=0;j<M;j++)
			{
				cin>>maze[i][j];
				visit[i][j]=false;
				if(maze[i][j]=='S')
				{
					startX=i;
					startY=j;
				}
			}
		}
		flag=false;
		dfs(0,startX,startY);
		if(flag)
		{
			cout<<"YES"<<endl;
		}
		else
		{
			cout<<"NO"<<endl;
		}
	}
}

ac了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用\[1\],这段代码是一个C语言程序,用于计算给定范围内的奇数和偶数的平方和和立方和。程序首先读取两个整数m和n,然后使用循环计算m到n之间的每个数的平方和和立方和。如果一个数是偶数,则将其平方加到变量x中;如果一个数是奇数,则将其立方加到变量y中。最后,程序输出变量x和y的值。 根据引用\[2\],这段代码的作者在解决这个问题时遇到了一些困难,包括横纵坐标的赋值和与行数列数的转换。作者最初使用结构体来解决问题,但在上传到OJ时遇到了语法错误。然后作者改用数组来解决问题,但仍然遇到了超时的问题。最后,作者在百度上寻找了解决方案。 根据引用\[3\],杭电OJ是一个实时提交系统,用于C++编程考试。该系统具有即时获得成绩排名的特点。当学生提交答案后,系统会判断答案是否正确,如果正确则标记为"AC",如果错误则计算错误提交次数和所花时间。每次错误提交都会增加一定的单位时间分。因此,即使一个学生在题数上领先,但在耗时上可能处于劣势。 综上所述,杭电OJ1040是一个C语言编程题,要求计算给定范围内的奇数和偶数的平方和和立方和。这段代码的作者在解决问题时遇到了一些困难,最终使用数组解决了问题。杭电OJ是一个实时提交系统,根据答案的正确性、错误提交次数和所花时间来计算成绩排名。 #### 引用[.reference_title] - *1* [杭电OJ 2007 C语言版 已通过](https://blog.csdn.net/m0_56912916/article/details/119041984)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [杭电OJ1010-C语言-浅析](https://blog.csdn.net/ruiruiruiruirain/article/details/79856272)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [杭电OJ2093 C语言](https://blog.csdn.net/qq_46582929/article/details/117429872)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值