P1101 Word square matrix

Title link: P1101
this question (⊙o⊙) ..., Konjac said that it will not be written by search, it is not written, so the direct violence
is known from the question, we have 8 directions, and all the 8 directions that meet the conditions need to be output. Problem Just because he may be able to use the repeated position, if you use search, you have to find a way to solve this problem, I will not
method:
we only need to find the first character, that is, y after finding this character, we traverse his 8 See if the sequence of characters in yizhong is satisfied, if you are satisfied, just record it.
Code

import java.util.*;
public class Main {
	static int n;
	static char a[][];
	static boolean b[][];
	static char key[] = new String("yizhong").toCharArray();
	static int xx[] = new int[]{-1,-1,-1,0,0,1,1,1};
	static int yy[] = new int[]{-1,0,1,-1,1,-1,0,1};
	static void check(int x,int y){
		for(int i = 0;i < 8;i++){
			int j = 0;
			int p = x;
			int q = y;
			for(j = 1;j < 7;j++){
				if(p+xx[i]>=0 && p+xx[i]<n && q+yy[i]>=0 && q+yy[i]<n && (a[p+xx[i]][q+yy[i]] == key[j])){
					p = p+xx[i];
					q = q+yy[i];
				}
				else
					break;
			}
			if(j == 7){
				for(int k = 0;k < 7;k++){
					b[p][q] = true;
					p = p-xx[i];
					q = q-yy[i];
				}
			}
		}
	}
	public static void main(String args[]){
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		a = new char[n][n];
		b = new boolean[n][n];
		for(int i = 0;i < n;i++)
			for(int j = 0;j < n;j++)
				b[i][j] = false;
		for(int i = 0;i < n;i++)
			a[i] = sc.next().toCharArray();
		for(int i = 0;i < n;i++)
			for(int j = 0;j < n;j++)
				if(a[i][j] == 'y')
					check(i,j);
		for(int i = 0;i < n;i++){
			for(int j = 0;j < n;j++){
				if(b[i][j])
					System.out.print(a[i][j]);
				else
					System.out.print("*");
			}
			System.out.println();
		}
	}
}

Published 32 original articles · praised 5 · visits 862

Guess you like

Origin blog.csdn.net/shizhuba/article/details/104289288