0
#include <iostream>
#include <fstream>

using namespace std;

char a[11][11];

bool free(int x, int y)
{
  if (x < 1 && x > 10)
  {
    return false;
  }
  if (y < 1 && y > 10)
  {
    return false;
  }
  for (int i = -1; i < 1; i++)
  {
    for (int j = -1; j < 1; j++)
    {
      if (a[x + i][y + j] != '.')
      {
	return false;
      }
      cout << endl;
    }
  }
  return true;
}

int main()
{
  int max = 20;
  int unused = 0;
  ifstream sisf("laevtest.01.sis");
  ofstream valf("laev.val");
  int k = 0;
  for (int i = 1; i < 11; i++)
  {
    for (int j = 1; j < 11; j++)
    {
      sisf >> a[i][j];
      if (a[i][j] == '#') 
      {
	k++;
      }
    }
  }
  unused = max - k;
  // Fill up
  for (int i = 0; i < 12; i++)
  {
    a[i][0]  = '.'; a[i][11] = '.';
    a[0][i]  = '.'; a[11][i] = '.';
  }
  bool ok = true;
  for (int i = 1; i < 11; i++) 
  {
    for (int j = 1; j < 11; j++)
    {
      for (int u = 0; u < unused - 1; u++)
      {
	// Horisontaalselt paigutamine (laius)
	ok = free(i, j + u);
	if (ok)
	{
	  a[i][i + u] = '#';
	}
      }
      for (int u = 0; u < unused - 1; u++)
      {
	// Vertikaalselt paigutamine
	ok = free(i + u, j);
	if (ok)
	{
	  a[i + u][j] = '#';
	}
      }
    }
  }
  for (int i = 1; i < 11; i++)
  {
    for (int j = 1; j < 11; j++)
    {
      valf << a[i][j];
    }
    valf << endl;
  }
  sisf.close();
  valf.close();
  return 0;
}

Output:

#..#..#.##
#..#......
...#.#.##.
.....#....
.#...#....
.....#....
.#........
.#....#...
.#........
..........

True output is:

#..#..#.##
#..#......
...#.#.##.
.....#....
.#.#.#....
.....#....
.#........
.#....#...
.#........
..........
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Lerner
0

It's not clear to me what all of your loops are trying to do and since I'm not fluent in your native language I don't understand your comments. I can say that your use of the variable called unused is unusual and why you want to overwrite the contents of the array called a after it has been read in from the file is something I can't explain. Your use of sisf, valf, and K seem straightforward.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.