贪心算法

everythingisyou 2020-06-16 12:21:35
(1)Lct准备研究一个名为“Link - Cut - Tree”的算法,他认为这个算法很美,所以他每天都说“lcttxdy”。有一天,他挑了一个n∗m棋盘,但这让他很奇怪的是棋盘上的每一个棋子都有一个小字母,他想知道有多少个大小写字母连在一起可以构成‘lcttxdy’。(注意:只有当一个字母在上、下、左、右、左上、左下、右上和右下时,我们称这两个字母是连在一起的) 输入 第一行包含一个整数t(1≤t≤100)——输入中的测试用例的数量。 每个测试用例的第一行包含两个整数n和m——棋盘格的长度和宽度(1≤n,m≤100) 接下来的n行每一行包含m个字母,它们都是英文小写字母 输出 对于每种情况,输出两行: 在第一行输出“YES”或“NO”——查找或不查找“lcttxdy” 在第二行输出“lcttxdy”的数目。如果没有找到打印-1 求代码
...全文
284 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
YangjulongTrue 2020-06-30
  • 打赏
  • 举报
回复
public static void main(String[] args) {	
F(10,10);
}
//n m 棋盘
public static void F(int n,int m) {
char[][] charset=new char[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
charset[i][j]= (char)(Math.random()*26+97);
System.out.print(charset[i][j]);
}
System.out.println();

}

select(new char[][] {new char[] {'l','c','t','t','x','d','y'}});


}
static LinkedList<String> string1=new LinkedList<String>();
//1 棋盘 x y a当前下标 cha 查找的字符
public static void Seach(char[][] qi,int x,int y,int a,char[] cha) {
if(a==cha.length-1) {
System.out.println("成功。。。。。。");
string1.forEach(e->System.out.println(e));
System.exit(0);
return ;
}
String ok=isok(qi,x,y,cha[a]);
if(ok!=null) {
String [] s=ok.split(",");
string1.add(ok);
Seach(qi,Integer.parseInt(s[0]),Integer.parseInt(s[1]),a+1,cha);
string1.removeLast();
}


}
public static void select(char[][] qi) {
for (int i = 0; i < qi.length; i++) {
for (int j = 0; j < qi[0].length; j++) {
Seach(qi,i,j,0,new char[] {'l','c','t','t','x','d','y'});
}
}
}
//查询 并返回 坐标
public static String isok(char[][] qi,int x,int y,char a) {

if(ishh(qi,x+1,y,a)) return (x+1)+","+y;
if(ishh(qi,x-1,y,a)) return (x-1)+","+y;
if(ishh(qi,x-1,y+1,a)) return (x-1)+","+(y+1);
if(ishh(qi,x-1,y-1,a)) return (x-1)+","+(y-1);
if(ishh(qi,x+1,y+1,a)) return (x+1)+","+(y+1);
if(ishh(qi,x+1,y-1,a)) return (x+1)+","+(y-1);
if(ishh(qi,x,y+1,a)) return x+","+(y+1);
if(ishh(qi,x,y-1,a)) return x+","+(y-1);



return null;
}
public static boolean ishh(char [][]qi,int x,int y,char a) {
try {
return qi[x][y]==a;
} catch (Exception e) {
return false;
}

}
select 改成上面随机生成的就行了 不过看脸 哎。。。。

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