바로 아래 문제를 거꾸로 한 문제입니다. 먼저 높이와 넓이를 입력한 후에 (해상도), 0과 1로 구성된 그림을 입력하면, 이를 비트맵 코드로 변환하여 출력합니다. 즉, 라인당 0의 갯수와 1의 갯수를 번갈아 코드로 출력합니다. 단, 항상 각 코드 라인의 첫번째 숫자는 0의 갯수이므로, 입력하는 그림에서 1로 시작하는 라인들은 0으로 시작하는 코드를 출력합니다.
#Input1
height & width: 6 5
0 1 1 1 0
0 0 0 0 1
0 1 1 1 1
1 0 0 0 1
1 0 0 0 1
0 1 1 1 1
#Output1
1 3 1
4 1
1 4
0 1 3 1
0 1 3 1
1 4
#Input2
height & width: 9 17
0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0
0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 1 0
0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 1 0
0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0
0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0
0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0
0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
#Output2
4 11 2
4 9 2 1 1
4 9 2 1 1
4 11 2
4 9 4
4 9 4
5 7 5
0 17
1 15 1
#Input3
height & width: 13 17
0 0 0 0 0 0 1 1 1 1 1 0 0 1 1 1 0
0 0 0 0 1 1 0 0 0 0 0 1 1 0 0 0 1
0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 1
0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0
0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0
0 0 1 0 0 0 0 0 0 0 0 0 0 1 1 0 0
0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0
0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0
0 0 1 0 0 0 0 0 0 0 1 0 0 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1
1 0 0 1 0 0 0 0 0 0 0 1 0 0 1
1 0 0 1 1 0 0 0 0 0 1 1 0 0 1
1 1 0 0 1 1 0 0 0 1 1 0 0 1 1
0 1 0 0 0 1 1 1 1 1 0 0 0 1 0
0 1 1 0 0 0 0 0 0 0 0 0 1 1 0
0 0 1 1 1 0 0 0 0 0 1 1 1 0 0
0 0 0 0 1 1 1 1 1 1 1 0 0 0 0
11개의 풀이가 있습니다.
Python 3
from itertools import groupby
def main():
bc = BitmapCoder()
print(bc)
class BitmapCoder:
def __init__(self):
self._reader()
def _reader(self):
(self.height, self.width) = (int(s) for s in input("Height & Width: ").strip().split())
self.matrix = []
for h in range(self.height):
lineinput = input().strip().replace(" ", "")
self.matrix.append(BitmapCoder._string2chunklist(lineinput))
def __str__(self):
return "\n".join(self.matrix)
@staticmethod
def _string2chunklist(inputstr: str) -> str:
rst = [str(len(list(g))) for _, g in groupby(inputstr)]
if inputstr[0] == "1":
rst.insert(0, "0")
return " ".join(rst)
if __name__ == '__main__':
main()
def convert (a):
arr=[]
while True:
n=len(a)
a = a.lstrip('0')
arr.append(n-len(a))
if len(a)==0:
break
n=len(a)
a = a.lstrip('1')
arr.append(n-len(a))
if len(a)==0:
break
return arr
a = input("height&width:").split(" ")
h = int(a[0])
w = int(a[1])
codearr=[]
for i in range(h):
while True:
print(i+1,'번째',end=' ')
b =''.join(input('0 1 코드 입력 하시오').split(' '))
if len(b) == w:
codearr.append(b)
break
for i in codearr:
for k in convert(i):
print(k,end=' ')
print('')
package bitmap_coder2;
import java.util.Scanner;
public class BitmapCoder2 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int i, j, num;
/*입력받기*/
System.out.print("height & width:");
int height = in.nextInt();
int width = in.nextInt();
String[] input_raw = new String[height];
String dummy = in.nextLine();
for(i=0; i<height; i++)
input_raw[i] = in.nextLine();
boolean previous; //true:1, false:0
for(i=0; i<height; i++) {
num=0;
previous=false;
for(j=0; j<input_raw[i].length(); j++) {
if(input_raw[i].charAt(j)=='0') {
if(previous==false)
num++;
else {
System.out.print(num+" ");
previous=false;
num=1;
}
}
else if(input_raw[i].charAt(j)=='1') {
if(previous==false) {
System.out.print(num+" ");
previous=true;
num=1;
}
else
num++;
}
}
System.out.println(num);
}
}
}
#Output3
6 5 2 3 1
4 2 5 2 3 1
3 1 9 1 2 1
3 1 9 1 1 1 1
2 1 11 1 2
2 1 10 2 2
2 1 9 1 1 1 2
2 1 8 1 2 1 2
2 1 7 1 3 1 2
1 1 1 1 4 2 3 1 3
0 1 2 1 2 2 5 1 3
0 1 3 2 5 2 4
1 3 2 5 6
#Output4
6 2 2 2 6
5 1 2 2 2 1 5
6 6 6
4 2 6 2 4
3 1 10 1 3
2 1 12 1 2
2 1 3 1 4 1 3 1 2
1 2 12 2 1
0 1 16 1
0 1 6 1 2 1 6 1
0 1 7 2 7 1
1 1 14 1 1
2 1 12 1 2
2 1 5 2 5 1 2
3 1 10 1 3
4 2 6 2 4
6 6 6
#Output5
4 7 4
2 3 5 3 2
1 2 9 2 1
1 1 11 1 1
0 2 11 2
0 1 3 2 3 2 3 1
0 1 3 2 3 2 3 1
0 1 13 1
0 1 2 1 7 1 2 1
0 1 2 2 5 2 2 1
0 2 2 2 3 2 2 2
1 1 3 5 3 1 1
1 2 9 2 1
2 3 5 3 2
4 7 4
def bitmap_coder(x):
for i in range (len(x)):
x[i]=x[i].split(' ')
while ('' in x[i]):
x[i].remove('')
for j in range (len(x[i])):
x[i][j]=int(x[i][j])
for i in range (len(x)):
if x[i][0]==0: #0으로 시작시
present_num,present_num_sum=0,0
for j in range (len(x[i])):
if present_num==x[i][j]:
present_num_sum+=1
else:
print(present_num_sum,end=' ')
present_num_sum=1
if x[i][j]==0:
present_num=0
elif x[i][j]==1:
present_num=1
print(present_num_sum)
elif x[i][0]==1: #1로 시작시에는 0을 앞에 출력
print(0,end=' ')
present_num,present_num_sum=1,0
for j in range (len(x[i])):
if present_num==x[i][j]:
present_num_sum+=1
else:
print(present_num_sum,end=' ')
present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map_coder(inp3)
print('\n#Output4')
bitmap_coder(inp4)
print('\n#Output5')
bitmap_coder(inp5)
def bitmapcoder(bit):
for n, s in enumerate(bit):
print('# Output{}'.format(n+1))
for l in s[2:]:
line = []
p = l.split()
cnt = 0
for b in range(0, len(p)-1):
if b == 0 and p[b] == '1':
line.append('0')
if p[b] == p[b+1]:
cnt += 1
else:
cnt += 1
line.append(str(cnt))
cnt = 0
cnt += 1
line.append(str(cnt))
print(' '.join(line))
print()
if __name__ == '__main__':
bit_txt = open('./bitmapcoder.txt', 'r').read().split('\n\n')
bit = [line.split('\n') for line in bit_txt]
bitmapcoder(bit)
#input2
height, width = 9, 17
bitmap = '''0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0
0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 1 0
0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 1 0
0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0
0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0
0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0
0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0'''.split('\n')
bitmap = [bitmap[i].split(' ') for i in range(height)]
coder = []
for i in range(height):
if bitmap[i][0] == '0':
line = []
elif bitmap[i][0] == '1':
line = [0]
count = 1
for j in range(1,width):
if bitmap[i][j] == bitmap[i][j-1]:
count += 1
elif bitmap[i][j] != bitmap[i][j-1]:
line.append(count)
count = 1
line.append(count)
coder.append(line)
import re
def sol() :
height_width, bit_map = list(map(int, input("height & width : ").split())), []
for g in range(0, height_width[0]) :
bit_map.append("".join(input("INPUT : ").split()))
if len(bit_map[-1]) != height_width[1] : return print("INVALID INPUT")
comp = re.compile("1+|0+")
for g2 in bit_map :
if g2[0] == '1' :
print([0]+list(map(len, comp.findall(g2))))
continue
print(list(map(len, comp.findall(g2))))
if __name__ == "__main__" :
sol()
결과
height & width : 6 5
INPUT : 0 1 1 1 0
INPUT : 0 0 0 0 1
INPUT : 0 1 1 1 1
INPUT : 1 0 0 0 1
INPUT : 1 0 0 0 1
INPUT : 0 1 1 1 1
[1, 3, 1]
[4, 1]
[1, 4]
[0, 1, 3, 1]
[0, 1, 3, 1]
[1, 4]
height & width : 9 17
INPUT : 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0
INPUT : 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 1 0
INPUT : 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 1 0
INPUT : 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0
INPUT : 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0
INPUT : 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0
INPUT : 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0
INPUT : 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
INPUT : 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
[4, 11, 2]
[4, 9, 2, 1, 1]
[4, 9, 2, 1, 1]
[4, 11, 2]
[4, 9, 4]
[4, 9, 4]
[5, 7, 5]
[0, 17]
[1, 15, 1]
H = int(input('height & width: ').split()[0])
for _ in range(H):
row = [int(x) for x in input().split()]
v, cnt = 0, 0
for b in row + [1 - row[-1]]:
if v != b:
cnt += 1
else:
print(cnt, end=' ')
v = 1 - v
cnt = 1
print()
def bitmap_decoder(array, zero, one, result):
"""Decode array with '0' or '1' and return the result
This function does not decode an index 0 of an array.
array: list with '0' or '1'. '0' and '1' are strings.
zero, one: 0 or 1, 0 and 1 are integers.
result: list"""
i = 1
while i < len(array):
if array[i] == '0':
if one == 0:
zero += 1
i += 1
if i == len(array):
result.append(zero)
break
else:
continue
else:
result.append(one)
zero += 1
one = 0
i += 1
if i == len(array):
result.append(zero)
break
else:
if zero == 0:
one += 1
i += 1
if i == len(array):
result.append(one)
break
else:
result.append(zero)
one += 1
zero = 0
i += 1
if i == len(array):
result.append(one)
break
else:
continue
return result
def print_result(array):
"""Arrange and return the decoded array.
array: list with '0' or '1'. '0' and '1' are strings.
"""
if array[0] == '1':
result = [0]
zero = 0
one = 1
bitmap_decoder(array, zero, one, result)
else:
result = []
zero = 1
one = 0
bitmap_decoder(array, zero, one, result)
return result
height = input("Enter height :")
height = height.strip()
height = int(height)
assert type(height) == int, "Enter an integer"
width = input("Enter width: ")
width = width.strip()
width = int(width)
assert type(width) == int, "Enter an integer"
arrays = []
for n in range(0, height):
array = input("Enter an array consisted of '0' or '1': ")
array = array.strip()
array = array.split(' ')
assert len(array) <= width, "An array can not be greater than width"
assert '0' in array or '1' in array
arrays.append(array)
for array in arrays:
result = print_result(array)
print(result)
col,row = map(int,input("Input col row:"),split())
data_arr = [list(map(int,input().split())) for _ in range(row)]
zero_arr = []
one_arr = []
for i in range(row):
zero_arr.clear();one_arr.clear()
for j in range(col):
if j == 0:
if data_arr[i][j] == 0: zero_arr.append(0)
else: one_arr.append(1);print(0,end=" ")
else:
if data_arr[i][j] == data_arr[i][j-1]:
if data_arr[i][j] == 0 : zero_arr.append(0)
else: one_arr.append(1)
else:
if data_arr[i][j] == 1:
one_arr.append(1)
print(len(zero_arr),end=" ")
zero_arr.clear()
else:
zero_arr.append(0)
print(len(one_arr),end=" ")
one_arr.clear()
if len(zero_arr) == 0: print(len(one_arr),end="\n")
elif len(one_arr) == 0: print(len(zero_arr),end="\n")
def bitmapCoder(h, w, map):
for r in range(h):
code = []
zs, os = 0, 0
for c in range(w):
if map[r][c] == 0:
if c > 0 and map[r][c-1] == 1:
code.append(str(os))
os = 0
zs += 1
elif map[r][c] == 1:
if c == 0 or ( c > 0 and map[r][c-1] == 0):
code.append(str(zs))
zs = 0
os += 1
if map[r][w-1] == 0:
code.append(str(zs))
else:
code.append(str(os))
print(' '.join(code))
print('Input1')
h, w = 6, 5
map = [[0, 1, 1, 1, 0], [0, 0, 0, 0, 1], [0, 1, 1, 1, 1],
[1, 0, 0, 0, 1], [1, 0, 0, 0, 1], [0, 1, 1, 1, 1 ]]
bitmapCoder(h, w, map)
print('\nInput2')
h, w = 9, 17
map = [[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0],
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0],
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0]]
bitmapCoder(h, w, map)
# ## 입력 받을 때
# height = int(input("Enter height :"))
# width = int(input("Enter width: "))
# map = []
# for h in range(height):
# line = [int(s) for s in input('Bitmap의 {0}번째 라인 을 입력 하세요: '.format(h+1)).split()]
# map.append(line)
# bitmapCoder(height, width, map)