求最大蛇的长度 本题只是笔记 代码是错的
2017-01-17 16:44
295 查看
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
[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]
== 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
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
[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]
== 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
相关文章推荐
- 控制图片长度比例,及图片最大高度,最大宽度 JS代码
- EditText在代码中设置最大输入长度并设置为密码输入框
- JTextField设置最大长度的小代码
- android-代码设置editText最大长度
- edittext设置输入类型、最大长度 在代码中
- EditText Java代码设置输入框最大输入长度等基础知识,自己记录一下
- 【代码笔记】判断中英文混合的字符长度的两种方法
- linux基础只是之awk命令使用,统计最大长度
- Java学习笔记30. 数组的使用(长度、遍历、最大最小值,查找)
- asp.net MaxLengthValidator 最大长度验证控件代码
- css知识笔记(四)——代码简写、颜色值、长度值
- 【代码笔记】iOS-判断中英文混合的字符长度的两种方法
- EditText在代码中设置最大输入长度并设置为密码输入框
- 笔记:高精代码关键语句总结
- 查找二叉排序树中最大的键值(c代码)
- 最大限制地提高代码的可重用性,克服传统面向对象编程方法在可重用性方面的不足
- 行的最大长度限制:8060
- [笔记]3.软件代码中的BUG问题的一些记录
- ThunderBird, NewsBlog部分代码阅读笔记