Problem:


Score: 300 points

Problem Statement

We have a canvas divided into a grid with H rows and W columns. The square at the i-th row from the top and the j-th column from the left is represented as (i, j).

Initially, all the squares are white. square1001 wants to draw a picture with black paint. His specific objective is to make Square (i, j) black when s_{i, j}= #, and to make Square (i, j) white when s_{i, j}= ..

However, since he is not a good painter, he can only choose two squares that are horizontally or vertically adjacent and paint those squares black, for some number of times (possibly zero). He may choose squares that are already painted black, in which case the color of those squares remain black.

Determine if square1001 can achieve his objective.  

Constraints

H is an integer between 1 and 50 (inclusive).

W is an integer between 1 and 50 (inclusive).

For every (i, j) (1 \leq i \leq H, 1 \leq j \leq W), s_{i, j} is # or ..

Input

Input is given from Standard Input in the following format:

H W
s_{1, 1} s_{1, 2} s_{1, 3} ... s_{1, W}
s_{2, 1} s_{2, 2} s_{2, 3} ... s_{2, W}
  :   :
s_{H, 1} s_{H, 2} s_{H, 3} ... s_{H, W}

Output

If square1001 can achieve his objective, print Yes; if he cannot, print No.

Sample Input 1

3 3
.#.
###
.#.

Sample Output 1

Yes

One possible way to achieve the objective is shown in the figure below. Here, the squares being painted are marked by stars.

Sample Input 2

5 5
#.#.#
.#.#.
#.#.#
.#.#.
#.#.#

Sample Output 2

No

square1001 cannot achieve his objective here.

Sample Input 3

11 11
...#####...
.##.....##.
#..##.##..#
#..##.##..#
#.........#
#...###...#
.#########.
.#.#.#.#.#.
##.#.#.#.##
..##.#.##..
.##..#..##.

Sample Output 3

Yes



Sample input:
3 3
.#.
###
.#.


Sample output:
Yes

Submission status: Accepted

Solution code:
W, H = map(int, input().split())
a = []
for i in range(W):
    a.append(list(input()))
ans = "Yes"
for j in range(W):
  for k in range(H):
    if a[j][k] == "#":
       if j == 0:
          if k == 0:
           if a[j][k+1]  == "#":
              continue
           elif a[j+1][k]  == "#":
              continue
           else:
              ans = "No"
              break 
          elif  k == H-1:
           if a[j][k-1] == "#":
              continue
           elif a[j+1][k]  == "#":
              continue
           else:
              ans = "No"
              break 
       elif j == W-1:
          if k == 0:
           if a[j][k+1]  == "#":
              continue
           elif a[j-1][k]  == "#":
              continue
           else:
              ans = "No"
              break 
          elif  k == H-1:
           if a[j][k-1] == "#":
              continue
           elif a[j-1][k]  == "#":
              continue
           else:
              ans = "No"
              break 
          else:
           if a[j][k-1] == "#":
              continue
           elif a[j][k+1]  == "#":
              continue
           elif a[j-1][k]  == "#":
              continue
           else:
              ans = "No"
              break
       elif k == 0:
         if a[j][k+1]  == "#":
            continue
         elif a[j-1][k]  == "#":
            continue
         elif a[j+1][k]  == "#":
            continue
         else:
            ans = "No"
            break 
       elif k == H-1:
         if a[j][k-1] == "#":
            continue
         elif a[j-1][k]  == "#":
            continue
         elif a[j+1][k]  == "#":
            continue
         else:
            ans = "No"
            break           
       else:
           if a[j][k-1] == "#":
              continue
           elif a[j][k+1]  == "#":
              continue
           elif a[j-1][k]  == "#":
              continue
           elif a[j+1][k]  == "#":
              continue
           else:
              ans = "No"
              break 
print(ans)

Submission status: Accepted

