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())
list_S = [ list(input()) for _ in range(h) ]
ans = "Yes"
a = 0
for i in range(h):
    for j in range(w):
        cnt = 0
        if list_S[i][j] == "#":

            if i + 1 >= 0 and i + 1< h and j >= 0 and j < w:
                if list_S[i+1][j] == "#":
                    cnt += 1

            if i - 1 >= 0 and i - 1< h and j >= 0 and j < w:
                if list_S[i-1][j] == "#":
                    cnt += 1

            if i >= 0 and i < h and j + 1 >= 0 and j + 1 < w:
                if list_S[i][j+1] == "#":
                    cnt += 1

            if i >= 0 and i < h and j - 1>= 0 and j - 1 < w:
                if list_S[i][j-1] == "#":
                    cnt += 1
            if cnt == 0:
                ans = "No"

print(ans)

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
s = [list(input()) for _ in range(h)]
def check(x,y):
  if s[x][y] == '.':
    return 1
  else:
    return 0
def check_ans(i,j):
  if s[i][j] == '.':
    pass
  else:
    cnt = 0
    cnt2 = 0
    if i - 1 >= 0:
      cnt += check(i-1,j)
      cnt2 += 1
    if i + 1 <= h-1:
      cnt += check(i+1,j)
      cnt2 += 1
    if j - 1 >= 0:
      cnt += check(i,j-1)
      cnt2 += 1
    if j + 1 <= w-1:
      cnt += check(i,j+1)
      cnt2 += 1
    if cnt == cnt2:
      print('No')
      exit()
    return 0

for i in range(h):
  for j in range(w):
    if check_ans(i,j) == 0:
      pass
print('Yes')

Submission status: Accepted

Solution code:
def main():
    h, w = [int(x) for x in input().split()]
    matrix = [list(input()) for _ in range(h)]
    for i in range(h):
        for j in range(w):
            if matrix[i][j] == '.':
                continue
            # Above
            if i > 0:
                if matrix[i - 1][j] == '#':
                    continue
            # Left
            if j > 0:
                if matrix[i][j - 1] == '#':
                    continue
            # Right
            if j < w - 1:
                if matrix[i][j + 1] == '#':
                    continue
            # Below:
            if i < h - 1:
                if matrix[i + 1][j] == '#':
                    continue

            # The cell is black and no adjacent black cell.
            return 'No'

    return 'Yes'


if __name__ == '__main__':
    print(main())

Submission status: Accepted

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

ans = 'Yes'
for h in range(1, H+1):
  for w in range(1, W+1):
    if s[h][w] == '#':
      if '#' not in [s[h-1][w], s[h+1][w], s[h][w-1], s[h][w+1]]:
        ans = 'No'
        break

print(ans)

Submission status: Accepted

Solution code:
import math
import string
import collections
from collections import Counter
from collections import deque


def readints():
    return list(map(int, input().split()))


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor


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

H, W = map(int, input().split())
s = [None]*H
for i in range(H):
    s[i] = input()
    # print(s[i])
for i in range(H):
    for j in range(W):
        if s[i][j] == '#':
            #print(i, j)
            if (i != 0 and s[i-1][j] == '#') or(i != H-1 and s[i+1][j] == '#')or(j != 0 and s[i][j-1] == '#') or(j != W-1 and s[i][j+1] == '#'):
                continue
            else:
                print('No')
                exit()
print('Yes')

Submission status: Accepted

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

for i in range(H):
  h=i
  for j in range(W):
    if s[h][j]=='#':
      w=j
      cnt=0
      for k,l in ([1,0], [-1,0], [0,1], [0,-1]):
        new_h, new_w=h+k, w+l
        if new_h<0 or new_h>=H or new_w<0 or new_w>=W:
          continue
        elif s[new_h][new_w] == '#':
          cnt+=1
          break
      if cnt==0:
        print('No')
        exit()
print('Yes')

Submission status: Accepted

Solution code:
def main():
    h, w = map(int, input().split())
    k = [["."]+list(input())+["."] for _ in range(h)]
    s = [[None]*(w+2) for _ in range(h+2)]
    s[0] = ["."]*(w+2)
    s[-1] = ["."]*(w+2)
    s[1:h+1] = k
    d = [[0, 1], [0, -1], [1, 0], [-1, 0]]
    for i in range(1, h+1):
        for j in range(1, w+1):
            if s[i][j] == ".":
                continue
            flag = True
            for (di, dj) in d:
                if s[i+di][j+dj] == "#":
                    flag = False
                    break
            if flag:
                print("No")
                return
    print("Yes")

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:

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

