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:
H, W = map(int, input().split())
S = ['a' * (W+2)] + [list('a' + input() + 'a') for i in range(H)] + ['a' * (W+2)]
for i in range(H):
    for j in range(W):
        if S[i+1][j+1] == '#':
            if [S[i][j+1], S[i+1][j], S[i+1][j+2], S[i+2][j+1]].count('#') < 1:
                print('No')
                exit()
print('Yes')

Submission status: Accepted

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

for i in range(h):
    l2 = list(input())
    l1.append(l2)

is_no = 0
for i in range(h):
    if is_no:
        break
    for j in range(w):
        if is_no:
            break
        if l1[i][j] == '#':
            if i == 0:
                if j == 0:
                    if l1[i][j + 1] == '.' and l1[i + 1][j] == '.':
                        is_no = 1
                        
                elif j == w - 1:
                    if l1[i][j - 1] == '.' and l1[i + 1][j] == '.':
                     is_no = 1

                else:
                    if l1[i][j - 1] == '.'  and l1[i][j + 1] == '.' and l1[i + 1][j] == '.':
                        is_no = 1


            elif i == h - 1:
                if j == 0:
                    if l1[i][j + 1] == '.' and l1[i - 1][j] == '.':
                        is_no = 1

                elif j == w - 1:
                    if l1[i][j - 1] == '.' and l1[i - 1][j] == '.':
                        is_no = 1

                else:
                    if l1[i][j - 1] == '.'  and l1[i][j + 1] == '.' and l1[i - 1][j] == '.':
                        is_no = 1


            else:
                if j == 0:
                    if l1[i][j + 1] == '.' and l1[i - 1][j] == '.' and l1[i + 1][j] == '.':
                        is_no = 1

                elif j == w - 1:
                    if l1[i][j - 1] == '.' and l1[i - 1][j] == '.' and l1[i + 1][j] == '.':
                        is_no = 1

                else:
                    if l1[i][j - 1] == '.'  and l1[i][j + 1] == '.' and l1[i - 1][j] == '.' and l1[i + 1][j] == '.':
                        is_no = 1


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

Submission status: Accepted

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

d = {(i, j):c for i, row in enumerate(s) for j, c in enumerate(row)}

def next_to_black(i, j):
    directions = ((-1, 0), (1, 0), (0, -1), (0, 1))
    surroundings = ((i + a, j + b) for a, b in directions)
    colors = (d.get(cell, '.') for cell in surroundings)
    return any(color == '#' for color in colors)

black_coords = (coord for coord, c in d.items() if c == '#')
paintable = all(next_to_black(*coord) for coord in black_coords)
print('Yes' if paintable else 'No')

Submission status: Accepted

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

d = {(i, j):c for i, row in enumerate(s) for j, c in enumerate(row)}

surrounds = ((-1, 0), (1, 0), (0, -1), (0, 1))
def black_in_surrounds(i, j):
    return any(d.get((i+a, j+b), '.') == '#' for a, b in surrounds)

if all(black_in_surrounds(i, j) for (i, j), c in d.items() if c == '#'):
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
import sys

h, w = map(int, input().split())
canvas = [list([i for i in input()]) for j in range(h)]
for i in range(h):
    for j in range(w):
        if canvas[i][j] == '#':
            if i == 0 and j == 0:
                if canvas[0][1] == '.' and canvas[1][0] == '.':
                    print('No')
                    sys.exit()
            elif i == 0 and j == w-1:
                if canvas[0][w-2] == '.' and canvas[1][w-1] == '.':
                    print('No')
                    sys.exit()
            elif i == h-1 and j == 0:
                if canvas[h-1][1] == '.' and canvas[h-2][0] == '.':
                    print('No')
                    sys.exit()
            elif i == h-1 and j == w-1:
                if canvas[h-1][w-2] == '.' and canvas[h-2][w-1] == '.':
                    print('No')
                    sys.exit()
            elif i == 0:
                if canvas[0][j-1] == '.' and canvas[0][j+1] == '.' and canvas[1][j] == '.':
                    print('No')
                    sys.exit()
            elif j == 0:
                if canvas[i-1][0] == '.' and canvas[i+1][0] == '.' and canvas[i][1] == '.':
                    print('No')
                    sys.exit()
            elif i == h-1:
                if canvas[h-1][j-1] == '.' and canvas[h-1][j+1] == '.' and canvas[h-2][j] == '.':
                    print('No')
                    sys.exit()
            elif j == w-1:
                if canvas[i-1][w-1] == '.' and canvas[i+1][w-1] == '.' and canvas[i][w-2] == '.':
                    print('No')
                    sys.exit()
            else:
                if canvas[i][j-1] == '.' and canvas[i][j+1] == '.' and canvas[i-1][j] == '.' and canvas[i+1][j] == '.':
                    print('No')
                    sys.exit()