Solution code:
def main():
    h, w = input().split()
    h = int(h)
    w = int(w)

    lst =[]
    for i in range(h):
        tmp = input()
        lst.append(list(tmp))

    flag = False

    for i in range(len(lst)):
        if flag:
            break

        for j in range(len(lst[i])):
            if lst[i][j] == "#":
                if j != w - 1:
                    if lst[i][j+1] == "#":
                        continue

                if j != 0:
                    if lst[i][j-1] == '#':
                        continue

                if i != 0:
                    if lst[i-1][j] == '#':
                        continue

                if i != h -1:
                    if lst[i+1][j] == '#':
                        continue

                print("No")
                flag = True
                break

    if flag == False:
        print("Yes")

if __name__ == "__main__":
        main()

Submission status: Accepted

Solution code:
import numpy as np
H, W = map(int, input().split())

cambus = []
for h in range(H):
    cambus.append(list(input()))


def judge(_cambus, w, h):
    global W, H
    now = _cambus[h][w]
    if now == '.':
        return True
    status = False
    for dx, dy in zip([-1, 1, 0, 0], [0, 0, 1, -1]):
        x = w + dx
        y = h + dy
        if x > -1 and x < W and y > -1 and y < H:
            if now == _cambus[y][x]:
                status = True
    return status


flag =  True
for w in range(W):
    for h in range(H):
        flag = judge(cambus, w, h)
        if not flag: break
    if not flag: break

if not flag: print('No')
else: print('Yes')

Submission status: Accepted

Solution code:
import sys
H,W = map(int , input().split())
l = []
for i in range(H):
    l.append(list(input()))

for i in range(H):
    if i == 0:
        for j in range(W):
            if j == 0 :
                if l[i][j] == '#':
                    if l[i+1][j] == '.' and l[i][j+1] == '.':
                        print("No")
                        sys.exit()
            if j == W -1 :
                 if l[i][j] == '#':
                    if l[i+1][j] == '.' and l[i][j-1] == '.':
                        print("No")
                        sysexit()
            else :
                if l[i][j] == '#':
                    if l[i+1][j] == '.' and l[i][j-1] == '.' and l[i][j+1] == '.':
                        print("No")
                        sys.exit()
    if i == H -1:
        for j in range(W):
            if j == 0 :
                if l[i][j] == '#':
                    if l[i-1][j] == '.' and l[i][j+1] == '.':
                        print("No")
                        sys.exit()
            if j == W -1 :
                 if l[i][j] == '#':
                    if l[i-1][j] == '.' and l[i][j-1] == '.':
                        print("No")
                        sys.exit()
            else :
                if l[i][j] == '#':
                    if l[i-1][j] == '.' and l[i][j-1] == '.' and l[i][j+1] == '.':
                        print("No")
                        sys.exit()
    else :
        for j in range(W):
            if j == 0 :
                if l[i][j] == '#':
                    if l[i-1][j] == '.' and  l[i+1][j] == '.' and l[i][j+1] == '.':
                        print("No")
                        sys.exit()
            if j == W -1 :
                 if l[i][j] == '#':
                    if l[i-1][j] == '.' and  l[i+1][j] == '.' and l[i][j-1] == '.':
                        print("No")
                        sys.exit()
            else :
                if l[i][j] == '#':
                    if l[i-1][j] == '.' and  l[i+1][j] == '.' and l[i][j-1] == '.' and l[i][j+1] == '.':
                        print("No")
                        sys.exit()

print("Yes")

Submission status: Accepted

Solution code:
n = input().split()

n = [int(n[0]), int(n[1])]
H = n[0]
W=n[1]

s = [list(map(str, input().split())) for i in range(H)]

