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 = [list(str(input())) for i in range(H)]
judge=True
for i in range(H):
    for j in range(W):
        if S[i][j]=='#':
            judge1=True
            judge2=True
            judge3=True
            judge4=True
            
            try:S[i][j-1]
            except IndexError:judge1=False
            else:
                if S[i][j-1]=='.':
                    judge1=False
                    
            try:S[i][j+1]
            except IndexError:judge2=False
            else:
                if S[i][j+1]=='.':
                    judge2=False
                    
            try:S[i+1][j]
            except IndexError:judge3=False
            else:
                if S[i+1][j]=='.':
                    judge3=False
                    
            try:S[i-1][j]
            except IndexError:judge4=False
            else:
                if S[i-1][j]=='.':
                    judge4=False

            if judge1==False and judge2==False and judge3==False and judge4==False:
                judge=False
                break

    if judge==False:break


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

Submission status: Accepted

Solution code:
#functions for getting rows and columns
def getRow(graph, index):
  return graph[index]
def getElem(row, index):
  return row[index]
def getAbove(graph, h, w):
  try:
    return getElem(getRow(graph, h-1), w)
  except:
    return "#"
def getBelow(graph, h, w):
  try:
    return getElem(getRow(graph, h+1), w)
  except:
    return "#"
def getRight(graph, h, w):
  try:
    return getElem(getRow(graph, h), w+1)
  except:
    return "#"
def getLeft(graph, h, w):
  try:
    return getElem(getRow(graph, h), w-1)
  except:
    return "#"

#getting input
h, w = [int(x) for x in input().split()]
graph = []
for hh in range(h):
  graph.append(input())


#processing
pos = "Yes"
for height in range(h):
  if pos == "No":
    break
  for width in range(w):
    element = getElem(getRow(graph, height), width)
    if element == ".":
      continue
    else:
      if getAbove(graph, height, width) == "#":
        continue
      elif getBelow(graph, height, width) == "#":
        continue
      elif getRight(graph, height, width) == "#":
        continue
      elif getLeft(graph, height, width) == "#":
        continue
      else:
        pos = "No"
        break


print(pos)

Submission status: Accepted

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

sll = []

for _ in range(h):
    sl = list(input())
    sll.append(sl)

for i in range(h):
    for j in range(w):
        if sll[i][j] == "#":
            flag = False
            if j+1 < w:
                if sll[i][j+1] == "#":
                    flag = True
            if j-1 >= 0:
                if sll[i][j-1] == "#":
                    flag = True
            if i+1 < h:
                if sll[i+1][j] == "#":
                    flag = True
            if i-1 >= 0:
                if sll[i-1][j] == "#":
                    flag = True
            if flag == False:
                print("No")
                import sys
                sys.exit()

print("Yes")

Submission status: Accepted

Solution code:
hw = [int(i) for i in input().split()]
h = hw[0]
w = hw[1]
rows = []
possible = True
for i in range(h):
  rows.append(input())
i = 0
if h==1 and w==1:
  if rows[0]=='#':
    possible = False
elif h==1 and w>1:
  i = 0
  while i<w:
    if rows[0][i]=='#':
      if i==0:
        if rows[0][i+1]=='.':
          possible = False
          break
      elif i==w-1:
        if rows[0][i-1]=='.':
          possible = False
          break
      else:
        if rows[0][i+1]=='.' and rows[0][i-1]=='.':
          possible = False
          break
    i+=1
elif h>1 and w==1:
  i = 0
  while i<h:
    if rows[i][0]=='#':
      if i==0:
        if rows[i+1][0]=='.':
          possible = False
          break
      elif i==h-1:
        if rows[i-1][0]=='.':
          possible = False
          break
      else:
        if rows[i+1][0]=='.' and rows[i-1][0]=='.':
          possible = False
          break
    i+=1
