Temple of the bone(dfs)

题目描述:

The doggie found a bone in an ancient maze, which fascinated him a lot.

doggi发现一个古代迷宫里面的骨头,这很吸引他。

However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking.

然而,当他拿起,迷宫开始摇晃,dog可能会在下沉的地面跌落

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.

迷宫是个N*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).

一开始,门是关着的。  他会在T-th秒的时刻 打开,持续少于1s的很短的时间

Therefore the doggie had to arrive at the door on exactly the T-th second.

他需要精确到达,在T-th秒这个时刻

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.

一旦他进入一个block,他这个砖块的地面会下沉 消失,在下一秒。

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 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. 

输出:

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

样例输入:

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


样例输出:

NO
YES

 

 

题目大意:有一个 N*M 的迷宫,包括起点 S,终点 D,墙 X,和地面.

0秒时主人公从 S 出发,每秒能走到四个与其相邻的位置中的一个,且每个位置被行走之后都不能再次走入,问是否存在这样一条路径使主人公在 T 秒时刚好走到 D。

 

他这个dfs找到出口还不行,还要到出口时,必须是t时刻。

 

 

#include <iostream>
using namespace std;

int n,m;
char maze[11][11];
bool mark[11][11];
int time;
int startY,startX;
int endY,endX;

bool isOk = false;

bool isInMap(int y,int x) {
	if ( y >= 1 && y <= n && x >= 1 && x <= m) {
		return true;
	}
	return false;
}



void dfs(int y, int x,int t) {
//	cout << "目前来到了位置y and x:" << y << "   " << x << "  ,time : " << t << endl;

	int ny,nx;

	// 下
	if (!isOk) {
		ny = y + 1;
		nx = x;
//		cout  << y << " " << x <<"的下:" << ny << " " << nx << ", 情况: " << isInMap(ny,nx) << maze[ny][nx]<< mark[ny][nx] << endl;

		if (isInMap(ny,nx) && maze[ny][nx] == '.' && mark[ny][nx] == 0) {
			mark[ny][nx] = 1;
			dfs(ny,nx,t+1);
			mark[ny][nx] = 0;
		}
		if (isInMap(ny,nx) && maze[ny][nx] == 'D') {
			if (t + 1 == time) {
//				cout  << "ok!" << endl;
				isOk = true;
				return;
			} else {
//				cout << "time 不对!" << endl;
			}
		}

	}

	//上
	if (!isOk) {

		ny = y - 1;
		nx = x;
//		cout  << y << " " << x <<"的上:" << ny << " " << nx << ", 情况: " << isInMap(ny,nx) << maze[ny][nx]<< mark[ny][nx] << endl;

		if (isInMap(ny,nx) && maze[ny][nx] == '.' && mark[ny][nx] == 0) {
			mark[ny][nx] = 1;
			dfs(ny,nx,t+1);
			mark[ny][nx] = 0;
		}
		if (isInMap(ny,nx) && maze[ny][nx] == 'D') {
			if (t + 1 == time) {
//				cout  << "ok!" << endl;
				isOk = true;
				return;
			} else {
//				cout << "time 不对!" << endl;
			}
		}

	}

	//左
	if (!isOk) {

		ny = y;
		nx = x - 1;
//		cout  << y << " " << x <<"的左:" << ny << " " << nx << ", 情况: " << isInMap(ny,nx) << maze[ny][nx]<< mark[ny][nx] << endl;

		if (isInMap(ny,nx) && maze[ny][nx] == '.' && mark[ny][nx] == 0) {
			mark[ny][nx] = 1;
			dfs(ny,nx,t+1);
			mark[ny][nx] = 0;
		}
		if (isInMap(ny,nx) && maze[ny][nx] == 'D') {
			if (t + 1 == time) {
//				cout  << "ok!" << endl;
				isOk = true;
				return;
			} else {
//				cout << "time 不对!" << endl;
			}
		}

	}


	//右

	if (!isOk) {

		ny = y;
		nx = x + 1;
//		cout  << y << " " << x <<"的右:" << ny << " " << nx << ", 情况: " << isInMap(ny,nx) << maze[ny][nx]<< mark[ny][nx] << endl;

		if (isInMap(ny,nx) && maze[ny][nx] == '.' && mark[ny][nx] == 0) {
			mark[ny][nx] = 1;
			dfs(ny,nx,t+1);
			mark[ny][nx] = 0;
		}
		if (isInMap(ny,nx) && maze[ny][nx] == 'D') {
			if (t + 1 == time) {
//				cout  << "ok!" << endl;
				isOk = true;
				return;
			} else {
//				cout << "time 不对!" << endl;
			}
		}

	}


}




int main() {

	while(cin >> n >> m >> time && n != 0 && m != 0 && time != 0) {
		// cout << "n m t : " << n << m << t << endl;
		isOk = false;

		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= m; j++) {
				cin >> maze[i][j];
				mark[i][j] = 0;

				if(maze[i][j] == 'S') {
					startY = i;
					startX = j;
				}

				if(maze[i][j] == 'D') {
					endY = i;
					endX = j;
				}

			}
		}

		// cout << "***" << startY << "   " << startX << endl;
		// cout << "***" << endY << "   " << endX << endl;

		dfs(startY, startX,0);

		if (isOk) {
			cout << "YES" << endl;
		} else {
			cout << "NO" << endl;
		}

	}
}

 

#include <iostream>
using namespace std;


char maze[101][101];
bool mark[101][101];
int yMax,xMax;
int time;

bool isFound;

int dir[4][2] = {
	{1,0},
	{-1,0},
	{0,1},
	{0,-1}
};

bool isInMap(int y,int x) {
	if (y <= yMax && y >= 1 && x >= 1 && x <= xMax) {
		return 1;
	}
	return 0;
}


void dfs(int y,int x,int t) {
//	cout << "current:" << y << " "<< x << " "<< t << " " << maze[y][x] << endl;
	if (t > time) {
		return ;
	}

	if (maze[y][x] == 'D' && t == time) {
//		cout << "ok" << endl;
		isFound = true;
		return ;
	}
	if (isFound == true) {
		return ;
	} else {
		int ny ,nx;
		for (int i = 0; i <= 3; i++) {
			ny = y + dir[i][0];
			nx = x + dir[i][1];
			if (isInMap(ny,nx) == 1 && (maze[ny][nx] == 'D'  || maze[ny][nx] == '.') && mark[ny][nx] == 0) {
				mark[ny][nx] = 1;
				dfs(ny,nx,t+1);
				mark[ny][nx] = 0;
			}

		}

	}
}




int main() {

	while (cin >> yMax >> xMax >> time) {
		if (yMax == 0 && xMax == 0 && time == 0){
			return 0;
		}


		int startY;
		int startX;

		isFound = false;
		for (int i = 1; i <= yMax; i++) {
			for (int j = 1; j <= xMax; j++) {
				cin >> maze[i][j];
				mark[i][j] = 0;
				if (maze[i][j] == 'S') {
					startY = i;
					startX = j;
				}

			}
		}


		dfs(startY,startX,0);

		if (isFound == true) {
			cout << "YES" << endl;
		} else {
			cout << "NO" << endl;
		}

	}



}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值