一つの方法は、あなたがプリフォーム毎時間のカウント、可能性を経るですアクションは、と私は
で
o o o
x o x
o o o
x o x
o o o
を表すcount
の
f(grid,count)
for each square in grid
if square is a circle // i.e. 'o'
return min(g(grid with vertical stroke,count+1),
g(grid with horizontal stroke,count+1))
return count
の分を取りますJavaで
boolean arr [][] = {
{false,false,false},
{true,false,true},
{false,false,false},
{true,false,true},
{false,false,false}
};
私が持っている:
// return copy of arr
static boolean[][] copy(boolean arr[][]){
boolean[][] c = new boolean[arr.length][arr[0].length];
for(int i = 0; i < arr.length; i++)
c[i] = arr[i].clone();
return c;
}
// select consecutive squares horizontal stroke at (i,j)
static boolean[][] leftRight(boolean arr[][],int i, int j){
boolean[][] c = copy(arr);
// right
int k = j;
while (true){
if(k==arr[0].length || c[i][k]){
break;
}
c[i][k] = true;
k++;
}
// left
k = j;
while (true){
if(k==-1 || c[i][k]){
break;
}
c[i][k] = true;
k--;
}
c[i][j] = true;
return c;
}
// select consecutive squares vertical stroke at (i,j)
static boolean[][] upDown(boolean arr[][],int i, int j){
boolean[][] c = copy(arr);
// down
int k = i;
while (true){
if(k==arr.length || c[k][j]){
break;
}
c[k][j] = true;
k++;
}
// up
k = i;
while (true){
if(k==-1 || c[k][j]){
break;
}
c[k][j] = true;
k--;
}
c[i][j] = true;
return c;
}
static int g(boolean arr[][], int count){
// for each square in grid
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++) {
// if false then select square
// and try horizontal and vertical stroke
// take the min of the 2
if (!arr[i][j]) {
return Math.min(g(leftRight(arr,i,j),count+1),g(upDown(arr,i,j),count+1));
}
}
}
return count;
}
public static void main(String args[]){
/*
o o o
x o x
o o o
x o x
o o o
*/
boolean arr [][] = {
{false,false,false},
{true,false,true},
{false,false,false},
{true,false,true},
{false,false,false}
};
System.out.println(g(arr,0));
}
出力:
5
あなたの問題の説明は、あなたの例のいずれかと一致していないようです。あなたの事例を説明できますか? –
質問は、「純粋に水平と垂直のストロークの組み合わせのみを使用して、グリッド内のすべてのゼロを塗りつぶす最小のストローク数は何ですか?」と言っても分かります。コースの上にペイントすることなく。 –
ありがとうございます。私のポストを更新してください。それは少し良いです。 –