Java:替换矩阵中的连续数字
我需要一种方法,在矩阵中找到一系列连续的数字,然后用1替换它们,用0替换所有其他数字,就像蛇一样移动。让我举一个例子,希望它更容易理解:Java:替换矩阵中的连续数字,java,matrix,counter,Java,Matrix,Counter,我需要一种方法,在矩阵中找到一系列连续的数字,然后用1替换它们,用0替换所有其他数字,就像蛇一样移动。让我举一个例子,希望它更容易理解: 9 2 9 9 6 8 9 9 4 该矩阵应转化为: 1 0 0 1 0 0 1 1 0 现在我得到的是: public int[][] replace(int[][] numbers, int n) { int[][] temp = numbers; int cont = 0; int cont2 = 0; for (in
9 2 9
9 6 8
9 9 4
该矩阵应转化为:
1 0 0
1 0 0
1 1 0
现在我得到的是:
public int[][] replace(int[][] numbers, int n) {
int[][] temp = numbers;
int cont = 0;
int cont2 = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i < n - 1 && temp[cont][cont2] == temp[cont + 1][cont2]) {
numbers[cont][cont2] = 1;
numbers[cont + 1][cont2] = 1;
cont++;
}
if (j < n - 1 && temp[cont][cont2] == temp[cont][cont2 + 1]) {
numbers[0][1] = 1;
numbers[0][0] = 1;
cont2++;
}
}
cont2 = 0;
}
return numbers;
}
public int[][]替换(int[][]数字,int n){
int[][]温度=数字;
int cont=0;
int cont2=0;
对于(int i=0;i
它几乎不起作用,只替换前2个值,然后出于某种原因停止。Im还缺少检查当前号码左侧号码的验证;还有让所有其他数字都变成0的部分,这不应该太难
有人知道我需要修改什么才能使这项工作正常吗?问题的说明已经包含了一个问题:当有两行数字时该怎么办?使用DFS(或者BFS)或任何其他路径查找算法,可以非常简单地找到解决方案本身。但是,这个主题的介绍非常好,所以我不会在这里发布任何代码。很难说,因为您的意图令人困惑,但是在
for
循环中,您有两个连续的if
语句。由于第一条语句可以更改第二条语句的条件表达式中使用的变量的值,我想知道您是否应该在第二条语句中使用else
而不是if
?首先,我发现了一些问题或误解了算法中的一些要点:
public int[][] replace(int[][] numbers, int n) {
int[][] temp = numbers; // /!\ temp points to the same array as numbers
int cont = 0; // why not using i
int cont2 = 0; // why not using j
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i < n - 1 && temp[cont][cont2] == temp[cont + 1][cont2]) {
numbers[cont][cont2] = 1; // /!\ temp=numbers is also modified
numbers[cont + 1][cont2] = 1;
cont++; // /!\ ~= i*j => index out of bound
}
if (j < n - 1 && temp[cont][cont2] == temp[cont][cont2 + 1]) {
numbers[0][1] = 1; // why fixed indexes ?
numbers[0][0] = 1;
cont2++;
}
}
cont2 = 0; // cont2 seems equivalent to j
}
return numbers;
}
public int[][]替换(int[][]数字,int n){
int[][]temp=numbers;///!\temp指向与数字相同的数组
int cont=0;//为什么不使用i
int cont2=0;//为什么不使用j
对于(int i=0;i索引超出范围
}
如果(j
你想要这样的东西吗(我还没有测试过):
public int[][]替换(int[][]数字,int n){
int[][]结果=新的int[n][n];
int ref=数字[0][0];
对于(int i=0;i0)isAdj=isAdj | |数[i-1][j]==ref;
如果(i0)isAdj=isAdj | |数[i][j-1]==ref;
如果(j你当前的总体想法是(据我所知,不管你的代码中有什么错误),它在一条路径中寻找类似的数字,而这条路径只向下和向右。这个想法不会在所有情况下都起作用。如果你遇到一块先向下然后向上的数字,那么它不会改变所有数字。例如,给定
2 1 2
2 1 2
2 2 2
您的算法将从左上角开始,然后向下,然后向右,但不会返回以完成U形
其次,行int[][]temp=numbers;
意味着对temp
所做的任何更改都会对numbers
进行更改,反之亦然。这就是所谓的浅拷贝,因为只复制引用,而不复制引用的数据。您可能需要深度拷贝
简单的答案是使用泛洪填充算法。泛洪填充基本上通过尽可能多地搜索整个棋盘来填充任意区域,同时遵循定义填充区域的规则。它可以成功探索的任何地方都会丢弃一个“面包屑”(特殊值)在那个位置,你可以回去把它改成任何需要的样子
下面的泛洪填充算法使用深度优先搜索来探索要填充的区域。如果要进行广度优先搜索,只需切换出优先级队列的堆栈
import java.awt.Point; //just an integer based point object for the Stack
import java.util.Stack; //get the Stack
public int[][] replace(int[][] numbers, int n) {
int value = numbers[0][0]; //value we are looking for
int[] rowChange = {-1, 0, 1, 0}; //for easy direction checking
int[] colChange = {0, 1, 0, -1};
// for the breadcrumbs; initialized to 0, so make a breadcrumb equal to 1
// then the resulting breadcrumb matrix will be what you want
int[][] breadcrumbs = new int[numbers.length][numbers[0].length];
Stack<Point> toExplore = new Stack<Point>();
toExplore.push(new Point(0, 0)); //explore the first point
// we are using Point.y as the row (first index),
// and Point.x as the column (second index)
//while there is a location to explore
while (toExplore.size() > 0) {
Point center = toExplore.pop();
// drop a breadcrumb
breadcrumbs[center.y][center.x] = 1;
// explore in the 4 cardinal directions
for (int i = 0; i < 4; i++) {
int row = center.y + rowChange[i];
int col = center.x + colChange[i];
// make sure we haven't already been here (bounds check first)
if ((row >= 0) && (row < numbers.length) && // bounds check
(col >= 0) && (col < numbers[0].length) &&
(breadcrumbs[row][col] == 0) && // make sure we haven't been there already
(numbers[row][col] == value)) // make sure it has the right value there
{
toExplore.push(new Point(col, row));
}
}
}
// filled with 1's where we explored and 0's where we didn't
return breadcrumbs;
}
import java.awt.Point;//只是堆栈中基于整数的点对象
import java.util.Stack;//获取堆栈
公共整数[][]替换(整数[][]数字,整数n){
int value=numbers[0][0];//我们正在寻找的值
int[]rowChange={-1,0,1,0};//用于方便的方向检查
int[]colChange={0,1,0,-1};
//对于面包屑;初始化为0,因此使面包屑等于1
//然后生成的面包屑矩阵就是您想要的
int[][]面包屑=新的int[numbers.length][numbers[0.length];
Stack-toExplore=新堆栈();
toExplore.push(新点(0,0));//探索第一个点
//我们使用Point.y作为行(第一个索引),
//和点.x作为列(第二个索引)
//虽然有一个位置可以探索
while(toExplore.size()>0){
Point center=toExplore.pop();
//放下面包屑
面包屑[center.y][center.x]=1;
//从四个基本方向进行探索
对于(int i=0;i<4;i++){
int row=center.y+rowChange[i];
int col=center.x+colChange[i];
//确保我们还没有到过这里(先检查边界)
如果((行>=0)&&&(行import java.awt.Point; //just an integer based point object for the Stack
import java.util.Stack; //get the Stack
public int[][] replace(int[][] numbers, int n) {
int value = numbers[0][0]; //value we are looking for
int[] rowChange = {-1, 0, 1, 0}; //for easy direction checking
int[] colChange = {0, 1, 0, -1};
// for the breadcrumbs; initialized to 0, so make a breadcrumb equal to 1
// then the resulting breadcrumb matrix will be what you want
int[][] breadcrumbs = new int[numbers.length][numbers[0].length];
Stack<Point> toExplore = new Stack<Point>();
toExplore.push(new Point(0, 0)); //explore the first point
// we are using Point.y as the row (first index),
// and Point.x as the column (second index)
//while there is a location to explore
while (toExplore.size() > 0) {
Point center = toExplore.pop();
// drop a breadcrumb
breadcrumbs[center.y][center.x] = 1;
// explore in the 4 cardinal directions
for (int i = 0; i < 4; i++) {
int row = center.y + rowChange[i];
int col = center.x + colChange[i];
// make sure we haven't already been here (bounds check first)
if ((row >= 0) && (row < numbers.length) && // bounds check
(col >= 0) && (col < numbers[0].length) &&
(breadcrumbs[row][col] == 0) && // make sure we haven't been there already
(numbers[row][col] == value)) // make sure it has the right value there
{
toExplore.push(new Point(col, row));
}
}
}
// filled with 1's where we explored and 0's where we didn't
return breadcrumbs;
}