I've checked all the possible ways it is going out of the bound but it is still occurring.
anyone please explain.
class Solution {
public:
vector<vector<int>> dfs(vector<vector<int>>& image, int sr, int sc, int color,int rColor){
if(sc<0 || sr<0 || sr>=image.size()
|| sc>=image[0].size() || rColor!=image[sr][sc])
return image;
image[sr][sc]=color;
int x[4]={0,-1,0,1};
int y[4]={-1,0,1,0};
for(int i=0; i<4; i++){
int xt=sr+x[i];
int yt=sc+y[i];
if(xt>=0 || yt>=0 || xt<image.size() || yt<image[0].size() || rColor==image[xt][yt]){
dfs(image,xt,yt,color,rColor);
}
}
return image;
}
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) {
int x=image[sr][sc];
dfs(image,sr,sc,color,x);
return image;
}
};
I've tried debugging with cout the output always remains inside the index/bound but still don't know where the error is.
Related
The title pretty much says it all. I'm required to write a program for a Software course I'm taking that puts two basic algorithms against each other in a game of checkers. For this, I have a Square class that stores whether the square is occupied (bool), which player by (enum Pl, either NA, P1, or P2), and the label of that square (Index, int). I then created a Board class, which is effectively a 2D array of Squares that will have dimensions specified by the user. However, in the implementation file of my Board class, I'm having an error using the board[][] array in one of my functions. I get the error "board was not declared in this scope", which shouldn't be too tricky to figure out, except that the exact same syntax (at least from what I can see, and I've been working on fixing this for a while now) that's causing the error works perfectly in the constructor for the Board class in the same file. Below is the code for my Board.h file, and the relevant parts of my Board.cpp file:
Board.h:
#ifndef BOARD_H
#define BOARD_H
#include "Square.h"
#include <string>
class Board
{
public:
Board(int);
int getBsize();
int getWinner();
std::string getCmove();
Square getSquare(int, int);
void setWinner(int);
void setCmove(std::string);
bool canMoveL(int, int);
bool canMoveR(int, int);
bool canTakeL(int, int);
bool canTakeR(int, int);
void P1Move(int, int);
void P2Move(int, int);
void printCmove(std::string);
void printWin(std::string);
private:
Square board[12][12];
int bSize;
int winner;
std::string cMove;
};
#endif // BOARD_H
Board.cpp constructor as well as top of the file (in case the error is with the libraries and files I've included in Board.cpp):
#include "Square.h"
#include "Board.h"
#include <string>
Board::Board(int bSize)
{
Board::bSize = bSize;
Board::winner = 0;
Board::cMove = "";
Board::board[12][12];
int x = bSize-1;
int index = 1;
for(int j = 0; j < bSize; j++)
{
for(int i = 0; i < bSize; i++)
{
if(((i%2!=0)&&(j%2==0))||((i%2==0)&&(j%2!=0)))
{
if((j==x/2)||(j==x/2+1))
{
board[i][j] = Square(false, NA, index);
index++;
}
else if(j < x/2)
{
board[i][j] = Square(true, P1, index);
index++;
}
else if(j > x/2+1)
{
board[i][j] = Square(true, P2, index);
index++;
}
}
else
{
board[i][j] = Square(false, NA, 0);
}
}
}
}
Function in Board.cpp causing the error:
bool canMoveL(int i, int j)
{
bool temp = false;
Square sq = board[i][j];
if((i-1 < 0)||((sq.getPl() == P1)&&(j-1 >= bSize))||((sq.getPl() == P2)&&(j-1 < 0)))
{
return temp;
}
if(sq.getOcc() == 0)
{
return temp;
}
else
{
if(sq.getPl() == P1)
{
temp = true;
}
else if(sq.getPl() == P2)
{
temp = true;
}
}
return temp;
}
Note: Since I haven't been able to test the canMovL function, I realise there may be errors in that as well, but I'm specifically just looking for help fixing the error I get with the board array in canMovL(). Thanks.
bool canMoveL(int i, int j)
should be
bool Board::canMoveL(int i, int j).
else you define free function unrelated to Board.
I was working on this code for a project on school and when I wanted to try debugging my code just got segmentation fault before even running the first line in main() so i was wondering if i miss something on my code or is the compiler's fault.
#include <iostream>
using namespace std;
class poly
{
public: int a[1000000];
private:
int forx(int x);
public:
poly(){cout<<"add";}
~poly(){cout<<"kill";}
void add();
void sum(int *x,int *y);
void dif(int *x,int *y);
void mult(int *x,int *y);
void renew();
};
void poly::add()
{
int i,n;
cin>>n;
a[0]=n;
for (i=1; i<=n; i++)
{
cin>>a[i];
}
}
int poly::forx(int x)
{
int s,i,p;
p=1;
for (i=1; i<=a[0]; i++)
{
s+=p*a[i];
p*=x;
}
return s;
}
void poly::sum(int *x,int *y)
{
int i,m=x[0]>y[0]?x[0]:y[0];
a[0]=m;
for (i=1; i<=a[0]; i++)
{
a[i]=x[i]+y[i];
}
}
void poly::dif(int *x,int *y)
{
int i,m=x[0]>y[0]?x[0]:y[0];
a[0]=m;
for (i=1; i<=a[0]; i++)
{
a[i]=x[i]-y[i];
}
for (i=a[0]; i>0; i--)
{
if (a[i]!=0) break;
a[0]--;
}
}
void poly::mult(int *x,int *y)
{
int i,j,k;
for (i=1; i<=(x[0]+y[0]-2); i++)
{
j=0;
k=y[0]-1;
while (j+k!=i)
{
if (j+k>i) k--;
if (j+k<i) j++;
}
while (j<x[0] && k>=0)
{
a[i]+=x[j]*y[k];
k--;
j++;
}
}
}
void poly::renew () {
int i;
for (i=1; i<=a[0]; i++)
{
cout<<a[i];
}
}
int main()
{
cout<<"starting";
poly w;
w.add();
poly h;
h.add();
poly o;
o.sum(w.a,h.a);
o.renew();
o.dif(w.a,h.a);
o.renew();
o.mult(w.a,h.a);
o.renew();
}
Becase of int a[1000000];, size of poly class is very large. Making a (actually you are making 3) local variable(s) of this class (on stack) would give you segmentation fault.
You can try making them static or move them to global scope or alloc them dynamically.
...
static poly w;
w.add();
static poly h;
h.add();
static poly o;
...
Another solution is to replace arrays with std::vector
change public: int a[1000000]; to
...
public: std::vector<int> a;
...
poly() : a(1000000) {cout<<"add";}
...
Now you can create local objects of this class.
Another related question Segmentation fault on large array sizes
I'm trying to swap to objects of class. It's a puzzle game something like http://www.neos-guide.org/sites/default/files/Fifteen_puzzle.png. I'm using std::swap but it doesn't work. Here's code:
if (puzzle[i-1][j].getNum() == 16) {
std::swap(puzzle[i][j], puzzle[i-1][j]);
}
else if (puzzle[i+1][j].getNum() == 16) {
std::swap(puzzle[i+1][j], puzzle[i][j]);
}
else if (puzzle[i][j+1].getNum() == 16 && j != 3) {
std::swap(puzzle[i][j+1], puzzle[i][j]);
}
else if (puzzle[i][j-1].getNum() == 16 && j != 0) {
std::swap(puzzle[i][j-1], puzzle[i][j]);
}
And here's my puzzle class:
class Puzzle {
private:
int x;
int y;
int num;
ALLEGRO_FONT *font;
public:
Puzzle();
~Puzzle();
void init(int posx, int posy, ALLEGRO_FONT *font);
void setNum(int num);
void draw();
void setXY(int x, int y);
int getX();
int getY();
int getNum();
bool click(int mx, int my);
};
I have tried std::swap on 2D array of integers, on two objects of same class, on 1D object array and 2D object array. It worked. It just doesn't work in this implementation. I also tried to comment out ALLEGRO_FONT as I thought that there is the problem. But nothing has changed. cplusplus.com says that I need to include <algorithm> or <utility>. I tried both (not at the same time) but it didn't work. Any suggestions? Thank you :)
I'm new on programming and I need to learn it for Arduido purposes. I used this code to test some things but I keep getting the "expected primary function before "int"" error, and it also says that the position functions isn't declared.
Am I declaring it wrong? I've tried many different things and kept getting the same message. My objective is to keep typing '1' and get 3, 6, 9, etc on the screen from calling the position function at cout.
#include <iostream>
using namespace std;
int main()
{
int degree=0;
int r=1;
while (r != '0')
{
cin >> r;
// this is where I get the error //
int position()
{
if ( r == 1 )
{
degree=degree+3;
}
return degree;
}
cout << position();
}
return 0;
}
Nesting functions is not allowed in C++. Change your code to be:
#include <iostream>
using namespace std;
int main()
{
// code
}
int position()
{
}
The problem is that your function position is in your function main, which is not possible in c++. Move the position out of the main function.
int position(int r, int degree)
{
if ( r == 1 )
{
degree=degree+3;
}
return degree;
}
int main()
{
int degree=0;
int r=1;
while (r != '0')
{
cin >> r;
cout << position(r, degree);
}
return 0;
}
I am getting the inconsistent error while i am trying to execute the following program.can anyone tell me where i am doing wrong...
int a,b=0;
int getvalue(int c)
{
int n=0;
a=c;
if(n<c)
n=a+b;
return n;
}
int newvalue(int c)
{
int n=0;
int a=c;
if(n<getvalue(c))
n=a+b;
return n;
}
voidmain()
{
int j=1;
int b=newvalue(j);
cout<<a+b+j<<end1;
return 0;
}
Try this (end1 should be endl) and I fixed the main signature.
#include <iostream>
int a,b=0;
int getvalue(int c)
{
int n=0;
a=c;
if(n<c)
n=a+b;
return n;
}
int newvalue(int c)
{
int n=0;
int a=c;
if(n<getvalue(c))
n=a+b;
return n;
}
int main()
{
int j=1;
int b=newvalue(j);
std::cout<<a+b+j<<std::endl;
return 0;
}
Without testing I suspect that a space between void and main could help you. voidmain() -> void main(). However if you already can compile the code my advice won't help.