mass = []
for i in range(h):
    s = input()
    s += "."
    mass.append(s)
mass.append("." * (w + 1))
for i in range(h):
    for j in range(w):
        if mass[i][j] == "#":
            if mass[i][j + 1] != "#" and mass[i][j - 1] != "#" and mass[i + 1][j] != "#" and mass[i - 1][j] != "#":
                print("No")
                exit()
print("Yes")

Submission status: Accepted

Solution code:

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

s = list(input() for _ in range(h))
flag = True
for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            if i == 0:
                if j == 0 :
                    if s[i][j + 1] != "#" and s[i + 1][j] != "#":
                        flag = False
                        break
                elif j == w - 1:
                    if s[i][j - 1] != "#" and s[i + 1][j] != "#":
                        flag = False
                        break
                else:
                    if s[i][j + 1] != "#" and s[i][j - 1] != "#" and s[i + 1][j] != "#":
                        flag = False
                        break
            if i == h - 1:
                if j == 0:
                    if s[i][j + 1] != "#" and s[i - 1][j] != "#":
                        flag = False
                        break
                elif j == w - 1:
                    if s[i][j - 1] != "#" and s[i - 1][j] != "#":
                        flag = False
                        break
            else:
                if j == 0:
                    if s[i][j + 1] != "#" and s[i + 1][j] != "#" and s[i - 1][j] != "#":
                        flag = False
                        break
                elif j == w - 1:
                    if s[i][j - 1] != "#" and s[i + 1][j] != "#" and s[i - 1][j] != "#":
                        flag = False
                        break
                else:
                    if s[i][j + 1] != "#" and s[i][j - 1] != "#" and s[i + 1][j] != "#" and s[i - 1][j] != "#":
                        flag = False
                        break

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

Submission status: Accepted

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

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

not_ok=0
for y in range(H):
    for x in range(W):
        if s[y][x] == '#':
            ok=0
            for i in range(4):
                if 0 <= x+dx[i]<W and 0 <=y+dy[i]<H:
                    if s[y+dy[i]][x+dx[i]] == '#':
                        ok=1
            if ok!=1:
                not_ok=1
if not_ok==1:
    print('No')
else:
    print('Yes')

Submission status: Accepted

Solution code:
def check(s,i,j,h,w):
    if i > 0 and s[i-1][j] == '#':
        return True
    elif i < h-1 and s[i+1][j] == '#':
        return True
    elif j > 0 and s[i][j-1] == '#':
        return True
    elif j < w-1 and s[i][j+1] == '#':
        return True
    else:
        return False


h,w = map(int,input().split())
s = [input() for i in range(h)]
ans = 'Yes'
for i in range(h):
    for j in range(w):
        if s[i][j] == '#' and not check(s,i,j,h,w):
            ans = 'No'
            break
print(ans)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
grid = [list(input()) for _ in range(H)]
for h in range(H):
    for w in range(W):
        if grid[h][w] == "#":
            for dh, dw in ((1, 0), (-1, 0), (0, 1), (0, -1)):
                next_h, next_w = h + dh, w + dw
                if -1 < next_h < H and -1 < next_w < W:
                    if grid[next_h][next_w] == "#":
                        break
            else:
                print("No")
                exit()
else:
    print("Yes")

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[min(i + 1, h - 1)][j] != "#" and s[i][min(j + 1, w - 1)] != "#" and s[i - 1][j] != "#" and s[i][j - 1] != "#":
                print("No")

                exit()

print("Yes")

Submission status: Accepted

Solution code:
import sys

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

masu = [list(map(str, input())) for i in range(h)]

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

flag = True
for i in range(h):
    for j in range(w):
        if masu[i][j] == '.':
            continue
        cnt = 0
        for k in range(4):
            ni = i + dy[k]
            nj = j + dx[k]

            if ni < 0 or ni >= h:
                continue
            if nj < 0 or nj >= w:
                continue
            if masu[ni][nj] == '#':
                cnt += 1

        if cnt == 0:
            flag = False


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

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
L = []
for i in range(H):
    L.append((input()))