print('Yes')

Submission status: Accepted

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

def search_around(i, j):
    ret = False
    if 0 <= i+1 < H and 0 <= j < W:
        if s[i+1][j] == '#':
            ret = True
    if 0 <= i < H and 0 <= j+1 < W:
        if s[i][j+1] == '#':
            ret = True
    if 0 <= i-1 < H and 0 <= j < W:
        if s[i-1][j] == '#':
            ret = True
    if 0 <= i < H and 0 <= j-1 < W:
        if s[i][j-1] == '#':
            ret = True
    return ret
    
ans = True
for i in range(H):
    s.append(input())
for i in range(H):
    for j in range(W):
        if s[i][j] == '#':
            if not search_around(i, j):
                ans = False
                break
if ans:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
cam = [input() for i in range(H)]
ans = 'Yes'
for i in range(1, H-1):
	for j in range(1, W-1):
		if cam[i][j] == "#":
			if cam[i-1][j] == cam[i+1][j] == cam[i][j-1] == cam[i][j+1] == ".":
				ans = 'No'
				break
print(ans)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
a = []
tmp = []
for i in range(H):
    tmp = list(map(str,input().split()))
    a.append(list(tmp[0]))

for i in range(H):
    for j in range(W):
        if a[i][j] == '#':
            if j - 1 >= 0 and a[i][j - 1] == "#":
                continue
            elif j + 1 < W and a[i][j + 1] == "#" :
                continue
            elif i + 1 < H and a[i + 1][j] == "#" :
                continue
            elif i - 1 >= 0 and a[i - 1][j] == "#":
                continue
            else:
                print("No")
                exit()
 
print("Yes")

Submission status: Accepted

Solution code:
H ,W = [int(i) for i in input().split(' ')]
s = []
#print(s)
for i in range(H):
    line = input()
    lis = []
    for str in line:
        lis.append(str)
    s.append(lis)


def drawable(h,w):
    for i in [-1,1]:
        if 0 <= w+i < W:
#            print(h,w+i)
            if s[h][w+i] =='#':
                return True
    for i in [-1,1]:
        if 0 <= h+i < H:
            if s[h+i][w] =='#':
                return True

    return False

flag = True
for i in range(H):
    for j in range(W):
#        print(i,j)
        if s[i][j]=='#' and not(drawable(i,j)):
           flag = False 

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

Submission status: Accepted

Solution code:
import numpy
import re
import math

def main():
	"""
	テストコード実行
	:return:
	"""
	h, w = map(int, input().split())
	src = [list(input()) for i in range(h)]
	dxy = [(-1, 0), (1, 0), (0, -1), (0, 1)]  # 4近傍

	for y in range(h):
		for x in range(w):
			if src[y][x] is "#":
				for dx, dy in dxy:
					#if x + dx < 0 or x + dx > w - 1 or y + dy < 0 or y + dy > h - 1 or src[y + dy][x + dx] is "#": continue
					if 0 <= x + dx < w and 0 <= y < h and src[y + dy][x + dx] is "#": break
				else:
					print("No")
					exit()
	print("Yes")



if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
import sys

# pre process
X = [int(i) for i in input().split()]
H, W = X
grid = []
for row in range(H):
    grid.append([char for char in input()])