else:
  while i<h:
    j = 0
    while j<w:
      if rows[i][j]=='#':
        if i==0 and j==0:
          if rows[i][j+1]=='.' and rows[i+1][j]=='.':
            possible=False
            break
        elif i==0 and j==w-1:
          if rows[i+1][j]=='.' and rows[i][j-1]=='.':
            possible = False
            break
        elif i==h-1 and j==0:
          if rows[i-1][j]=='.' and rows[i][j+1]=='.':
            possible = False
            break
        elif i==h-1 and j==w-1:
          if rows[i-1][j]=='.' and rows[i][j-1]=='.':
            possible = False
            break
        elif i==0 and j>0:
          if rows[i][j+1]=='.' and rows[i][j-1]=='.' and rows[i+1][j]=='.':
            possible=False
            break
        elif i>0 and j==0:
          if rows[i+1][j]=='.' and rows[i-1][j]=='.' and rows[i][j+1]=='.':
            possible = False
            break
        elif i>0 and j==w-1:
          if rows[i+1][j]=='.' and rows[i-1][j]=='.' and rows[i][j-1]=='.':
            possible = False
            break
        elif i==h-1 and j>0:
          if rows[i][j+1]=='.' and rows[i-1][j]=='.' and rows[i][j-1]=='.':
            possible = False
            break
        else:
          if rows[i+1][j]=='.' and rows[i-1][j]=='.' and rows[i][j+1]=='.' and rows[i][j-1]=='.':
            possible = False
            break
      j+=1
    if possible==False:
      break
    i+=1
if possible==False:
  print("No")
else:
  print("Yes")

Submission status: Accepted

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

flag=0

for k in range(1,H+1):
    for j in range(1,W+1):
        if field[k][j]=='#' and field[k-1][j]!='#' and field[k+1][j]!='#' and field[k][j-1]!='#' and field[k][j+1]!='#':
            flag=1

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

Submission status: Accepted

Solution code:
import sys
h, w = map(int, input().split(" "))
p = []
for i in range(h):
  l = []
  s = input()
  for j in range(w):
    l.append(s[j])
  p.append(l)
  
         
for i in range(h):
  for j in range(w):
    if p[i][j] == "#":
      try:
        if p[i-1][j] == "#":
          continue
      except IndexError:
        pass
      try:
        if p[i][j+1] == "#":
          continue
      except IndexError:
        pass
      try:
        if p[i+1][j] == "#":
          continue
      except IndexError:
        pass
      try:
        if p[i][j-1] == "#":
          continue
      except IndexError:
        pass
      print("No")
      sys.exit()
      
print("Yes")

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
dx=[-1,1,0,0]
dy=[0,0,-1,1]
a=[]
for i in range(h):
    a.append(list(input()))
for i in range(h):
    for j in range(w):
        if a[i][j]=="#":
            frag=False
            if j-1>=0 and a[i][j-1]=="#":
                frag=True
            if j+1<=w-1 and a[i][j+1]=="#":
                frag=True
            if i-1>=0 and a[i-1][j]=="#":
                frag=True
            if i+1<=h-1 and a[i+1][j]=="#":
                frag=True
            if frag==False:
                print("No")
                exit()
print("Yes")

Submission status: Accepted

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

check = [(-1, 0), (0, -1), (1, 0), (0, 1)]
for j in range(h):
  for i in range(w):
    if grid[j][i] == "#":
      temp = [(i+cx, j+cy) for cx, cy in check if (0<= i+cx <w and 0<= j+cy <h)]
      count = 0
      for t_cx, t_cy in temp:
        if grid[t_cy][t_cx] == "#":
          count += 1
      if count == 0:
        print("No")
        exit()
print("Yes")

Submission status: Accepted

Solution code:
y,x=map(int,input().split())
s=[]
ans='Yes'
for i in range(y):
    s.append(list(input()))