for i in range(H):
    for j in range(W):
        if L[i][j] == "#":
            flag = False
            if i-1 >=0 and L[i-1][j] == "#":
                flag = True
            if i+1 <H and L[i+1][j] == "#":
                flag = True
            if j-1 >=0 and L[i][j-1] == "#":
                flag = True
            if j+1 <W and L[i][j+1] == "#":
                flag = True
            if flag == False:
                print("No")
                exit()
print("Yes")

Submission status: Accepted

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

SS = []
for i in range(H):
    SS.append(list(input()))
    
def seekindices(h,w,H,W):
    cand = []
    for x in [-1,0,1]:
        for y in [-1,0,1]:
            if abs(x) == 1:
                if h + x >= 0 and h + x < H:
                    cand.append([h+x,w])
            elif abs(y) == 1:
                if w + y >=0 and w + y < W:
                    cand.append([h,w+y])
    return(cand)

ans = 'Yes'

for h in range(H):
    for w in range(W):
        if SS[h][w] == "#":
            seeks = seekindices(h,w,H,W)
            bochi = 0
            for s in seeks:
                i,j = s[:]
                if SS[i][j] == "#":
                    bochi = 1
            if bochi == 0:
                ans = 'No'
            
print(ans)

Submission status: Accepted

Solution code:
from itertools import product
n, m = map(int,input().split())
canvas = []
flag = False
for i in range(n):
    canvas.append(input())
for i,j in product([num for num in range(n)],[num for num in range(m)]):
    if canvas[i][j] == '.':
        continue
    result = 0
    if i > 0 and canvas[i-1][j] == '#':
        result += 1
    if i < n-1 and canvas[i+1][j] == '#':
        result += 1
    if j > 0 and canvas[i][j-1] == '#':
        result += 1
    if j < m-1 and canvas[i][j+1] == '#':
        result += 1
    if result == 0:
        flag = True
        break
if flag:
    print('No')
else:
    print('Yes')

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
s = [list(input()) for i in range(h)]
s2 = [[0]*(w+2) for i in range(h+2)]
for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            s2[i+1][j+1] = 1
for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            if s2[i+1][j] + s2[i+1][j+2] + s2[i][j+1] + s2[i+2][j+1] == 0:
                print('No')
                exit()
print('Yes')

Submission status: Accepted

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

for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            flg = False
            for x, y in d:
                if 0 <= x + j < w and 0 <= y + i < h:
                    if s[y+i][x+j] == "#":
                        flg = True
                        break
            else:
                if not flg:
                    print("No")
                    exit()

print("Yes")

Submission status: Accepted

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

for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            flg = False
            for x, y in d:
                if 0 <= x + j < w and 0 <= y + i < h:
                    if s[y+i][x+j] == "#":
                        flg = True
                        continue
            else:
                if not flg:
                    print("No")
                    exit()

print("Yes")

Submission status: Accepted

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

ans = "Yes"
for i, m in enumerate(s):
    for j, n in enumerate(m):
        if n == "#":
            flg = 0
            for x, y in t:
                if 0 <= x + i < h and 0 <= y + j < w:
                    if s[x+i][y+j] == "#":
                        flg = 1
            if flg == 0:
                ans = "No"

print(ans)

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [list(input()) for i in range(h)]
S1 = [[0] * (w+2) for i in range(h+2)]
for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            S1[i+1][j+1] = 1
S2 = [[0] * w for i in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            S2[i][j] = S1[i][j+1] + sum(S1[i+1][j:j+3]) + S1[i+2][j+1]
for i in range(h):
    if 1 in S2[i]:
        print("No")
        exit()
print("Yes")

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 i > 0 and s[i-1][j] == "#":
                continue
            elif i < H-1 and s[i+1][j] == "#":
                continue
            elif j > 0 and s[i][j-1] == "#":
                continue
            elif j < W - 1 and s[i][j+1] == "#":
                continue

            else:
                print("No")
                exit(0)

print("Yes")

Submission status: Accepted

Solution code:
h, w = list(map(int, input().split()))
s = ['.'*(w+2)]

for i in range(h):
	s.append('.'+input()+'.')
s.append('.'*(w+2))

ans = True

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+1][j]=='.' and s[i][j-1]=='.' and s[i][j+1]=='.':
				ans = False