# solve
if H == 1 and W == 1:
    print('No')
    exit(0)
elif H == 1:
    for w in range(W):
        if grid[0][w] == "#":
            if w == 0:
                if not(grid[0][w+1] == '#'):
                    print("No")
                    exit(0)
            elif w == W-1:
                if not(grid[0][w-1] == '#'):
                    print("No")
                    exit(0)
            else:
                if not (grid[0][w - 1] == '#' or grid[0][w+1] == '#'):
                    print("No")
                    exit(0)
elif W == 1:
    for h in range(H):
        if grid[h][0] == "#":
            if h == 0:
                if not(grid[h+1][0] == '#'):
                    print("No")
                    exit(0)
            elif h == H-1:
                if not(grid[h-1][0] == '#'):
                    print("No")
                    exit(0)
            else:
                if not (grid[h-1][0] == '#' or grid[h+1][0] == '#'):
                    print("No")
                    exit(0)
else:
    for h in range(H):
        for w in range(W):
            if grid[h][w] == "#":
                # 枠の内側
                if h != 0 and h != H-1 and w != 0 and w != W-1:
                    if not(grid[h+1][w] == "#" or grid[h-1][w] == '#' or grid[h][w-1] == '#' or grid[h][w+1] == '#'):
                        print("No")
                        exit(0)
                # 枠の上ふち
                elif h == 0:
                    if w == 0:
                        if not(grid[h+1][w] == "#" or grid[h][w+1] == '#'):
                            print("No")
                            exit(0)
                    elif w == W-1:
                        if not(grid[h+1][w] == "#" or grid[h][w-1] == '#'):
                            print("No")
                            exit(0)
                    else:
                        if not (grid[h + 1][w] == "#" or grid[h][w - 1] == '#' or grid[h][w + 1] == '#'):
                            print("No")
                            exit(0)
                # 枠の下ふち
                elif h == H-1:
                    if w == 0:
                        if not (grid[h - 1][w] == "#" or grid[h][w + 1] == '#'):
                            print("No")
                            exit(0)
                    elif w == W-1:
                        if not (grid[h - 1][w] == "#" or grid[h][w - 1] == '#'):
                            print("No")
                            exit(0)
                    else:
                        if not (grid[h - 1][w] == "#" or grid[h][w - 1] == '#' or grid[h][w + 1] == '#'):
                            print("No")
                            exit(0)
                # 枠の左ふち
                elif w == 0:
                    if not (grid[h + 1][w] == "#" or grid[h-1][w] == '#' or grid[h][w + 1] == '#'):
                        print("No")
                        exit(0)
                # 枠の左ふち
                elif w == W-1:
                    if not (grid[h + 1][w] == "#" or grid[h - 1][w] == '#' or grid[h][w -1] == '#'):
                        print("No")
                        exit(0)
print('Yes')

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
C=['.'*(W+2)]+['.'+input()+'.'for _ in[0]*H]+['.'*(W+2)]
for i in range(1,H+1):
    for j in range(1,W+1):
        if C[i][j]=='#'and'#'not in[C[i-1][j], C[i][j-1], C[i][j+1], C[i+1][j]]:
            print('No')
            exit()
print('Yes')

Submission status: Accepted

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

field = []

for i in range(H):
  field.append(list(input()))
count = 0
  
for i in range(H):
  for j in range(W):
    if field[i][j]=='#':
      if j - 1 >= 0 and field[i][j - 1] == "#":
        continue
      elif j + 1 < W and field[i][j + 1] == "#" :
        continue
      elif i + 1 < H and field[i + 1][j] == "#" :
        continue
      elif i - 1 >= 0 and field[i - 1][j] == "#":
        continue
      else:
        print("No")
        exit()

print("Yes")

Submission status: Accepted

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

s = [list(map(lambda c: c == "#", input())) for _ in range(H)]
for h in range(H):
    for w in range(W):
        if s[h][w] \
                and (h == 0 or not s[h - 1][w]) \
                and (h == H - 1 or not s[h + 1][w]) \
                and (w == 0 or not s[h][w - 1]) \
                and (w == W - 1 or not s[h][w + 1]):
            print("No")
            exit()

