Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8112973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:40:36+00:00 2026-06-06T02:40:36+00:00

I am trying to solve this SPOJ problem . The question asks to find

  • 0

I am trying to solve this SPOJ problem. The question asks to find the shortest path for each black(1) pixel.

Since it is a unweighted graph I used BFS.

for input:

3 3
010
000
000

it’s giving:

323
434
343

instead of:

101
212
323

This is my code

#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
typedef pair < int, int >ii;
int R, C, i, j;
queue < ii > myQueue;

int visit[100][100];
int dist[100][100];
void bfs(ii s)
{
    int i, j;
    int count = 0;
    ii node;
    memset(visit, 0, sizeof(visit));
    memset(dist, 0, sizeof(dist));
    myQueue.push(s);
    dist[node.first][node.second] = 0;

    while (!myQueue.empty()) {
        node = myQueue.front();
        myQueue.pop();
        if (visit[node.first][node.second])
            continue;
        visit[node.first][node.second] = 1;

        //cout << node.first << " " << node.second << "\n";
        i = node.first;
        j = node.second;

        if (j - 1 < R && j - 1 >= 0) {
            myQueue.push(make_pair(i, j - 1));
            if(dist[i][j - 1] == 0)
            dist[i][j - 1] = dist[i][j] + 1;
        }
        if (j + 1 < R && j + 1 >= 0) {
            myQueue.push(make_pair(i, j + 1));
            if(dist[i][j+1] == 0)
            dist[i][j + 1] = dist[i][j] + 1;
        }
        if (i - 1 < C && i - 1 >= 0) {
            myQueue.push(make_pair(i - 1, j));
            if(dist[i-1][j] == 0)
            dist[i - 1][j] = dist[i][j] + 1;
        }
        if (i + 1 < C && i + 1 >= 0) {
            myQueue.push(make_pair(i + 1, j));
            if(dist[i+1][j] == 0)
            dist[i + 1][j] = dist[i][j] + 1;
        }
    }
}

int main()
{
    char input[100][100];
    scanf("%d %d", &R, &C);
    for (i = 0; i < R; i++)
        scanf("%s", &input[i]);
    int GRID[R][C];
    for (i = 0; i < R; i++)
        for (j = 0; j < C; j++)
            GRID[i][j] = input[i][j] - '0';
    for (i = 0; i < R; i++)
        for (j = 0; j < C; j++) {
            if (GRID[i][j] == 1)
                bfs(make_pair(i, j));
        }
    for (i = 0; i < R; i++) {
        for (j = 0; j < C; j++) {
            printf("%d", dist[i][j]);
        }
        printf("\n");
    }
}

ideone

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T02:40:38+00:00Added an answer on June 6, 2026 at 2:40 am

    Try this:

    if (j - 1 < R && j - 1 >= 0) {
        myQueue.push(make_pair(i, j - 1));
        if(dist[i][j - 1] == 0)
        dist[i][j - 1] = dist[i][j] + 1;
    }
    

    do this for all dist[][].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying to solve this problem on SPOJ (http://www.spoj.pl/problems/REC/) F(n) = a*F(n-1) +
I am trying to solve this problem http://www.spoj.pl/problems/PEBBMOV/ . I think I have the
I was trying to solve this problem - http://www.spoj.pl/problems/LISA/ I thought of Greedy initially,
I am trying to solve this problem : https://www.spoj.pl/problems/CERC07S/ I have identified that i
I have been trying to solve this problem for a while, could not find
I was trying this problem, http://www.spoj.pl/problems/TWOSQ/ . We have to find the number of
I am trying to solve this problem from SPOJ, it's a dynamic programming problem,
So I'm trying to solve this problem that asks to look for palindromes in
since I'm trying to solve this problem for some days, I thought about giving
Trying to solve this problem . I would like to learn how the bootstrapper

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.