print('Yes' if ans else '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] == '#':
            direction = []
            if i > 0:
                direction.append((-1, 0))
            if i < H-1:
                direction.append((1, 0))
            if j > 0:
                direction.append((0, -1))               
            if j < W-1:
                direction.append((0, 1))
            for d in direction:
                if s[i+d[0]][j+d[1]] != '.':
                    break
            else:
                print('No')
                exit()
else:
    print('Yes')

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
a=[list(input()) for i in range(h)]
dx=((0,1),(0,-1),(1,0),(-1,0))
flag=True
for i in range(h):
    for j in range(w):
        cnt=0
        if a[i][j]=="#":
            for y,x in dx:
                if 0<=i+y<=h-1 and 0<=j+x<=w-1 and a[i+y][j+x]=="#":
                    cnt+=1
            if cnt==0:
                flag=False
print("Yes" if flag else "No")

Submission status: Accepted

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

for i in range(1, H + 1):
    for j in range(1, W + 1):
        ansf = True
        if s[i][j] == '#':
            if s[i][j + 1] == '.' and s[i][j - 1] == '.' and\
                s[i - 1][j] == '.' and s[i + 1][j] == '.':
                  ansf = False
                  break
    if not ansf:
        print('No')
        exit()

print('Yes')

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
a = [list(input()) for _ in range(h)]
dx = [1,0,-1,0]
dy = [0,1,0,-1]
for k in range(h):
    for j in range(w):
        x,y = k,j
        if a[x][y] == "#":
            for i in range(4):
                xi,yi = x+dx[i],y+dy[i]
                if 0 <= xi < h and 0 <= yi < w and a[xi][yi] == "#":
                    break
            else:
                print("No")
                exit()

print("Yes")

Submission status: Accepted

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

for h in range(H):
    for w in range(W):
        if d[h][w] == '.':
            continue
        if w + 1 < W:
            if d[h][w + 1] == '#':
                continue
        if h + 1 < H:
            if d[h + 1][w] == '#':
                continue
        if -1 < w - 1:
            if d[h][w - 1] == '#':
                continue
        if -1 < h -1:
            if d[h - 1][w] == '#':
                continue
        print('No')
        exit()
print('Yes')

Submission status: Accepted

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

def isOK():
    for h in range(H):
        for w in range(W):
            if A[h][w] == '.':
                continue
            if all([A[h + dh][w + dw] == '.' for dh, dw in [(0, 1), (0, -1), (1, 0), (-1, 0)] if (0 <= h + dh < H and 0 <= w + dw < W)]):
                return False
    return True

print('Yes' if isOK() else 'No')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
h, w = list(map(int, input().split()))
s = ['.'*(w+2)]

for i in range(h):
	s.append('.'+input()+'.')
s.append('.'*(w+2))

ans = True

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+1][j]=='.' and s[i][j-1]=='.' and s[i][j+1]=='.':
				ans = False

print('Yes' if ans else 'No')

Submission status: Accepted

Solution code:
H, W = map(int,input().split())
src = [list(input()) for i in range(H)]
 
dxy = [(-1,0),(0,-1),(0,1),(1,0)]

for h in range(H):
  count = 0
  for w in range(W):
    count = 0
    if src[h][w] == ".":
      continue
    
    for dx,dy in dxy:      
      count += 1
      if w + dx < 0 or w + dx > W-1 or h + dy < 0 or h + dy > H-1:
        continue
      
      if src[h + dy][w + dx] == "#":
        break
      
      if count == 4:
        print("No")
        exit()
        
print("Yes")

Submission status: Accepted

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

    for h in range(1, H+1):
        for w in range(1, W+1):
            if S[h][w] == '#':
                if S[h-1][w] == '.' and S[h+1][w] == '.' and S[h][w+1] == '.' and S[h][w-1] == '.':
                    return False
    return True


print('Yes' if main() else 'No')

Submission status: Accepted

Solution code:
import sys

# import bisect
# from collections import Counter, deque, defaultdict
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
# import itertools
# from operator import attrgetter, itemgetter

# import math