c = True
for i in range(H):
    for j in range(W):
        if(s[i][0][j] == '#'):
            if(i == 0):
                if(j == 0):
                    if(s[i + 1][0][j] == '.' and s[i][0][j + 1] == '.'):
                        c = False
                        break
                elif(j == W - 1):
                    if(s[i+1][0][j ] == '.' and s[i][0][j-1] == '.'):
                        c = False
                        break
                else:
                     if(s[i + 1][0][j] == '.' and s[i][0][j + 1] == '.' and s[i][0][j-1] == '.'):
                        c = False
                        break
            elif(i == H - 1):
                if(j == 0):
                    if(s[i - 1][0][j] == '.' and s[i][0][j +1] == '.'):
                        c = False
                        break
                elif(j == W - 1):
                    if(s[i-1][0][j] == '.' and s[i][0][j-1] == '.'):
                        c = False
                        break
                else:
                     if(s[i - 1][0][j] == '.' and s[i][0][j + 1] == '.' and s[i][0][j-1] == '.'):
                        c = False
                        break
            else:
                if(j == 0):
                    if(s[i - 1][0][j] == '.' and s[i][0][j +1] == '.' and s[i + 1][0][j] == '.'):
                        c = False
                        break
                elif(j == W - 1):
                    if(s[i-1][0][j] == '.' and s[i][0][j-1] == '.' and s[i + 1][0][j] == '.'):
                        c = False
                        break
                else:
                     if(s[i - 1][0][j] == '.' and s[i][0][j + 1] == '.' and s[i][0][j-1] == '.' and s[i + 1][0][j] == '.'):
                        c = False
                        break
                
    
if(c):
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
canvas = []

for h in range(H):
    input_line = input()
    l = []
    [l.append(input_line[i]=='#') for i in range(W)]
    canvas.append(l)

for h in range(H):
    for w in range(W):
        if canvas[h][w]:
            if not((h!=0 and canvas[h-1][w]) or (w!=0 and canvas[h][w-1]) or (h!=H-1 and canvas[h+1][w]) or (w!=W-1 and canvas[h][w+1])):
                print('No')
                break
    else:
        continue
    break
else:
    print('Yes')

Submission status: Accepted

Solution code:
H, W = map(int, raw_input().split())
P = [list(raw_input()) for h in range(H)]
ans = True
P.insert(0, ['.' for w in range(W)])
P.append(['.' for w in range(W)])
for i in range(len(P)):
  P[i].insert(0, '.')
  P[i].append('.')
for h in range(1,H+1):
  for w in range(1, W+1):
    if P[h][w] == '#':
      if P[h][w+1] != '#' and P[h+1][w] !='#' and P[h][w-1] != '#' and P[h-1][w] != '#':
        ans = False
print 'Yes' if ans else 'No'

Submission status: Accepted

Solution code:
h,w = map(int, raw_input().split())
P = []
for i in range(h):
    p = list(raw_input())
    P.append(p)

result = True
for i in range(h):
    for j in range(w):
        if P[i][j] == '#':
            try:
                up = P[i-1][j]
            except:
                up = ''
            try:
                down = P[i+1][j]
            except:
                down = '.'
            try:
                right = P[i][j+1]
            except:
                right = '.'
            try:
                left = P[i][j-1]
            except:
                left = '.'

            if up == '.' and down == '.' and right == '.' and left == '.':
                result = False

if result:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
h,w = map(int,input().split())

d=[]

d.append("."*(w+2))

for i in range(h):
    r = input()
    d.append("."+r+".")
    
d.append("."*(w+2))

#print(d)

no = False

def isok(d,i,j):
    c = 0
    c = c+1 if d[i-1][j]=="#" else c
    c = c+1 if d[i][j-1]=="#" else c
    c = c+1 if d[i][j+1]=="#" else c
    c = c+1 if d[i+1][j]=="#" else c
    result= True if c > 0 else False
    return result
        
    
for i in range(1,h+2):
    if no == True:
        break
    for j in range(1,w+2):
        if no == True:
            break
        if d[i][j] == "#":
            a = isok(d,i,j)
            if a == False:
                no = True
                break