print("Yes")

Submission status: Accepted

Solution code:
piyo = (input()).split()
H = int(piyo[0])
W = int(piyo[1])
flag = True
pic = []

kuu = [0 for i in range(W+1)]

pic += kuu

for i in range(H):
  pic = pic + [0] + list(input()) + [0]

pic += kuu

for i in range(len(pic)):
  if pic[i] == "#":
    if not(pic[i-1] == "#" or pic[i+1] == "#" or pic[i-W-2] == "#" or pic[i+W+2] == "#"):
      flag = False
      break
      

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

Submission status: Accepted

Solution code:
a =str(input()).split(" ")
h = int(a[0])
w = int(a[1])
maps= []

for i in range(h):
    maps.append(list(input()))

for i in range(h):
    for j in range(w):
        if maps[i][j] == "#":
            if j - 1 >= 0 and maps[i][j - 1] == "#":
                continue
            elif j + 1 < w and maps[i][j + 1] == "#" :
                continue
            elif i + 1 < h and maps[i + 1][j] == "#" :
                continue
            elif i - 1 >= 0 and maps[i - 1][j] == "#":
                continue
            else:
                print("No")
                exit()
        else:
            pass
print("Yes")

Submission status: Accepted

Solution code:
import numpy as np
h,w=map(int,input().split())
s=[input() for i in range(h)]
flag=False
ans="Yes"
for i in range(h):
    for j in range(w):
        if(s[i][j]=="#"):
            flag=False
            for k in[-1,1]:
                if(i+k<h and 0<=i+k and s[i+k][j]=="#"):
                    flag=True
                    break
                if(j+k<w and 0<=j+k and s[i][j+k]=="#"):
                    flag=True
                    break
            if(flag==False):
                ans="No"
                break
        
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
h,w=map(int,input().split())
s=list(input() for i in range(h))
flag=False
ans="Yes"
for i in range(h):
    for j in range(w):
        if(s[i][j]=="#"):
            flag=False
            for k in[-1,1]:
                if(i+k<h and 0<=i+k and s[i+k][j]=="#"):
                    flag=True
                    break
                if(j+k<w and 0<=j+k and s[i][j+k]=="#"):
                    flag=True
                    break
            if(flag==False):
                ans="No"
                break
        
print(ans)

Submission status: Accepted

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

ans = 'Yes'
 
for i in range(h):
  for j in range(w):
    if s[i][j] == '#':
      flag = False
      
      for k in [-1, 1]:
        if k+i>= 0 and k+i<=h-1 and s[k+i][j] == '#':
          flag = True
          break
        if k+j>= 0 and k+j<=w-1 and s[i][j+k] == "#":
          flag = True
          break
      
      if flag == False:
        ans = 'No'
        break
        
        
print(ans)

Submission status: Accepted

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

grid=[]
for i in range(h):
    grid.append(list(map(int,input().replace("#","1").replace(".","0"))))

ans="Yes"

for row in range(1,h-1):
    for col in range(1,w-1):
        if not(grid[row-1][col] or grid[row+1][col] or grid[row][col-1] or grid[row][col+1]) and grid[row][col]:
            ans="No"
            break

print(ans)

Submission status: Accepted

Solution code:
h,w = map(int, input().split(' '))
s = []
s0 = '.'*(w+2)
s.append(s0)
i = 0
while i < h:
    s0 = '.'+input()+'.'
    s.append(s0)
    i += 1
s0 = '.'*(w+2)
s.append(s0)
f0 = 0
h0 = 0
while h0 < len(s) and f0 == 0:
    slist = list(s[h0])
    indices = [i for i, x in enumerate(slist) if x == "#"]
    for ind in indices:
        if (s[h0][ind-1] == "." and s[h0][ind+1] == '.') and (s[h0-1][ind] == '.' and s[h0+1][ind] == '.'):
            f0 = 1
            break
    h0 += 1
if f0 == 0:
    print('Yes')
else:
    print('No')

Submission status: Accepted

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

