View Code of Problem 3684

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
char arr[1005][1005];
bool isvisited[1005][1005];
int t;
int direction[4][2] = { {0,-1},{0,1},{1,0},{-1,0} };
bool DFS(int x,int y,int n,int m,int count) {
	isvisited[x][y] = true;
	if (arr[x][y] == 'F'&&count <= t) {
		return true;
	}
	for (int i = 0; i < 4; i++) {
		if (x + direction[i][0] >= 0 && x + direction[i][0] < n&&y + direction[i][1] >= 0 && y + direction[i][1] < m
			&&isvisited[x + direction[i][0]][y + direction[i][1]] == false
			&&arr[x+direction[i][0]][y+direction[i][1]]!='#') {
			if (DFS(x + direction[i][0], y + direction[i][1], n, m, count + 1) == true) {
				return true;
			}
		}
	}
	isvisited[x][y] = false;
	return false;
}
int main()
{
	int n, m;
	while (cin >> n >> m >> t) {
		memset(arr, ' ', sizeof(arr));
		memset(isvisited, false, sizeof(isvisited));
		int x, y;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				char c;
				cin >> c;
				arr[i][j] = c;
				if (c == 'S') {
					x = i;
					y = j;
				}
			}
		}
		if (DFS(x, y, n, m, 0)) {
			cout << "Happy" << endl;
		}
		else {
			cout << "Cry" << endl;
		}
	}
	
	return 0;
}

Double click to view unformatted code.


Back to problem 3684