程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> FOSU菜鳥新篇章Tempter of the Bone

FOSU菜鳥新篇章Tempter of the Bone

編輯:C++入門知識

Tempter of the Bone

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


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
題目鏈接點擊打開鏈接

這道題的意思是小狗從S點出發走到D點,每次走一步而且只能是相鄰的,X代表不可走,.代表可走,
問在T步內可不可以到達終點D,這道題可以用深搜解決,不過深搜很容易超時,要加上一定的剪枝
才可以不說了,看代碼解釋
#include
#include
#include
#include
using namespace std;
char map[10][10];
int dir[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
int s1,s2,e1,e2,N,M,T,flag;
int dfs(int a,int b,int step1)
{
	if(step1==T&&a==e1&&b==e2)//判斷是否到達終點,是否步數為T,是就返回遞歸
		return 1;
	if(T-step1>map[i][j];//如果用scanf會發現超時,應該是輸入的問題
				if(map[i][j]=='S')//找到開始的坐標跟結束的坐標
				{
					s1=i;
					s2=j;
				}
				if(map[i][j]=='D')
				{
					e1=i;
					e2=j;
				}
			}
			
		}
		map[s1][s2] = 'X';
		if(dfs(s1,s2,0))
			printf("YES\n");
		else
			printf("NO\n");
		
	}
	return 0;
	
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved