求最大蛇的长度 本题只是笔记 代码是错的
来源:互联网 发布:ipad 下载不了软件 编辑:程序博客网 时间:2024/04/30 14:43
package work;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class anaconda {
static class Node {
int x;
int y;
int step;
}
static int N, M, maxlen;
static int[][] data;
static Node[] queue;
static int[] Dx = { -1, 0, 1, 0 };
static int[] Dy = { 0, 1, 0, -1 };
static int counts;
static int[] res;
public static void main(String[] args) throws FileNotFoundException {
/* Scanner sc=new Scanner(System.in); */
Scanner sc = new Scanner(new File("src/anaconda"));
while (true) {
N = sc.nextInt();
M = sc.nextInt();
data = new int[N][M];
res = new int[N * M];
queue = new Node[N * M];
if (N == 0 && M == 0) {
break;
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
data[i][j] = sc.nextInt();
}
}
counts = 0;
for (int n = 0; n < N; n++) {
for (int m = 0; m < M; m++) {
if (data[m][n] == 1) {
counts++;
res[counts] = bfs(m, n, 1);
}
}
}
maxlen = 0;
for (int i = 0; i < N*M; i++) {
if(maxlen<res[i]){maxlen=res[i];}
}
System.out.println(maxlen);
}
}
private static int bfs(int n, int m, int step) {
int head = 0;
int tail = 0;
int count = 0;
Node n1 = new Node();
n1.x = n;
n1.y = m;
n1.step = step;
queue[tail++] = n1;
while (head < tail) {
for (int i = 0; i < 4; i++) {
int dx = queue[head].x + Dx[i];
int dy = queue[head].x + Dy[i];
if (dx < 0 || dy >= N || dy < 0 || dy >= M) {
break;
}
if (dx >= 0 && dy < N && dy >= 0 && dy < M && data[dx][dy] == 1) {
data[dx][dy] = 2;
Node n2 = new Node();
n2.x = dx;
n2.y = dy;
n2.step = queue[head].step + 1;
queue[tail++] = n2;
count++;
}
}
head++;
}
return count;
}
}
、、input
5 5
0 0 0 1 0
0 0 0 1 1
1 1 1 0 0
0 0 1 0 0
0 0 0 0 0
10 10
0 0 0 0 0 1 0 0 0 0
1 1 1 1 0 1 1 1 1 0
1 0 0 1 0 0 0 0 1 1
1 1 0 1 0 0 0 0 0 0
0 1 0 1 0 0 0 0 0 0
1 1 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 1 1 1 1 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 1 0 0 0
10 10
0 0 0 0 0 1 0 0 0 0
1 0 0 0 0 1 1 0 0 0
1 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0
、、output
2
2
1
- 求最大蛇的长度 本题只是笔记 代码是错的
- 大蛇的蛋
- 一条吞掉自己的大蛇
- LA2678 - Subsequence(求序列中长度最短的和>=S的子序列长度)
- 求数组的长度?
- 求字符串的长度
- 求折线的长度
- 求数组的长度
- 求字符串的长度
- 求N!的长度
- 求字符串的长度
- 求数组的长度
- 我们只是代码的搬运工
- 加班最勤奋的往往是失败者,他们只是在拼命杀死自己|真实故事
- OJ_1176,神奇的开学发作业本题
- 回归只是最开始的选择
- 求字符的长度,半角是1bytle,全角是2bytle
- 语言只是工具,最重要的是系统架构,没有了架构,就会变成代码机器人。
- CMake手册详解 (九)
- 32位64位操作系统基本数据类型字节大小
- js字符串常用方法详细整理
- 微信小程序详解——页面之间的跳转方式【路由】和参数传递
- 开此博客感言
- 求最大蛇的长度 本题只是笔记 代码是错的
- 清理iOS工程里无用的图片,可瘦身ipa
- CMake手册详解 (十)
- 顺序容器的简单初始化
- Mongdb常用资料地址
- 2016年终总结
- BZOJ 1002: [FJOI2007]轮状病毒
- 创建Image图像的几种方法
- CMake手册详解 (十一)