dimanche 21 juin 2020

Code just loads for a second, then closes without doing anything

For some reason, whenever I run this code, it just opens; loads for a sec; then closes without doing anything. Whenever I try to narrow it down to a piece of code, it makes absolutely no sense, like the line int dirX.

#include <iostream> 
#include <queue>

using namespace std;

void solve()
{
    // ENTER CODE BELOW

    struct Loc
    {
        int x, y;
        Loc (int xx=0, int yy=0) : x(xx), y(yy) {}
    };
    int n=0, currX=1002, currY=1002, dx[]={-1,1,0,0},dy[]={0,0,-1,1}; string str=""; bool isFence[2010][2010]; queue<Loc> q;
    int ret=-1;
    
    for (int i = 0; i < 2005; i++) {
        for (int j = 0; j < 2005; j++) {
            isFence[i][j]=false;
        }
    }
    cin >> n >> str;
    isFence[currX][currY]=true;
    int dirX, dirY;
    for (auto i : str)
    {
        dirX=0; dirY=0;
        if (i=='N') dirX=-1;
        else if (i=='S') dirX=1;
        else if (i=='W') dirY=-1;
        else dirY=1;
        for (int j = 0; j < 2; j++) {
            currX += dirX;
            currY += dirY;
            isFence[currX][currY]=true;
        }
    }
    Loc curr; int nx, ny;
    for (int i = 0; i < 2005; i++)
    {
        for (int j = 0; j < 2005; j++)
        {
            cout << isFence[i][j] << endl;
            if (isFence[i][j]) continue;
            ret++;
            q = std::queue<Loc>();
            q.push(Loc(i,j));
            isFence[i][j]=true;
            while (!q.empty())
            {
                curr = q.front(); q.pop();
                for (int k = 0; k < 4; k++) {
                    nx = curr.x+dx[k]; ny=curr.y+dy[k];
                    if (nx >= 0 && nx < 2005 && ny >= 0 && ny<2005 && !isFence[nx][ny]) {
                        isFence[nx][ny]=true;
                        q.push(Loc(nx, ny));
                    }
                }
            }
        }
    }

    cout << ret;

    // ENTER CODE ABOVE
}

int main()
{
    solve();
}

Also, the reason I have all my code in the solve() function was because this is an assignment and I have to do it this way.

Sidenote: I wrote this code very quickly, so it's very badly formatted.

Aucun commentaire:

Enregistrer un commentaire