matryca 2d i zmiana wartosci sasiadow

0

Witam wszystkich.

Pracuje z 2 wymiarowa tablica. Probuje zmienic wartosci (z "1" na "4") wszystkich sasiadow otaczajacych moj punkt startowy. Niestety, moj obecny kod zmienia rowniez wartosc punktu startowego. Jak tego uniknac? Oto kod:

#include <iostream>
#include <cstdio>
#include <fstream>
#include <queue>
#include "node.h"
#include <math.h>
#include <string>

using namespace std;

void loadMap();
void displayMap();

//Map size
const int Nsize = 5;			//number of rows
const int Msize = 7;			//number of columns

//Start location
int sX=2;
int sY=1;


//Map
static int map[Nsize][Msize];	//work map

//Directions
const int dir=8;
static int dirX[dir]={1, 1, 0, -1, -1, -1, 0, 1};
static int dirY[dir]={0, 1, 1, 1, 0, -1, -1, -1};
static int dir_map[Nsize][Msize];  //map of directions


int main()
{
	loadMap();

	for (int i=0; i<dir; i++)
	{
		for(int j=0; j<dir; j++)
		{

	
	map[sX+dirY[i]][sY+dirX[j]] = 4;
		}
	}
	
	displayMap();


getchar();
return 0;

}

void loadMap ()
{
//load the map from .txt file
ifstream file;
file.open("testmap.txt");

if(file.good()==true)
{
    for(int i=0;i<Nsize;i++)
    {
       for(int j=0;j<Msize;j++)
       {
          file>>map[i][j];
       }
    }
    file.close();
}
else
{
    cout<<"Cannot open the file";
}
}

void displayMap()
{
	//Display the map
for(int i=0;i<Nsize;i++)
{
    for(int j=0;j<Msize;j++)
    {
        cout<<" "<<map[i][j];
    }
        cout<<endl;
    }
}
0
if ((i != sX) && (j != sY)) // lub `if ((sX+dirY[i] != sX) ...`, nie jestem pewien co u Ciebie jest czym :P
 map[sX+dirY[i]][sY+dirX[j]] = 4;
0

Dzieki za szybka odpowiedz. Niestety, gdy pisze:

for (int i=0; i<dir; i++)
	{
		for(int j=0; j<dir; j++)
		{
			if ((i != sX) && (j != sY))
			map[sX+dirY[i]][sY+dirX[j]] = 4;
		}
	}

program dalej zmienia wartosc "komorki" centralnej, a tego probuje uniknac...

1

To może:
if ((sX+dirY[i] != sX) && (sY+dirX[j] != sY))

0
 for (int i=0; i<dir; i++)
	{
		for(int j=0; j<dir; j++)
		{
			if ((sX+dirY[i] != sX) && (sY+dirX[j] != sY)) 
			map[sX+dirY[i]][sY+dirX[j]] = 4;
		}
	}
	

To rozwiazanie daje ciekawy rezultat, ale dalej nie o to chodzi :). Teraz program, w odroznieniu od poprzednich wersji, nie zmienia wartosci "komorki" centralnej, ale zmienia wartosci sasiadow znajdujacych sie po ukosie, lecz nie tych w pionie i poziomie... Jakies sugestie?

Oto moj pelen kod:

 #include <iostream>
#include <cstdio>
#include <fstream>
#include <queue>
#include "node.h"
#include <math.h>
#include <string>

using namespace std;

void loadMap();
void displayMap();

//Map size
const int Nsize = 5;			//number of rows
const int Msize = 7;			//number of columns

//Start and finish locations
int sX=2;
int sY=1;

int fX=2;
int fY=5;

//Node maps
static int closedNodes[Nsize][Msize]; //the set of nodes alreadirY evaluated
static int openNodes[Nsize][Msize]; // the set of nodes to be evaluated; initialy only containing start node
static int map[Nsize][Msize];	//work map

//Directions
const int dir=8;
static int dirX[dir]={1, 1, 0, -1, -1, -1, 0, 1};
static int dirY[dir]={0, 1, 1, 1, 0, -1, -1, -1};
static int dir_map[Nsize][Msize]; //map of directions (contains parents-children connection)




int main()
{
	loadMap();

	for (int i=0; i<dir; i++)
	{
		for(int j=0; j<dir; j++)
		{
			if ((sX+dirY[i] != sX) && (sY+dirX[j] != sY)) 
			map[sX+dirY[i]][sY+dirX[j]] = 4;
		}
	}
	
	displayMap();


getchar();
return 0;
//***********************************************************************
}

void loadMap ()
{
	//load the map from .txt file
ifstream file;
file.open("testmap.txt");

if(file.good()==true)
{
    for(int i=0;i<Nsize;i++)
    {
       for(int j=0;j<Msize;j++)
       {
          file>>map[i][j];
       }
    }
    file.close();
}
else
{
    cout<<"Cannot open the file";
}
}

void displayMap()
{
	//Display the map
for(int i=0;i<Nsize;i++)
{
    for(int j=0;j<Msize;j++)
    {
        cout<<" "<<map[i][j];
    }
        cout<<endl;
    }
}
0

Rozwiazane! Potrzebna byla tylko jedna petla for (int i=0; i<dir; i++)
Dzieki za pomoc!

1 użytkowników online, w tym zalogowanych: 0, gości: 1