ans = 'Yes'
for i in range(1, H-1):
    for j in range(1, W-1):
        if square[i][j] == '#':
            if square[i-1][j] == square[i+1][j] == square[i][j-1] == square[i][j+1] == '.':
                ans = 'No'
                break

print(ans)

Submission status: Accepted

Solution code:
H, W = list(map(int, input().split()))
s = []
s.append(['.']*(W+2))
for i in range(H):
    #二次元配列に入力を入れる
    array =['.']
    array.extend(list(input()))
    array.extend(['.'])
    s.append(array)
s.append(['.']*(W+2))
#print(s)
count = 0
for i in range(1,H+1):
    for j in range(1,W+1):
        if(s[i][j] == '#' and s[i][j-1] != '#' and s[i][j+1] != '#' and s[i-1][j] != '#' and s[i+1][j] != '#'):
            count = count+1

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

Submission status: Accepted

Solution code:
W, H = map(int,input().split())
arr = [input() for j in range(W)]
ans = 'Yes'

for i in range(0, W):
    for j in range(0, H):
        t = 0
        if arr[i][j] == '#':
            for k in (-1, 1):
                if (-1 < i+k < W):
                    if arr[i+k][j] == '#':
                        t = 1
                        break
                        
                if (-1 < j+k < W):
                    if arr[i][j+k] == '#':
                        t = 1
                        break
            if t == 0:
                ans = 'No'
                break
                
print(ans)

Submission status: Accepted

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

    for i in range(1, H + 1):
        for j in range(1, W + 1):
            if s[i][j] == '#':
                if s[i + 1][j] != '#' and\
                        s[i][j + 1] != '#' and\
                        s[i - 1][j] != '#' and\
                        s[i][j - 1] != '#':
                    print('No')
                    return 0

    print('Yes')
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

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

s=[0]*H
for i in range(H):
  s[i] = list(input())


def main():
  for i in range(H):
    for j in range(W):
      if s[i][j]=='.':
        continue
  
      flg=0
      l=j-1
      r=j+1
      t=i-1
      u=i+1
      if l>=0 and s[i][l]=='#':
        flg=1
  
      if r<W and s[i][r]=='#':
        flg=1
      
      if t>=0 and s[t][j]=='#':
        flg=1
  
      if u<H and s[u][j]=='#':
        flg=1
  
      if not flg:
        print("No")
        return
  
  print("Yes")

main()

Submission status: Accepted

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

ans=True
for i in range(h):
    for j in range(w):
        if s[i][j]=='.':
            continue
        if 0<i and s[i-1][j]=='#':
            continue
        if i<h-1 and s[i+1][j]=='#':
            continue
        if 0<j and s[i][j-1]=='#':
            continue
        if j<w-1 and s[i][j+1]=='#':
            continue
        ans=False
        break
    else:
        continue
    break
if ans:
    print("Yes")
else:
    print("No")

Submission status: Accepted

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

res = True

for j in range(1, W-1):
    for i in range(1, H-1):
        if graph[i][j] == '#' and graph[i + 1][j] == '.' and graph[i - 1][j] == '.' and graph[i][j + 1] == '.' and graph[i][j - 1] == '.':
            print('No')
            exit()
print('Yes')

Submission status: Accepted

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


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

safe=1



for i in range(W):
    for j in range(H):
        if arr[i][j]=="#":
            if i-1 >=0 and i+1 < W  and j-1>=0 and j+1<H:
                if arr[i-1][j] == "#" or arr[i+1][j] == "#" or arr[i][j-1] == "#" or arr[i][j+1] == "#":
                    safe=1
                else:
                    print("No")
                    exit()
                    
                    
print("Yes")

Submission status: Accepted

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

# 周囲に"."を追加
lst_S = [["." for i in range(W)]] + lst_S

for i in range(H + 1):
    lst_S[i] = ["."] + lst_S[i] + ["."]

lst_S = lst_S + [["." for i in range(W + 2)]]

count = 0