for j in range(y):
    for k in range(x):
        if s[j][k]=='#':
            if j==y-1:
                if k==x-1:
                    if s[j][k]!=s[j-1][k] and s[j][k]!=s[j][k-1]:
                        ans='No'
                        break
                elif k==0:
                    if s[j][k]!=s[j-1][k] and s[j][k]!=s[j][k+1]:
                        ans='No'
                        break
                else:
                    if s[j][k]!=s[j-1][k] and s[j][k]!=s[j][k+1] and s[j][k]!=s[j][k-1]:
                        ans='No'
                        break
            elif j==0:
                if k==x-1:
                    if s[j][k]!=s[j+1][k] and s[j][k]!=s[j][k-1]:
                        ans='No'
                        break
                elif k==0:
                    if s[j][k]!=s[j+1][k] and s[j][k]!=s[j][k+1]:
                        ans='No'
                        break
                else:
                    if s[j][k]!=s[j+1][k] and s[j][k]!=s[j][k+1] and s[j][k]!=s[j][k-1]:
                        ans='No'
                        break
            else:
                if k==x-1:
                    if s[j][k]!=s[j+1][k] and s[j][k]!=s[j][k-1] and s[j][k]!=s[j-1][k]:
                        ans='No'
                        break
                elif k==0:
                    if s[j][k]!=s[j+1][k] and s[j][k]!=s[j][k+1] and s[j][k]!=s[j-1][k]:
                        ans='No'
                        break
                else:
                    if s[j][k]!=s[j+1][k] and s[j][k]!=s[j][k+1] and s[j][k]!=s[j][k-1] and s[j][k]!=s[j-1][k]:
                        ans='No'
                        break
print(ans)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

