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 8073249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:16:38+00:00 2026-06-05T14:16:38+00:00

I try to solve the spoj problem using python . My algorithm should be

  • 0

I try to solve the spoj problem using python. My algorithm should be only O(n^2), but still TLE is returned…
My method is just a multiple source BFS.

  1. Find out the position of all 1
  2. Run BFS on each ‘1’, store the shortest distance into an 2D list named “ans”
  3. print ans

problem link: http://www.spoj.pl/problems/BITMAP/

if __name__ == '__main__':
    n = int(input())    #reading number of test cases
    for k in range(n): 
        (row, col) = input().split()    #row and col
        row = int(row)
        col = int(col)
        #store the bitmap
        bitmap = []
        for i in range(row):
            line = input()
            bitmap.append(line)
        ans = [[False for i in range(col)] for j in range(row)]     #init a 2d array to store answer
        front = []
        num_element = row*col
        processed = 0
        for i in range(row):
            for j in range(col):
                if(bitmap[i][j] == '1'):
                    ans[i][j] = 0
                    front.append((i,j))
                    processed = processed +1
        while processed < num_element:
            new_list = []
            for node in front:
                i = node[0]
                j = node[1]
                new_distance = ans[i][j] + 1
                if(i> 0 and ans[i-1][j] is False):
                    ans[i-1][j] =new_distance                    
                    new_list.append((i-1,j))
                    processed = processed +1
                if(i< row -1 and ans[i+1][j] is False):
                    ans[i+1][j] =new_distance
                    new_list.append((i+1,j))
                    processed = processed +1
                if(j > 0 and ans[i][j-1] is False):
                    ans[i][j-1] =new_distance
                    new_list.append((i,j-1))
                    processed = processed +1
                if(j < col -1 and ans[i][j+1] is False):
                    ans[i][j+1] =new_distance
                    new_list.append((i,j+1))
                    processed = processed +1
            front = new_list
        #print the answer
        for line in ans:
            s = map(str, line)
            print(" ".join(s))
        input()
  • 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-05T14:16:40+00:00Added an answer on June 5, 2026 at 2:16 pm

    Thank you. I finally implement the same algorithm in C++ and accepted! It seems that problem needs 2d array is not suitable for python.

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

Sidebar

Related Questions

i try to solve a problem but didnt succeed. I retrieve datas from google
i try to solve a problem but did not succeed. I have a index
I've tried all sorts of design approaches to solve this problem, but I just
I just started using the Eclipse Memory Analyzer to try to solve a memory
I try to solve a problem for a few weeks, but I don't find
I've read several example programs and tutorials to try and solve my problem but
I try to solve this problem several times and give up. Now, when I
I have a strange problem that I could not solve. When I try to
Recently, I'm learning F#. I try to solve problem in different ways. Like this:
i try to solve a problem. i have an index page and users enter

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.