for i in range(1, H + 1):
    for j in range(1, W + 1):
        if lst_S[i][j] == "#" and lst_S[i][j - 1] == "." and lst_S[i][j + 1] == "." and lst_S[i - 1][j] == "." and lst_S[i + 1][j] == ".":
            print("No")
            count += 1
            break
    if count > 0:
        break

if count == 0:
    print("Yes")

Submission status: Accepted

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

for i in range(h):
    l.append(list(list(input())))
for i in range(h):
    for j in range(w):
        if(l[i][j] == "#"):
            if (i != 0):
                if(l[i-1][j] == "#"):continue
            if (j != 0):
                if (l[i][j-1] == "#"): continue
            if (i != h-1):
                if (l[i+1][j] == "#"): continue
            if (j != w-1):
                if (l[i][j+1] == "#"): continue
            print("No")
            exit()
print("Yes")

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[list(input())for i in range(h)]
dx=[1,-1,0,0]
dy=[0,0,1,-1]
for i in range(h):
    for j in range(w):
        if s[i][j]=="#":
            judge=False
            for k in range(4):
                x=i+dx[k]
                y=j+dy[k]
                if 0<=x and x<h and 0<=y and y<w and s[x][y]=="#":
                    judge=True
            if judge==False:
                print("No")
                exit()
print("Yes")

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[input() for _ in range(h)]
flag=1
nx=[1,0,-1,0]
ny=[0,1,0,-1]
for y in range(h):
  for x in range(w):
    if(s[y][x]=="#"):
      for i in range(4):
        tx,ty=x+nx[i],y+ny[i]
        if(0<=ty and ty<h and 0<=tx and tx<w):
          if(s[ty][tx]=="#"):break
      else:
        print("No")
        exit()
print("Yes")

Submission status: Accepted

Solution code:
def valid(s, r, c):
    if s[r][c] != '#': return True
    if r > 0 and s[r-1][c] == '#': return True
    if r < H-1 and s[r+1][c] == '#': return True
    if c > 0 and s[r][c-1] == '#': return True
    if c < W-1 and s[r][c+1] == '#': return True
    return False

def solve():
    for i in range(H):
        for j in range(W):
            if not valid(s, i, j): return False
    return True

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

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

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
s = [list(input()) for i in range(H)]
for i in range(H):
	for j in range(W):
		if s[i][j] == "#":
			if s[max(0,i-1)][j] == "." and s[min(H-1,i+1)][j] == "." and s[i][max(0,j-1)] == "." and s[i][min(W-1,j+1)] == ".":
			    print("No")
			    exit()
print("Yes")

Submission status: Accepted

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

line_list = []
for i in range(h):
    line_list.append(input())

if h == 1:
    if w == 1:
        print("No")
        exit()
    else:
        current_line = line_list[0]
        j = 0
        while j < w - 1:
            if current_line[j] == "#":
                if current_line[j + 1] == "#":
                    j += 1
                    while j < w - 1:
                        if current_line[j + 1] != "#":
                            break
                        else:
                            j += 1
                    j += 1
                    continue
            j += 1
    print("Yes")
    exit()
elif w == 1:
    if h == 1:
        print("No")
        exit()
    else:
        j = 0
        while j < h - 1:
            if line_list[j] == "#":
                if line_list[j + 1] == "#":
                    j += 1
                    while j < h - 1:
                        if line_list[j + 1] != "#":
                            break
                        else:
                            j += 1
                    j += 1
                    continue
            j += 1
    print("Yes")
    exit()


before_line = [["."] for _ in range(w)]
current_line = list(line_list[0])
for i in range(h-1):
    after_line = list(line_list[i + 1])
    j = 0
    while j < w-1:
        if current_line[j] == "#":
            if current_line[j+1] == "#":
                j += 1
                while j < w - 1:
                    if current_line[j + 1] != "#":
                       break
                    else:
                        j += 1
                j += 1
                continue
            elif before_line[j] != "#" and after_line[j] != "#":
                print("No")
                exit()
        j += 1
    before_line = current_line
    current_line = after_line

print("Yes")

Submission status: Accepted