# import numpy as np

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    h, w = list(map(int, readline().split()))
    s = [input() for _ in range(h)]

    possible = False

    for row in range(h):
        for col in range(w):
            if s[row][col] == "#":
                possible = False
                for i in [-1, 1]:
                    nr = row + i
                    nc = col + i
                    if 0 <= nr < h and s[nr][col] == "#":
                        possible = True
                    if 0 <= nc < w and s[row][nc] == "#":
                        possible = True
                if not possible:
                    print('No')
                    sys.exit()

    print('Yes')


if __name__ == '__main__':
    main()

Submission status: Accepted

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

for i in range(H):
    L[i] = list(input())

for i in range(H):
    for j in range(W):

        if L[i][j] == "#":

            c = 0
            if i-1 >= 0:
                if L[i-1][j] == "#":
                    c += 1
            if i+1 <= H-1:
                if L[i+1][j] == "#":
                    c += 1
            if j-1 >= 0:
                if L[i][j-1] == "#":
                    c += 1
            if j+1 <= W-1:
                if L[i][j+1] == "#":
                    c += 1

            if c == 0:
                print("No")
                break
    
    else:
        continue
    break
else:
    print("Yes")

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[ input() for _ in range(h)]
#print(s)
for i in range(h):
    for j in range(w):
        def valid(a,b):
            if a>=0 and a<h and b>=0 and b<w:
                return True
            return False
        if s[i][j]=='#':
            flag=False
            if valid(i+1,j):
                if s[i+1][j]=='#':
                    flag=True
            if valid(i-1,j):
                if s[i-1][j]=='#':
                    flag=True
            if valid(i,j-1):
                if s[i][j-1]=='#':
                    flag=True
            if valid(i,j+1):
                if s[i][j+1]=='#':
                    flag=True
            if not flag:
                print('No')
                import sys
                sys.exit()
print('Yes')

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
map = ["#"*(w+2)]

for i in range(h):
    lst = input()
    lst = "#"+lst+"#"
    map.append(lst)
map.append("#"*(w+2))
# print(map)

for i in range(1,h+1):
    for j in range(1, w+1):
        if map[i][j]=="#":
            if map[i-1][j]=="." and map[i][j-1]=="." and map[i+1][j]=="." and map[i][j+1]==".":
                print("No")
                exit()

print("Yes")

Submission status: Accepted

Solution code:
def check_paint(s,h,w):
    
    List = [s[h-1][w]
            ,s[h][w-1]
            ,s[h][w]
            ,s[h][w+1]
            ,s[h+1][w]]
    
    count = 0
    
    for i in range(5):
        if List[i]=="#":
            count+=1
            
    return count
            
H,W = map(int,input().split(" "))
Paint = [""]*(H+2)
#上下の場外
Paint[0] = "0"*(W+2)
Paint[-1] = "0"*(W+2)
for i in range(1,H+1):
    Paint[i] = "0"+input()+"0"

flag = 0

for i in range(1,H+1):
    for j in range(1,W+1):
        # 空白箇所は無視
        if Paint[i][j]==".":
            continue
        else:
            c = check_paint(Paint,i,j)
            # 自分を含め上下左右で'#'が2つ以上あれば塗れる
            if c<2:
                # 塗れない場合はその時点で終了
                print("No")
                flag=1
                break
    if flag==1:
        break
        
if flag==0:
    print("Yes")

Submission status: Accepted

Solution code:
#template
def inputlist(): return [int(j) for j in input().split()]
from collections import Counter
#template
#issueから始める
H,W = inputlist()
pic = ['8']*H
for i in range(H):
    pic[i] = list(input())
dx = [1,0,-1,0]
dy = [0,1,0,-1]
ans = "Yes"
for x in range(H):
    for y in range(W):
        if pic[x][y] == "#":
            count = 0
            for i in range(4):
                nx = x+dx[i]
                ny = y+dy[i]
                if nx < 0 or ny <0 or nx >=H or ny >= W:
                    continue
                if pic[nx][ny] == "#":
                    count+=1
            if count == 0:
                ans = "No"
                print(ans)
                exit()
print(ans)

Submission status: Accepted

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

for h in range(H):
  for w in range(W):
    if s[h][w] == '#':
      count = 0
      for i,j in [(0,1),(1,0),(-1,0),(0,-1)]:
        if s[h+i][w+j] == '#':
          count += 1
          continue
      if count == 0:
        print("No")
        exit()
    else:
      pass

