P1101 单词方阵



import java.util.Scanner;

public class Main {
	static char[][] word;
	static boolean[][] judge;
	static boolean flag;
	static int letterloc;
	static char[] goal = "yizhong".toCharArray();
	static int n;

	static void dfs(int x, int y, int move) {
		if (letterloc == 6) {
			flag = true;
			judge[x][y] = true;
			return;
		}
		letterloc++;
		if (move == 1 && x + 1 < n && word[x + 1][y] == goal[letterloc])
			dfs(x + 1, y, move);
		if (move == 2 && x - 1 > 0 && word[x - 1][y] == goal[letterloc])
			dfs(x - 1, y, move);
		if (move == 3 && y + 1 < n && word[x][y + 1] == goal[letterloc])
			dfs(x, y + 1, move);
		if (move == 4 && y - 1 > 0 && word[x][y - 1] == goal[letterloc])
			dfs(x, y - 1, move);
		if (move == 5 && x + 1 < n && y + 1 < n
				&& word[x + 1][y + 1] == goal[letterloc])
			dfs(x + 1, y + 1, move);
		if (move == 6 && x + 1 < n && y - 1 > 0
				&& word[x + 1][y - 1] == goal[letterloc])
			dfs(x + 1, y - 1, move);
		if (move == 7 && x - 1 > 0 && y + 1 < n
				&& word[x - 1][y] == goal[letterloc])
			dfs(x - 1, y + 1, move);
		if (move == 8 && x - 1 > 0 && y - 1 > 0
				&& word[x - 1][y] == goal[letterloc])
			dfs(x - 1, y - 1, move);
		if (flag)
			judge[x][y] = true;
		letterloc--;
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		word = new char[n + 1][n + 1];
		judge = new boolean[n + 1][n + 1];
		for (int i = 0; i < n; i++) {
			word[i] = sc.next().toCharArray();
		}

		for (int x = 0; x < n; x++) {
			for (int y = 0; y < n; y++) {
				if (word[x][y] == 'y') {
					for (int move = 1; move <= 8; move++) {
						letterloc = 0;
						flag = false;
						dfs(x, y, move);
					}
				}
			}
		}

		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (judge[i][j])
					System.out.print(word[i][j]);
				else {
					System.out.print('*');
				}
			}
			System.out.println();
		}
	}
}

猜你喜欢

转载自blog.csdn.net/sobermineded/article/details/79705909