Solution code:
# H * W の行列をする
H, W = map(int, input().split())
field = [list(input()) for i in range(H)] 
count = 0

for i in range(H):
    for j in range(W):
        # 注目している点が#でその上下左右が#でないときにcountが加算される
        # このcountが1以上になると、#が孤立していることになり、Noが出力される
        if (i == 0 and j == 0) or (i == H-1 and j == 0) or (i == 0 and j == W-1) or (i == H-1 and j == W-1):
            pass
        elif i == 0:
            if field[i][j] == '#' and field[i][j-1] != '#' and field[i][j+1] != '#' and field[i+1][j] != '#':
                count += 1
        elif i == H-1:
            if field[i][j] == '#' and field[i][j-1] != '#' and field[i][j+1] != '#' and field[i-1][j] != '#':
                count += 1
        elif j == 0:
            if field[i][j] == '#' and field[i][j-1] != '#' and field[i][j+1] != '#' and field[i+1][j] != '#':
                count += 1
        elif j == W-1:
            if field[i][j] == '#' and field[i][j-1] != '#' and field[i+1][j] != '#' and field[i-1][j] != '#':
                count += 1
        elif field[i][j] == '#' and field[i][j-1] != '#' and field[i][j+1] != '#' and field[i+1][j] != '#' and field[i-1][j] != '#':
            count += 1
print('No' if count>=1 else 'Yes')

Submission status: Accepted

Solution code:
#!/usr/bin/python3

h, w = map(int, input().split())
s = [input() for i in range(h)]

ns = ((0, -1), (-1, 0), (0, 1), (1, 0))
for i in range(h):
    for j in range(w):
        if s[i][j] != '#':
            continue
        for dx, dy in ns:
            nx = j + dx
            ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:
                if s[ny][nx] == '#':
                    break
        else:
            print('No')
            exit()

print('Yes')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
s = [["."] * (W + 2)]
for _ in range(H):
    s.append(["."] + list(input()) + ["."])
s.append(["."] * (W + 2))


def judge(s):
    for i in range(1, H + 1):
        for j in range(1, W + 1):
            if s[i][j] == "#":
                if s[i + 1][j] == "."and s[i][j + 1] == "."and s[i - 1][j] == "."and s[i][j - 1] == ".":
                    return "No"
    return "Yes"


print(judge(s))

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
#inputの両端に.を入れてリストに格納
ls = [[i for i in '.'+input()+'.'] for i in range(h)]
#上段を埋める
ls.insert(0, ['.' for i in range(w+2)])
#下段を埋める
ls.append(['.' for i in range(w+2)])

ans = 'Yes'
for i in range(1, h+1):
    for j in range(1, w+1):
        if ls[i][j] == '#' and '#' not in [ls[i][j-1], ls[i][j+1], ls[i-1][j], ls[i+1][j]]:
            ans = 'No'
            
print(ans)

Submission status: Accepted

Solution code:
import itertools

h, w = map(int, input().split())

s = [0] * h
for i in range(h):
    s[i] = list(input())

ans = 'Yes'
for i, j in itertools.product(range(h), range(w)):
    if s[i][j] == '.':
        continue
    c = False
    for k, l in [(-1, 0), (0, -1), (1, 0), (0, 1)]:
        in_range = i + k >= 0 and j + l >= 0 and i + k < h and j + l < w
        if in_range and s[i + k][j + l] == '#':
            c = True
            break
    if not c:
        ans = 'No'
        break
print(ans)

Submission status: Accepted

Solution code:
import sys
H,W = map(int,input().split())
temp =[0 for i in range(H)]
for i in range(H):
    temp[i] = input()
dx = [-1,1,0,0]
dy = [0,0,1,-1]
ans = True
for j in range(H):
    for k in range(W):
        if(temp[j][k]=="#"):
            judge =False
            for i in range(4):
                j2 = j+dx[i]
                k2 = k+dy[i]
                if 0 <= j2 < H and 0<= k2 < W and temp[j2][k2] =="#":
                    judge = True
            if not judge:
                ans =False
if ans:
    print("Yes")
else:
    print("No")