Blue Bridge Cup competition Exams: image rotation problem

Picture picture rotation is one of the easiest approach, in this question, it is necessary to rotate the picture clockwise 90 degrees. By a n × m two-dimensional array of a picture represented, for example, a 3 × 4 picture gives examples:
. 1. 3. 5. 7
. 9. 8. 6. 7
. 3. 5. 9. 7
image rotated 90 degrees clockwise this image as follows:
. 9. 1. 3
. 5. 8. 3
. 9. 7. 5
. 7. 6. 7
a given initial image, calculate the rotated image.
[Input format
of the first line of input contains two integers n and m, represent the number of rows and columns. Next n lines of m integers, denotes a given image. Each picture element (pixel) is an integer value between 0 and 255 (including 0 and 255).
[] Output format
output m rows and n columns, represent the rotated image.

[Sample input]
. 3. 4
. 1. 3. 5. 7
. 9. 8. 6. 7
. 3. 5. 9. 7
[Sample] output
. 3. 9. 1
. 5. 8. 3
. 9. 7. 5
. 7. 6. 7

We use python to solve the road problem, answer the following:

n,m = map(int ,input().split())       #map的作用是把输入的str映射成int
lst = []                              
for i in range(n):
	#先用split把输入的字符串转换成字符串列表,然后用int映射成整形map,最后再转换成整形list
	lst.append(list(map(int,input().split())))   

# print(lst)
n_list =[]
for i in range(m):
	n_line = [ lst[n-1-j][i] for j in range(n) ]
	n_list.append(n_line)

for x in n_list:
	for y in x:
		print(y,end=' ')
	print('')

Results are as follows:
Here Insert Picture Description
knowledge this problem has involved:
the 1.INPUT () function to get the user's keyboard input string generated;
2. The method of dividing the split string to string list;
3.map () method of the string list shaping mapped map object;
4.list method of forcibly turn the object into a list object map;
the method for generating the list of a new formula of shaping list; . 6
... the append method to add the list of elements;
with the end of a function parameter 7.print and outputs an empty string wrap techniques;

The core problem of this algorithm:
coordinate transformation mapping, coordinate transformation we started law can be observed, such as
new coordinates (1,1) corresponds to the old coordinate (3,1)
new coordinates (1,2) corresponds to the old coordinate (2,1 )
new coordinates (1,3) correspond to (1,1) the old coordinate
new coordinates (2,1) corresponding to the old coordinate (3,2)
new coordinates (2,2) corresponding to the old coordinate (2,2)
new coordinates (2 3) corresponds to the old coordinate (1,2)
...
we soon found that the law, which is the new coordinates of the horizontal axis is the vertical axis coordinate old and new vertical coordinate is the complement of the old coordinates, so we can write the core algorithms:

n_list = [ [ lst[n-1-j][i] for j in range(n) ] for i in range(m)]

Notice that it simplifies the source code for two cycles using nested list formula.

Published 207 original articles · won praise 16 · views 9952

Guess you like

Origin blog.csdn.net/weixin_41855010/article/details/104569185
Recommended