result = "No" if no==True else "Yes"
print(result)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat May  5 20:55:26 2018
ABC096-3
@author: maezawa
"""

dxy = [[-1,0],[0,1],[1,0],[0,-1]]

def is_connect(i,j):
    global dxy
    global s
    for d in dxy:
        #print(i+d[0], j+d[1])
        if s[i+d[0]][j+d[1]] == '#':
            return True
    return False

h, w = list(map(int, input().split()))
s = ['-'*(w+2)]
for i in range(h):
    s_in = input()
    s_temp = '-'+s_in+'-'
    s.append(s_temp)
s.append('-'*(w+2))

#for i in range(h+2):
#    print(s[i])
is_paint = 'Yes'
for i in range(1,h+1):
    for j in range(1,w+1):
        if s[i][j] == '#':
            if is_connect(i,j):
                continue
            else:
                is_paint = 'No'
                break

print(is_paint)

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
array = [input().split() for i in range(h)]

m = 0

for i in range(1,h-1):
    for j in range(1,w-1):
        if array[i][0][j] == '#' and ( array[i-1][0][j]=='.' and array[i+1][0][j]=='.' and array[i][0][j-1]=='.' and array[i][0][j+1]=='.'):
            m = m+1
        else:
            pass

for i in range(1,h-1):
    if array[i][0][0]=='#' and ( array[i-1][0][0]=='.' and array[i+1][0][0]=='.' and array[i][0][1]=='.'):
        m = m + 1
    else:
        pass

for i in range(1,h-1):
    if array[i][0][w-1]=='#' and ( array[i-1][0][w-1]=='.' and array[i+1][0][w-1]=='.' and array[i][0][w-2]=='.'):
        m = m + 1
    else:
        pass

for j in range(1,w-1):
    if array[0][0][j]=='#' and ( array[0][0][j-1]=='.' and array[0][0][j+1]=='.' and array[1][0][j]=='.'):
        m = m + 1
    else:
        pass

for j in range(1,w-1):
    if array[h-1][0][j]=='#' and ( array[h-1][0][j-1]=='.' and array[h-1][0][j+1]=='.' and array[h-2][0][j]=='.'):
        m = m + 1
    else:
        pass

if array[0][0][0]=='#' and ( array[0][0][1]=='.' and array[1][0][0]=='.'):
    m = m + 1
else:
    pass

if array[0][0][w-1]=='#' and ( array[0][0][w-2]=='.' and array[1][0][w-1]=='.'):
    m = m + 1
else:
    pass

if array[h-1][0][w-1]=='#' and ( array[h-1][0][w-2]=='.' and array[h-2][0][w-1]=='.'):
    m = m + 1
else:
    pass


if array[h-1][0][0]=='#' and ( array[h-2][0][0]=='.' and array[h-1][0][1]=='.'):
    m = m + 1
else:
    pass

if m>=1:
    print('No')
else:
    print('Yes')

Submission status: Accepted

Solution code:
def a(n,x):
    if n<0:
        return n+1
    elif n >= x:
        return n-1
    else:
        return n
def f():
    h,w = map(int,input().split())
    s = []
    for i in range(h):
        s.append(list(input()))
    for i in range(h):
        for j in range(w):
            if s[i][j]=='#':
                if s[a(i-1,h)][j]=='.' and s[a(i+1,h)][j]=='.' and s[i][a(j-1,w)]=='.' and s[i][a(j+1,w)]=='.':
                    return 'No'
    return 'Yes'

print(f())

Submission status: Accepted

Solution code:
def printg():
	for i in range(H+2):
		print(G[i])

def p(s):
	if s == '#':
		return True
	else:
		return False

def isok(i,j):
	u = p(G[i][j-1])
	r = p(G[i+1][j])
	d = p(G[i][j+1])
	l = p(G[i-1][j])
	if u or r or d or l:
		return True
	else:
		return False

if __name__ == '__main__':
	H,W = map(int,input().split())
	G = [['B']*(W+2) for _ in range(H+2)]

	for i in range(H):
		l = input()
		for j in range(W):
			G[i+1][j+1] = l[j]

	for i in range(H+1):
		for j in range(W+1):
			if G[i][j] == '#':
				if not isok(i,j):
					print('No')
					exit()

	print('Yes')

Submission status: Accepted

Solution code:
[H,W] = list(map(int,input().split()))
canvas = []
for h in range(H):
    canvas.append([x for x in input()]) 

ans = True
for h,line in enumerate(canvas):
    for w,dot in enumerate(line):
        if dot == '#':
            ans0 = False
            if h-1 >=0 and canvas[h-1][w] == "#": ans0 =True #OK
            if h+1 < H and canvas[h+1][w] == "#": ans0 =True #OK
            if w-1 >=0 and canvas[h][w-1] == "#": ans0 =True #OK
            if w+1 < W and canvas[h][w+1] == "#": ans0 =True #OK
            if ans0 == False :
                ans = False
                break

if ans == True:
  print("Yes")
else:
  print("No")

Submission status: Accepted

Solution code:
h, w = input().split()
h = int(h)
w = int(w)
field = []
for _ in range(h):
    field.append(input())
    
ans = True
for i in range(h):
    for j in range(w):
        if field[i][j] == '#':
            temp = 1
            dd = [(1, 0), (-1, 0), (0, 1), (0, -1)]
            for ddd in dd:
                dx, dy = ddd
                nx, ny = i + dx, j + dy
                if nx >= 0 and nx < h and ny >= 0 and ny < w and field[nx][ny] == '#':
                    temp += 1
            if temp == 1:
                ans = False
                break

if ans:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
H,W = list(map(int,input().split()))
s = [ list(input()) for i in range(H) ]

checked =[ [ 0 for i in range(W)   ] for j in range(H)]
# ok 1
# ng -1
# yet 0

def check(i,j):
    if checked[i][j] == 1:
        return True
    if s[i][j] == ".":
        return True
    if i == 0 and j == 0:
        if s[1][0] == "#":
            checked[1][0] = 1
            return True
        elif s[0][1] == "#":
            checked[0][1] = 1
            return True
        else:
            return False
    if i == 0 and j == W -1:
        if s[1][j] == "#":
            checked[i][j] == 1
            return True
        elif s[0][j-1] == "#":
            checked[0][j-1] = 1
            return True
        else:
            return False
    if i == H - 1 and j == 0:
        if s[i - 1][0] == "#":
            checked[i- 1][0] = 1
            return True
        elif s[i][1] == "#":
            checked[i][1] = 1
            return True
        else:
            return False
    if i == H -1 and j == W - 1:
        if s[i - 1][j] == "#":
            checked[i- 1][j] = 1
            return True
        elif s[i][j - 1] == "#":
            checked[i][j -1] = 1
            return True
        else:
            return False
    if  i == 0:
        if s[i + 1][j] == "#":
            checked[i+1][j] = 1
            return True
        if s[i][j -1] == "#":
            checked[i][j-1] = 1
            return True
        if s[i][j+1] == "#":
            checked[i][j+1] = 1
            return True
        else:
            return False
    if i == H - 1:
        if s[i - 1][j] == "#":
            checked[i-1][j] = 1
            return True
        if s[i][j -1] == "#":
            checked[i][j-1] = 1
            return True
        if s[i][j+1] == "#":
            checked[i][j+1] = 1
            return True
        else:
            return False
    if j == 0:
        if s[i - 1][j] == "#":
            checked[i- 1][j] = 1
            return True
        if s[i + 1][j] == "#":
            checked[i+ 1][j] = 1
            return True
        elif s[i][j + 1] == "#":
            checked[i][ j +1] = 1
            return True
        else:
            return False
    if j == W - 1:
        if s[i - 1][j] == "#":
            checked[i- 1][j] = 1
            return True
        if s[i + 1][j] == "#":
            checked[i+ 1][j] = 1
            return True
        elif s[i][j - 1] == "#":
            checked[i][ j -1] = 1
            return True
        else:
            return False
    else:
        if s[i - 1][j] == "#":
            checked[i- 1][j] = 1
            return True
        if s[i + 1][j] == "#":
            checked[i+ 1][j] = 1
            return True
        elif s[i][j - 1] == "#":
            checked[i][ j -1] = 1
            return True
        elif s[i][j + 1] == "#":
            checked[i][ j +1] = 1
            return True
        else:
            return False

# 一行
for i,sw in enumerate(s):
    for j, sh in enumerate(sw):
        flag = check(i,j)
        if not flag:
            print("No")
            exit()

print("Yes")

Submission status: Accepted

Solution code:
h, w = map(int,input().split())
pic = []

kabe_up_down = []
for i in range(w+2):
    kabe_up_down.append('.')
pic.append(kabe_up_down)

for i in range(h):
    onelist = ['.']
    inp = list(str(input()))
    for mm in range(len(inp)):
        onelist.append(inp[mm])
    onelist.append('.')
    pic.append(onelist)
    
pic.append(kabe_up_down)    


flag = 1
a = 'a'
for i in range(1,h+1):
    for j in range(1,w+1):
        here = pic[i][j]
        if here =='#' and pic[i+1][j] == '#':
            pic[i][j] = a
            pic[i+1][j] = a
        elif here =='#' and pic[i][j+1] == '#':
            pic[i][j] = a
            pic[i][j+1] = a
        elif here =='#' and pic[i-1][j] == '#':
            pic[i][j] = a
            pic[i-1][j] = a
        elif here =='#' and pic[i][j+1] == '#':
            pic[i][j] = a
            pic[i][j+1] = a    
            
for i in range(1,h+1):
    for j in range(1,w+1):
        here = pic[i][j]
        if here == '#' and pic[i+1][j] == 'a':
            pic[i][j] = a
            pic[i+1][j] = a
        elif here =='#' and pic[i][j+1] == 'a':
            pic[i][j] = a
            pic[i][j+1] = a
        elif here =='#' and pic[i-1][j] == 'a':
            pic[i][j] = a
            pic[i-1][j] = a
        elif here =='#' and pic[i][j+1] == 'a':
            pic[i][j] = a
            pic[i][j+1] = a    
        
for i in range(1,h+1):
    for j in range(1,w+1):
        here = pic[i][j]
        if here == a and pic[i+1][j] == '#':
            pic[i][j] = a
            pic[i+1][j] = a
        elif here =='a' and pic[i][j+1] == '#':
            pic[i][j] = a
            pic[i][j+1] = a
        elif here =='a' and pic[i-1][j] == '#':
            pic[i][j] = a
            pic[i-1][j] = a
        elif here =='a' and pic[i][j+1] == '#':
            pic[i][j] = a
            pic[i][j+1] = a            
            
for i in range(1,h+1):
    for j in range(1,w+1):
        if pic[i][j] == '#':
            flag = 0
            

if flag == 0:
    print('No')
else:
    print('Yes')

Submission status: Accepted

Solution code:

if __name__ == "__main__":
    H, W = list(map(int, input().split()))
    canvas = []
    for h in range(0, H):
        canvas.append(input())
    # 全ての"#"のマス縦横のいずれかが"#"ならばOK
    for i in range(H):
        for j in range(W):
            # canvas[i][j]の四方に#があるか確認する
            if canvas[i][j] == "#":
                if (i-1 >= 0) and (canvas[i-1][j] == "."):
                    if (i+1 < H) and (canvas[i+1][j] == "."):
                        if (j-1 >= 0) and (canvas[i][j-1] == "."):
                            if (j+1 < W) and (canvas[i][j+1] == "."):
                                print("No")
                                exit()
    print("Yes")

Submission status: Accepted

Solution code:
H,W  = map(int, input().split()) 
S = [list(input()) for i in range(H)] 
T =[['.']*(W+2)]
for y in range (H):
    T.append(['.']+S[y]+['.'])

T.append(['.']*(W+2))

ans = 'Yes'
for y in range(1,H+1):
    for x in range(1,W+1):
        if ((T[y][x]=='#') and (T[y-1][x]=='.') 
            and (T[y+1][x]=='.') and (T[y][x+1]=='.') and (T[y][x-1]=='.') ):
            ans = 'No'

print(ans)

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().strip()

h,w = na()
ar = []
for i in range(h):
    ar.append(ns())

safe = True
def ex():
    for i in range(len(ar)):
        for j in range(w):
            if ar[i][j] == '#':
                if i == 0:
                    if j == 0:
                        if ar[i][j+1] == "#" or ar[i+1][j] == "#":
                            continue
                        else:
                            return False
                    elif j == w-1:
                        if ar[i][j-1] == "#" or ar[i+1][j] == "#":
                            continue
                        else:
                            return False
                    else:
                        if ar[i][j-1] == "#" or ar[i][j+1] == "#" or ar[i+1][j] == "#":
                            continue
                        else:
                            return False
                elif i == len(ar)-1:
                    if j == 0:
                        if ar[i][j+1] == "#" or ar[i-1][j] == "#":
                            continue
                        else:
                            return False
                    elif j == w-1:
                        if ar[i][j-1] == "#" or ar[i-1][j] == "#":
                            continue
                        else:
                            return False
                    else:
                        if ar[i][j-1] == "#" or ar[i][j+1] == "#" or ar[i-1][j] == "#":
                            continue
                        else:
                            return False
                else:
                    if j == 0:
                        if ar[i][j+1] == "#" or ar[i-1][j] == "#" or ar[i+1][j] == "#":
                            continue
                        else:
                            return False
                    elif j == w-1:
                        if ar[i][j-1] == "#" or ar[i-1][j] == "#" or ar[i+1][j] == "#":
                            continue
                        else:
                            return False
                    else:
                        if ar[i][j-1] == "#" or ar[i][j+1] == "#" or ar[i-1][j] == "#" or ar[i+1][j] == "#":
                            continue
                        else:
                            return False
    return True
if ex() == True:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
maps = [input().rstrip() for x in range(H)]
for i in range(H):
  for j in range(W):
    if maps[i][j] == '#':
      count = 0
      if 0 <= i-1 and maps[i-1][j] == '#':
        count += 1
      if i+1 < H and maps[i+1][j] == '#':
        count += 1
      if 0 <= j-1 and maps[i][j-1] == '#':
        count += 1
      if j+1 < W and maps[i][j+1] == '#':
        count += 1
      if count == 0:
        print('No')
        exit()
print('Yes')

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

# AtCoder Beginner Contest
# Problem C

if __name__ == '__main__':
    h, w = list(map(int, input().split()))
    board = [list(map(str, input())) for _ in range(h)]
    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]

    for i in range(h):
        for j in range(w):
            if board[i][j] == '#':
                count = 0

                for k in range(len(dx)):
                    x = i + dx[k]
                    y = j + dy[k]

                    if (x >= 0) and (x < h) and (y >= 0) and (y < w) and (board[x][y] == "#"):
                        count += 1

                if count == 0:
                    print('No')
                    exit()

    print('Yes')

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# 
import math
import sys
import itertools
import numpy as np

h,w = map(int, input().split())
a = [['.'] * (w+2) for i in range(h+2)]

for i in range(1,h+1):
    temp = list(input())
    for j in range(1,w+1):
        a[i][j] = temp[j-1]

#print(a)
ok =1
for i in range(1,h+1):
    for j in range(1,w+1):
        if a[i][j]== "#":
            if a[i-1][j]=="." and a[i+1][j]=="." and a[i][j-1]=="." and a[i][j+1]==".":
                ok = 0
                break
    if ok == 0:
        break

if ok == 0:
    print("No")
else:
    print("Yes")

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(100000)

H,W=list(map(int,input().split())) # R行 C列 の迷路を受け取る

canv=[]
for i in range(H):
    canv.append([])
    line_input=input() # 迷路を1行取得
    for j in range(W):
        canv[i].append(line_input[j:j+1]) # slice による部分文字列取得

dx=[1,0,-1,0]
dy=[0,1,0,-1]

def dfs(x,y):
    for i in range(4):
        canv[x][y]="."
        nx = x + dx[i]
        ny = y + dy[i]
        if 0<=nx and nx<H and 0<=ny and ny<W:
            if canv[nx][ny]=="#":
                dfs(nx,ny)

for i in range(H):
    for j in range(W):
        for k in range(4):
            nx1 = i + dx[k]
            ny1 = j + dy[k]
            if 0<=nx1 and nx1<H and 0<=ny1 and ny1<W:
                if canv[i][j]=="#" and canv[nx1][ny1]=="#":
                    dfs(i,j)
flg = True
for i in range(H):
    for j in range(W):
        if canv[i][j]=="#":
            flg = False
if flg: print("Yes")
else: print("No")

Submission status: Accepted

Solution code:
import numpy as np
import sys

h, w = map(int, sys.stdin.readline().strip().split(" "))

a = np.zeros((h,w), dtype=int)

for i in range(h):
    l = sys.stdin.readline().strip()
    for j in range(w):
        a[i, j] = (l[j] == "#")

for i in range(h):
    for j in range(w):
        if a[i, j] == 0:
            continue
        check = True
        if i >= 1:
            check &= a[i-1, j] == 0
        if i < h-1:
            check &= a[i+1, j] == 0
        if j >= 1:
            check &= a[i, j-1] == 0
        if j < w-1:
            check &= a[i, j+1] == 0
        if check:
            print("No")
            quit()
print("Yes")

Submission status: Accepted

Solution code:
H, W = map(int, input().split(' '))

grids = []
for i in range(H):
    grids.append(list(input()))

for i in range(H):
    for j in range(W):
        if grids[i][j] == '.':
            continue

        if i == 0 and j == 0:
            if grids[i][j + 1] == '.' and grids[i + 1][j] == '.':
                print("No")
                exit()

        elif i == 0 and j == W - 1:
            if grids[i][j - 1] == '.' and grids[i + 1][j] == '.':
                print("No")
                exit()

        elif i == 0:
            if grids[i][j - 1] == '.' and grids[i][j + 1] == '.' and grids[i + 1][j] == '.':
                    print("No")
                    exit()

        elif i == H - 1 and j == 0:
            if grids[i][j + 1] == '.' and grids[i - 1][j] == '.':
                print("No")
                exit()

        elif i == H - 1 and j == W - 1:
            if grids[i][j - 1] == '.' and grids[i - 1][j] == '.':
                print("No")
                exit()

        elif i == H - 1:
            if grids[i][j - 1] == '.' and grids[i][j + 1] == '.' and grids[i - 1][j] == '.':
                print("No")
                exit()

        elif j == 0:
            if grids[i - 1][j] == '.' and grids[i][j + 1] == '.' and grids[i + 1][j] == '.':
                    print("No")
                    exit()

        elif j == W - 1:
            if grids[i - 1][j] == '.' and grids[i][j - 1] == '.' and grids[i + 1][j] == '.':
                    print("No")
                    exit()

        else:
            if grids[i - 1][j] == '.' and grids[i + 1][j] == '.' and grids[i][j - 1] == '.' and grids[i][j + 1] == '.':
                    print("No")
                    exit()

print("Yes")