0
点赞
收藏
分享

微信扫一扫

18年 字母阵列

非衣所思 2022-02-24 阅读 62
eclipse
public class t1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//输入字母
		String str="SLANQIAOZOEXCCGBMOAYWKHIBCCIPLJQSLANQIAORSFWFNYAXIFZVWALCOAIQNAL";
		//转换为一维字符数组
		char [] stringArr = str.toCharArray();
		//转化为二维8*8字符数组
		char[][]c=new char[8][8];
		int k=0;
		for (int i = 0; i <8; i++) {
			for (int j = 0; j <8; j++) {
				//赋值
				c[i][j]=stringArr[k];
				k++;
			}
		}
		for (char[] ds : c) {
			//输出测试
			System.out.println(ds);
		}
		//总共有八个移动方向
		int[][]move= {
				{-1,-1},
				{-1,0},
				{-1,1},
				{0,1},
				{1,1},
				{1,0},
				{1,-1},
				{0,-1},
		};
		int count=0;//记录符合条件的个数
		String lq="LANQIAO";
		for (int i = 0; i <8; i++) {
			for (int j = 0; j <8; j++) {
				if (c[i][j]=='L') {//从L开始移动方向
					for (int z = 0; z <8; z++) {
						int x=i;
						int y=j;
						String l="L";
						for (int k1 = 1; k1 <lq.length(); k1++) {
							x+=move[z][0];//移动方向
							y+=move[z][1];
							if (x<0||x>=8||y<0||y>=8||lq.indexOf(c[x][y])==-1) {
								break;
							}
							l=l+c[x][y];//拼接字符
						}
						if (l.equals(lq)) {//符合条件
							count++;
						}
					}
				}
			}
		}
		System.out.println(count);

	}

}

举报

相关推荐

0 条评论