print("Yes")

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [input() for i in range(h)]
for i in range(h):
    for j in range(w):
        check = 0
        if s[i][j] == '#':
            if i != 0:
                if s[i - 1][j] == '#':
                    check += 1
            if j != 0:
                if s[i][j - 1] == '#':
                    check += 1
            if i != (h - 1):
                if s[i + 1][j] == '#':
                    check += 1
            if j != (w - 1):
                if s[i][j + 1] == '#':
                    check += 1
            if check == 0:
                print('No')
                exit()
print('Yes')

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
S = [ [s for s in input()] for _ in range(h) ]
flag = True
for y in range(h):
    for x in range(w):
        if S[y][x] == '#':
            flag = False
            if y > 0 and S[y-1][x] == '#': flag = True
            if y < h-1 and S[y+1][x] == '#': flag = True
            if x > 0 and S[y][x-1] == '#': flag = True
            if x < w-1 and S[y][x+1] == '#': flag = True
        if flag == False:
            print('No')
            exit()
print('Yes')

Submission status: Accepted

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

black = []
for i in range(H):
    for j in range(W):
        if maze[i][j] == '#':
            black.append((i, j))

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
flag = [False] * len(black)
index = 0
for y, x in black:
    index += 1
    for i, j in zip(dx, dy):
        nx = x + i
        ny = y + j
        if 0 <= nx < W and 0 <= ny < H:
            if maze[ny][nx] == '#':
                flag[index - 1] = True
                #continue
        #else:
            #continue
        
if all(flag):
    print('Yes')
else:
    print('No')

Submission status: Accepted

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

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

for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            flag = False
            for k in range(4):
                y = i + dy[k]
                x = j + dx[k]
                if 0 <= y < h and 0 <= x < w:
                    if s[y][x] == "#":
                        flag = True
            if flag == False:
                print("No")
                exit()
print("Yes")

Submission status: Accepted

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

ans = 'Yes'

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')
                exit()

print(ans)

Submission status: Accepted

Solution code:
from collections import *
from heapq import *
from itertools import *
from fractions import gcd
import sys
from decimal import *
import copy
input=lambda :sys.stdin.readline().rstrip()

H,W=map(int,input().split())
S=[input() for i in range(H)]
for h in range(H):
    for w in range(W):
        if S[h][w]=="#":
            for y,x in [[1,0],[-1,0],[0,1],[0,-1]]:
                Y=h+y;X=w+x
                if 0<=Y<H and 0<=X<W:
                    if S[Y][X]=="#":
                        break
            else:
                print("No")
                exit()
print("Yes")

Submission status: Accepted

Solution code:
h, w  = map(int, input().split())
s = [['_'] * w for _ in range(h)]
for i in range(h):
    arr = list(str(input()))
    for j in range(w):
        s[i][j] = arr[j]

flag = True
for i in range(1,h-1):
    for j in range(1,w-1):
        if s[i][j] == '#' and s[i-1][j] == '.' and s[i][j-1] == '.' and s[i+1][j] == '.' and s[i][j+1] == '.':
            flag = False

if s[0][0] == '#' and s[1][0] == '.' and s[0][1] == '.':
    flag = False
if s[-1][-1] == '#' and s[-2][-1] == '.' and s[-1][-2] == '.':
    flag = False
if s[0][-1] == '#' and s[1][-1] == '.' and s[0][-2] == '.':
    flag = False
if s[-1][0] == '#' and s[-2][0] == '.' and s[-1][1] == '.':
    flag = False
    
for i in range(1,h-1):
    if s[i][0] == '#' and s[i-1][0] == '.' and s[i+1][0] == '.' and s[i][1] == '.':
        flag = False
    if s[i][-1] == '#' and s[i-1][-1] == '.' and s[i+1][-1] == '.' and s[i][-2] == '.':
        flag = False

for i in range(1,w-1):
    if s[0][i] == '#' and s[0][i-1] == '.' and s[0][i+1] == '.' and s[1][i] == '.':
        flag = False
    if s[-1][i] == '#' and s[-1][i-1] == '.' and s[-1][i+1] == '.' and s[-2][i] == '.':
        flag = False        
    

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