Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python Program that reads a matrix MN which represents the map and prints the number of enemies ready to attack the kingdom: # 2 D

Python Program that reads a matrix MN which represents the map and prints the number of enemies ready to attack the kingdom:
#2D array to store map of kingdom
arr=[]
#Take input from user for size of matrix
[M][N]= int(input())
#Taking values from user
for i in range(0,M):
arr.append([int(j) for j in input().split()])
#2D list to store true or false
#True represent current block is already visited
#False represent not visited
check =[]
#initially all values set to false
for i in range(M):
temp =[]
for j in range(N):
temp.append(False)//appends an element to end of the list
check.append(temp)
#variable to store the final sum
sum = int(0)
for i in range(M):
for j in range(N):
if arr[i][j]==0:
if i!=0 and check[i-1][j]== False:
check[i-1][j]= True
sum = sum+arr[i-1][j]
if j!=0 and check[i][j-1]== False:
check[i][j-1]= True
sum = sum+arr[i][j-1]
if j!=N-1 and check[i][j+1]== False:
check[i][j+1]= True
sum = sum+arr[i][j+1]
if i!=M-1 and check[i+1][j]== False:
check[i+1][j]= True
sum = sum+arr[i+1][j]
print(sum)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems For Advanced Applications 15th International Conference Dasfaa 2010 Tsukuba Japan April 2010 Proceedings Part 1 Lncs 5981

Authors: Hiroyuki Kitagawa ,Yoshiharu Ishikawa ,Wenjie Li ,Chiemi Watanabe

2010th Edition

3642120253, 978-3642120251

More Books

Students also viewed these Databases questions