H, W = MAP()
s = [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 s[i][j] == '#':
			for sx, sy in zip(dx, dy):
				y = i+sy
				x = j+sx
				if 0 <= y <= H-1 and 0 <= x <= W-1:
					if s[y][x] == '#':
						break
			else:
				print("No")
				sys.exit()
else:
	print("Yes")

Submission status: Accepted

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

Submission status: Accepted

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

for h in range(1, H+1):
    for w in range(1,W+1):
        if M[h][w]=='#' and ('....'==M[h][w+1]+M[h][w-1]+M[h+1][w]+M[h-1][w]):
            print('No')
            exit()

print('Yes')

Submission status: Accepted

Solution code:
H, W = map(int,input().split())
grid = ['.'*(W+2)]
for _ in range(H):
    tmp = input()
    tmp = '.' + tmp + '.'
    grid.append(tmp)
grid.append('.'*(W+2))
for i in range(1,H+1):
    for k in range(1,W+1):
        if grid[i][k] == '.':
            continue
        if grid[i][k+1] != '#' and grid[i][k-1] != '#':
            if grid[i+1][k] != '#' and grid[i-1][k] != '#':
                print('No')
                exit()
print('Yes')

Submission status: Accepted

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

L=[]
l=["." for i in range(W+2)]
L.append(l)
for i in range(H):
    a=list(input())
    a=["."]+a+["."]
    L.append(a)
L.append(l)
#print(L)

X=[1,-1,0,0]
Y=[0,0,1,-1]

for i in range(1,H+1):
    for j in range(1,W+1):
        if L[i][j]=="#":
            F=1
            for k in range(4):
                if L[i+X[k]][j+Y[k]]=="#":
                    F=0
            if F==1:
                print("No")
                exit()
print("Yes")

Submission status: Accepted

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

Submission status: Accepted

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

s = []
s.append("#" * (W+2))
for i in range(H):
    tmp = "#"
    tmp += input()
    tmp += "#"
    s.append(tmp)
s.append("#" * (W+2))

flag = True
for i in range(1, len(s)-1):
    for j in range(1, W+1):
        if s[i][j] == "#":
            if (s[i][j-1] == "." and s[i][j+1] == "." 
                and s[i-1][j] == "." and s[i+1][j] == "."):
                flag = False
                break
            
if flag:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
from sys import stdin
import numpy as np

h,w = [int(x) for x in stdin.readline().rstrip().split()]
li = [["." for i in range(w+2)]for j in range(h+2)]

for i in range(h):
    s = stdin.readline().rstrip()
    for j in range(w):
        li[i+1][j+1] = s[j]

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

Submission status: Accepted

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

s = [input() for _ in range(h)]

ans = 'Yes'

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

print(ans)

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
S=['.'+input()+'.' for i in range(H)]
S.append('.'*(W+2))
S.insert(0,'.'*(W+2))
F=1

for i in range(H+2):
    for j in range(W+2):
        if S[i][j]=='#' and S[i+1][j]==S[i-1][j]==S[i][j+1]==S[i][j-1]=='.':
            F=0
            break

print(['No','Yes'][F])

Submission status: Accepted

Solution code:
m,n=map(int,input().split())
hoge=[[0 for i in range(n+2)] for j in range(m+2)]
for i in range(m):
  sss=input()
  for j in range(n):
    if(sss[j]=="#"):
      hoge[1+i][1+j]=1
    

flag=True
for i in range(m):
  for j in range(n):
    if(hoge[1+i][1+j]==1):
      ans=0
      #print(1+i,1+j,ans,hoge[i][1+j],hoge[1+i][j],hoge[2+i][1+j],hoge[1+i][2+j])
      ans=max(hoge[i][1+j],hoge[1+i][j],hoge[2+i][1+j],hoge[1+i][2+j])
      if(ans==0):
        flag=False
        
if(flag):
  print("Yes")
else:
  print("No")

Submission status: Accepted

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

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

for i in range(h):
    for j in range(w):
        if s[i][j]=="#":
            if check(i, j, w, h, s):
                True
            else:
                print("No")
                exit()

print("Yes")

Submission status: Accepted

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


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

for i in range(h):
    for j in range(w):
        if (s[i][j] == "#"):
            if check(i, j, w, h, s):
                True
            else:
                print("No")
                exit()
print("Yes")

Submission status: Accepted

Solution code:

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

h,w = list(map(int, input().split()))
s = [list(input()) for _ in range(h)]
for i in range(h):
  for j in range(w):
    if(s[i][j] == "#"):
      if(check(i, j, w, h, s)):
        True
      else:
        print("No")
        exit()
print("Yes")

Submission status: Accepted

Solution code:
# C - Grid Repainting 2
H, W = map(int,input().split())
S = [list(str(input())) for i in range(H)]

for h in range(H):
    for w in range(W):
        if S[h][w] == '#':
            tmp = 0
            if w != W-1:
                if S[h][w+1] == '#':
                    tmp = 1
            if h != 0:
                if S[h-1][w] =='#':
                    tmp = 1
            if h != H-1:
                if S[h + 1][w] == '#':
                    tmp = 1
            if w != 0:
                if S[h][w - 1] == '#' and w != 0:
                    tmp = 1
            if tmp == 0:
                print('No')
                exit()
print('Yes')

Submission status: Accepted

Solution code:
def get_neighbors(r, c):
    if 0 < r:
        yield r - 1, c
    if r < h - 1:
        yield r + 1, c
    if 0 < c:
        yield r, c - 1
    if c < w - 1:
        yield r, c + 1


def solve(h, w, s):
    for r in range(h):
        for c in range(w):
            if s[r][c] == '#':
                if all(s[nr][nc] == '.' for nr, nc in get_neighbors(r, c)):
                    return False
    return True


if __name__ == '__main__':
    h, w = map(int, input().split())
    s = [input() for _ in range(h)]

    print('Yes' if solve(h, w, s) else 'No')

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

H, W = [int(x) for x in input().strip().split()]
S = [list(input().strip()) for _ in range(H)]
C = [['.'] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        if S[i][j] == '#' and C[i][j] == '.':
            for i2, j2 in zip([1, 0, -1, 0],[0, 1, 0, -1]):
                if i+i2 < 0 or i+i2 >= H or j+j2 < 0 or j+j2 >= W:
                    continue
                if S[i+i2][j+j2] == '#':
                    C[i][j] = C[i+i2][j+j2] = '#'
                    break
            else:
                print('No')
                exit()

print('Yes')

Submission status: Accepted

Solution code:
H, W = list(map(lambda x: int(x), input().split(' ')))
table = [[c for c in input()] for _ in range(H)]

for i in range(H):
    for j in range(W):
        if table[i][j] == '#':
            if i > 0 and table[i-1][j] == '#':
                continue

            elif i < H-1 and table[i+1][j] == '#':
                continue

            elif j > 0 and table[i][j-1] == '#':
                continue

            elif j < W-1 and table[i][j+1] == '#':
                continue

            else:
                print('No')
                exit()

print('Yes')

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
L=[["."]+list(input())+["."] for _ in range(H)]
l=[["." for i in range(W+2)]]
L=l+L+l
A=[-1,1,0,0]
B=[0,0,-1,1]
n=True
for i in range(1,H+1):
  for j in range(1,W+1):
    if L[i][j]=="#":
      if all(L[i+A[k]][j+B[k]]!="#" for k in range(4)):
        n=False
        break
  else:
    continue
  break
if n:
  print("Yes")
else:
  print("No")

Submission status: Accepted

Solution code:
#                         author:  kagemeka 
#                         created: 2019-11-07 13:09:56(JST)
import sys
# import collections
# import math
# import string
# import bisect
# import re
# import itertools
# import statistics
# import functools
# import operator

def main():
    h, w = (int(x) for x in sys.stdin.readline().split())
    canvas = ['.' * (w + 2)]
    for i in range(h):
        line = '.' + sys.stdin.readline().rstrip() + '.'
        canvas.append(line)
    canvas.append('.' * (w + 2))
    for i in range(1, h+1):
        for j in range(1, w+1):
            if canvas[i][j] == '.':
                continue
            else:
                if canvas[i-1][j] == '#' or canvas[i][j-1] == '#' or canvas[i][j+1] == '#' or canvas[i+1][j] == '#':
                    continue
                else:
                    print('No')
                    exit()
    print('Yes')


if __name__ == "__main__":
    # execute only if run as a script
    main()

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
X = [[[0, 1][a=="#"] for a in input()]+[0] for i in range(H)] + [[0] * (W+1)]
for i in range(H):
    for j in range(W):
        if X[i][j] == 1 and X[i][j-1] == X[i-1][j] == X[i][j+1] == X[i+1][j] == 0:
            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 = map(int,input().split())
s = []
for i in range(h):s.append(list(input()))
dx = [-1, 0, 1, 0]
dy = [0, 1, 0,-1]
for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            flag=False
            for k in range(4):
                x = j+dx[k];y=i+dy[k]
                if 0 <= x and x <= w-1 and 0 <= y and y <= h-1:
                    if s[y][x]=="#":flag=True
            if not flag:
                print('No')
                exit()
print('Yes')

Submission status: Accepted

Solution code:
from sys import exit
H, W = map(int, input().split())
slist = [["."] * (W + 2), ]
for _ in range(H):
    line = list(input())
    line.insert(0, ".")
    line.append(".")
    slist.append(line)
slist.append(["."] * (W + 2))
points = ((0, -1), (0, 1), (-1, 0), (1, 0))
flg = True
for i in range(1, H + 1):
    for j in range(1, W + 1):
        if slist[i][j] == "#":
            counter = 0
            for x, y in points:
                if slist[i - x][j - y] == "#":
                    counter += 1
                    break
            else:
                print("No")
                exit()
print("Yes")

Submission status: Accepted

Solution code:
def dfs(grid, start, wall, w, h):
    dxy = [(0,1), (0,-1), (1,0), (-1,0)]
    for i in range(1,h+1):
        for j in range(1,w+1):
            if grid[i][j] == '.':
                continue
            if sum([grid[i+dy][j+dx] == wall  for dx,dy in dxy]) == 0:
                return False
    return True

h,w = map(int, input().split())
wall = '.'
edges = [wall] * (w+2)
grid = [edges]
for i in range(h):
    s = input()
    grid.append([wall] + list(s) + [wall])
grid.append(edges)
print('Yes' if dfs(grid, [0,0], '#', w, h) else 'No')

Submission status: Accepted

Solution code:
import sys
import collections


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7

    h, w = list(map(int, input().rstrip('\n').split()))
    s = [list(str(input().rstrip('\n'))) for _ in range(h)]
    ql = []
    ql = collections.deque(ql)
    for i in range(h):
        for j in range(w):
            if s[i][j] == "#":
                ql.append([0, i, j])
    while True:
        if len(ql) != 0:
            b = False
            cost, xv, yv = ql.popleft()
            for xv, yv in [[xv + 1, yv], [xv - 1, yv], [xv, yv + 1], [xv, yv - 1]]:
                if 0 <= xv < h and 0 <= yv < w:
                    if s[xv][yv] == "#":
                        b = True
                        break
            if not b:
                print("No")
                exit()
        else:
            break
    print("Yes")


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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

for i in range(h):
  for j in range(w):
    if grid[i][j]=='#':
      isFillable = False
      for k in range(4):
        x = j + dx[k]
        y = i + dy[k]
        if 0 <= x and x <= w-1 and 0 <= y and y <= h-1:
          if grid[y][x] == "#":
            isFillable = True
      if not isFillable:
        print('No')
        exit()

print('Yes')

Submission status: Accepted

Solution code:
h, w = list(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())
campus = [list(input()) for h in range(H)]

ans = 1
for h in range(H):
    for w in range(W):
        if campus[h][w] == '#':
            check = 0
            if h != 0:
                if campus[h-1][w] == '#':
                    check = 1
            if w != 0:
                if campus[h][w-1] == '#':
                    check = 1
            if h != H-1:
                if campus[h+1][w] == '#':
                    check = 1
            if w != W-1:
                if campus[h][w+1] == '#':
                    check =1
            if check == 0:
                ans = 0
                break

if ans == 1:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
import numpy as np
h, w = map(int, input().rstrip("\n").split())

array = np.array([[" "] * (w + 2)] + [list(" " + input().rstrip("\n") + " ") for _ in range(h)] + [[" "] * (w + 2)])

def solve():
    for i in range(1, h + 1):
        for j in range(1, w + 1):
            if array[i][j] == "#":
                if "#" in array[i - 1][j] + array[i + 1][j] + array[i][j - 1] + array[i][j + 1]:
                    continue
                else:
                    return "No"
    return "Yes"

print(solve())

Submission status: Accepted

Solution code:
def DFS(x,y): # h*wの配列logの深さ優先探索
    global h,w,maze,log
    if not (x<0 or h<=x or y<0 or w<=y):
        if (y!=0 and maze[x][y-1]=="#") or (y!=w-1 and maze[x][y+1]=="#") or (x!=0 and maze[x-1][y]=="#") or (x!=h-1 and maze[x+1][y]=="#"):
            log[x][y]=1
    

def main():
    n,m = map(int,input().split())
    global h,w,maze,log
    maze=[]
    for _ in range(n):
        maze.append(list(input()))
    log = [[0 for _ in range(m)] for _ in range(n)]
    for i in range(n):
        for j in range(m):
            if maze[i][j]==".":
                log[i][j]=1
    h=n
    w=m
    for i in range(n):
        for j in range(m):
            if log[i][j]==0:
                DFS(i,j)
    for i in range(n):
        for j in range(m):
            if log[i][j]==0:
                print("No")
                return
    print("Yes")
    
main()

Submission status: Accepted

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

ans = 'Yes'
break_flg = False

for h_i in range(h):
  if break_flg:
    break
  for w_i in range(w):
    check_str = target[h_i][w_i]
    if check_str == '#':
      left = target[h_i][w_i-1] if w_i-1>=0 else '.'
      right = target[h_i][w_i+1] if w_i+1<w else '.'
      top = target[h_i-1][w_i] if h_i-1>=0 else '.'
      bottom = target[h_i+1][w_i] if h_i+1<h else '.'
      
      if left==right==top==bottom=='.':
        ans = 'No'
        break_flg = True
        break
        
print(ans)

Submission status: Accepted

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

g = ["." + input() + "." for _ in range(H)]
g = ["." * (W + 2)] + g + ["." * (W + 2)]

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
mat=[]
dx=[0,0,-1,1]
dy=[1,-1,0,0]
for i in range(n):
    mat.append(list(input().strip()))
f=0 
for i in range(n):
    for j in range(m):
        if mat[i][j]=='#':
            f=0 
            for k in range(4): 
                if 0<=i+dx[k]<n and 0<=j+dy[k]<m and mat[i+dx[k]][j+dy[k]]=='#': 
                    f=1  
            if not f:
                print('No')
                exit()
print('Yes')

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
able = True
tonari = [(-1, 0), (0, -1), (1, 0), (0, 1)]
s = []
for _ in range(h):
    s.append(input())
for i in range(h):
    for j in range(w):
        if s[i][j] == '#' and able:
            count = 0
            for tx, ty in tonari:
                y = i + ty
                x = j + tx
                if 0 <= y < h and 0 <= x < w:
                    if s[y][x] == '#':
                        count += 1
            if count == 0:
                able = False
                break

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