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 = [input() for i in range(h)]
up = down = right = left = '.'
for x in range(h):
    for y in range(w):
        if(list(s[x])[y] == '#'):
            if(y != w-1):
                right = list(s[x])[y+1]
            if(x != h-1):
                down = list(s[x+1])[y]
            if(x != 0):
                up = list(s[x-1])[y]
            if(y != 0):
                left = list(s[x])[y-1]
            if(up != '#' and down != '#' and right != '#' and left != '#'):
                print("No")
                exit()
            up = down = right = left = '.'
print("Yes")

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
s = [input() for _ in range(H)]
if H==1 and W==1:
    if s[0][0] == '.':print('Yes')
    else:print('No')
else:
    island = False
    for i in range(H):
        for j in range(W):
            if s[i][j] == '#':
                bis = False
                if i-1>=0:
                    if s[i-1][j] == '#':bis = True
                if i+1<H:
                    if s[i+1][j] == '#':bis = True
                if j-1>=0:
                    if s[i][j-1] == '#':bis = True
                if j+1<W:
                    if s[i][j+1] == '#':bis = True
                if bis == False:
                    island = True
    if island:
        print('No')
    else:
        print('Yes')

Submission status: Accepted

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

def find_black(x,y):
    m_x = [ 0, 1,0,-1]
    m_y = [-1,0,1,0]
    #print("{0},{1}: come".format(x, y))

    for ii in range(len(m_x)):
        nx, ny = (x + m_x[ii]), (y + m_y[ii])
        #print("is black:{0},{1} = # {2},{3}".format(nx, ny, w,h ))
        if (0 <= nx < w) and (0 <= ny < h):
            if dat[ny][nx] == "#":
                #print("YES")
                return True
    return False

c = True
for i in range(h):
    for j in range(w):
        if dat[i][j] == "#":
            #print("{0},{1} = #".format(j, i))
            if find_black(j, i) is False:
                c = False

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

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
 
lst = ["." + input() + "." for _ in range(h)]
matrix = ["." * (w + 2)] + lst + ["." * (w + 2)]
 
 
ans = "Yes"
for i in range(1, h + 1):
    for j in range(1, w + 1):
        center = matrix[i][j]
        up = matrix[i - 1][j]
        down = matrix[i + 1][j]
        left = matrix[i][j - 1]
        right = matrix[i][j + 1]
        if center == "#" and up == down == left == right == ".":
            ans = "No"
print(ans)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
s = [input() for i in range(H)]
dy, dx = [1, 0, -1, 0], [0, 1, 0, -1]
for i in range(H):
    for j in range(W):
        if s[i][j] == '.': continue
        f = 1
        for d1, d2 in zip(dy, dx):
            ni, nj = i + d1, j + d2
            if 0 <= ni < H and 0 <= nj < W and s[ni][nj] == '#':
                f = 0
                break
        if f:
            print('No')
            quit()
print('Yes')

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

import sys

sys.setrecursionlimit(10**7)


def read_h(typ=int):
    return list(map(typ, input().split()))


def read_v(n, m=1, typ=int):
    return [read_h(typ) if m > 1 else typ(input()) for _ in range(n)]


def paintable(H, W, S, i, j):
    return (i - 1 >= 0 and S[i - 1][j] == '#') or (i + 1 <= H - 1 and S[i + 1][j] == '#') or (
        j - 1 >= 0 and S[i][j - 1] == '#') or (j + 1 <= W - 1 and S[i][j + 1] == '#')


def main():
    H, W = read_h()
    S = read_v(H, typ=str)

    for i, row in enumerate(S):
        for j, c in enumerate(row):
            if c == '.':
                continue

            if not paintable(H, W, S, i, j):
                print('No')
                return

    print('Yes')


if __name__ == '__main__':
    main()

Submission status: Accepted

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

mp = []
for i in range(h):
    tmp = list(input())
    tml = []
    for j in range(w):
        tml.append(0 if tmp[j] == "." else 1)
    mp.append(tml)
    
#print(mp)

flg = 0
for i in range(1,h-1):
    for j in range(1,w-1):
        if((mp[i][j] == 1) and (mp[i-1][j]+mp[i+1][j]+mp[i][j-1]+mp[i][j+1] == 0)):
            flg = 1
 #           print("{}, {}".format(i,j))
            break
        
ans = ["Yes","No"]
		
print(ans[flg])

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
field=["."*(w+2)]+["."+input()+"." for i in range(h)]+["."*(w+2)];flag=0
for i in range(1,h+1):
  for j in range(1,w+1):
    if field[i][j]=="#":
      if "#" not in [field[i-1][j],field[i+1][j],field[i][j-1],field[i][j+1]]:
        flag=1
        break
if flag==0:print("Yes")
else:print("No")

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
C = [list(input()) for i in range(H)]
X = [1,0,-1,0]
Y = [0,-1,0,1]
for y in range(H):
  for x in range(W):
    check = 0
    if C[y][x] == "#":
      for i in range(4):
        nx = x + X[i]
        ny = y + Y[i]
        if 0 <= nx < W and 0 <= ny < H and C[ny][nx] == "#":
          check = 1
          break
      if check == 0:
        print("No")
        exit()
print("Yes")

Submission status: Accepted

Solution code:
row = list(map(int, input().split()))
s = [input().split() for i in range(row[0])]
for i in range(row[0]):
    s[i] = list(s[i][0])
dx = [0, -1, 1, 0]
dy = [-1, 0, 0, 1]
Flag = True
for i in range(row[0]):
    for j in range(row[1]):
        if s[i][j] == "#":
            flag = False
            for d in range(4):
                nx = i + dx[d]
                ny = j + dy[d]
                if 0 <= nx < row[0] and 0<= ny < row[1]:
                    if s[nx][ny] == "#":
                        flag = True
                        break
            if flag == False:
                Flag = False
                break
    if Flag == False:
        print("No")
        break
if Flag:
    print("Yes")

Submission status: Accepted

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

S = []
for i in range(H):
	S.append("." + input() + ".")

S = ["."*(W+2)] + S + ["."*(W+2)]
flag = 1
for i in range(H):
	for j in range(W):
		if S[i][j]=="#":
			if S[i-1][j]=="." and S[i+1][j]=="." and S[i][j-1]=="." and S[i][j+1]==".":
				flag = 0
print("Yes" if flag else "No")

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
board = []
for i in range(0, H):
	board.append(input())
ans = 'Yes'
for i in range(0, H):
	for j in range(0, W):
		if board[i][j] == '#':
			flag = True
			for k in range(0, 2):
				nexth = i - 1 + 2*k
				nextw = j - 1 + 2*k
				if 0 <= nexth and nexth < H:
					if board[nexth][j] == '#':
						flag = False
						break
				if 0 <= nextw and nextw < W:
					if board[i][nextw] == '#':
						flag = False
						break
			if flag:
				ans = 'No'
				break
	if ans == 'No':
		break
print(ans)

Submission status: Accepted

Solution code:
def f(i, S, H, W):
    indices = [i - W, i - 1, i + 1, i + W]
    indices = [i for i in indices if 0 <= i < H * W]
    return any([S[i] == "#" for i in indices])


H, W = map(int, input().split())
S = ""
for _ in range(H):
    S += input()
for i in range(H * W):
    if S[i] == "#":
        if not f(i, S, H, W):
            print("No")
            break
else:
    print("Yes")

Submission status: Accepted

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

for h in range(H):
  for w in range(W):
    if G[h][w] == '.':
      continue
    if h == 0:
      if w == 0:
        right = G[h][w+1]
        down = G[h+1][w]
        if right == '.' and down == '.':
          print('No')
          exit()
      elif w == W-1:
        left = G[h][w-1]
        down = G[h+1][w]
        if left == '.' and down == '.':
          print('No')
          exit()
      else:
        right = G[h][w+1]
        left = G[h][w-1]
        down = G[h+1][w]
        if right == '.' and left == '.' and down == '.':
            print('No')
            exit()
    elif h == H-1:
      if w == 0:
        right = G[h][w+1]
        up = G[h-1][w]
        if right == '.' and up == '.':
          print('No')
          exit()
      elif w == W-1:
        left = G[h][w-1]
        up = G[h-1][w]
        if left == '.' and up == '.':
          print('No')
          exit()
      else:
        right = G[h][w+1]
        left = G[h][w-1]
        up = G[h-1][w]
        if right == '.' and left == '.' and up == '.':
            print('No')
            exit()
    else:
      if w == 0:
        right = G[h][w+1]
        up = G[h-1][w]
        down = G[h+1]
        if right == '.' and up == '.' and down == '.':
          print('No')
          exit()
      elif w == W-1:
        left = G[h][w-1]
        up = G[h+1][w]
        down = G[h-1][w]
        if left == '.' and up == '.' and down=='.' :
          print('No11')
          exit()
      else:
        right = G[h][w+1]
        left = G[h][w-1]
        up = G[h+1][w]
        down = G[h-1][w]
        if right == '.' and left == '.' and up == '.' and down == '.':
            print('No')
            exit()
print('Yes')

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = ['.'*(w+2)] + ['.' + input() + '.' for i in range(h)] + ['.'*(w+2)]
d = [[1, 0], [0, 1], [-1, 0], [0, -1]]

ans = 0
for i in range(1, h+1):
    for j in range(1, w+1):
        if s[i][j] == '.':
            continue
        c = 0
        for k in range(4):
            if s[i+d[k][0]][j+d[k][1]] == "#":
                c += 1
        if c == 0:
            ans = -1

if ans == -1:
    print("No")
else:
    print("Yes")

Submission status: Accepted

Solution code:
H, W = map(int,input().split())
S = [input() for i in range(H)]
S = ["."*(W)]+S+["."*(W)]
S = ["."+s+"." for s in S]
ans = True
for h in range(1,H+1):
    if ans == False:
        break
    for w in range(1,W+1):
        if S[h][w]=="#":
            if not(S[h-1][w]=="#" or S[h+1][w]=="#" or S[h][w+1]=="#" or S[h][w-1]=="#"):
                ans = False
                break
print("Yes" if ans else "No")

Submission status: Accepted

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

def solve(S):
    for i in range(1,h+1): #i = 1,2,..,hについて処理
        for j in range(1,w+1): #j = 1,2,...,wについて処理
            if S[i][j] == "#":
                if S[i-1][j] == ("#" or "a") or S[i+1][j] == ("#" or "a") or S[i][j-1] == ("#" or "a") or S[i][j+1] == ("#" or "a"):
                    S[i][j] == "a"
                else:
                    return False
    return True

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

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
S = [["." for i in range(w+2)]]
S += [["."] + list(input()) + ["."] for i in range(h)]
S += [["." for i in range(w+2)]]
ans = False
for i in range(1,h+1): #i = 1,2,..,hについて処理
    for j in range(1,w+1): #j = 1,2,...,wについて処理
        if S[i][j] == "#":
            if S[i-1][j] == ("#" or "a") or S[i+1][j] == ("#" or "a") or S[i][j-1] == ("#" or "a") or S[i][j+1] == ("#" or "a"):
                S[i][j] == "a"
            else:
                break
    else:
        continue
    #print(S)
    print("No")
    break
else:
    print("Yes")

Submission status: Accepted

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

dict = (
    (-1, 0),
    (0, -1),
    (0, 1),
    (1, 0),
)
achievable = True

for i in range(h):
    for j in range(w):
        count = 0
        if grid[i][j] == "#":
            for dy, dx in dict:
                y = i + dy
                x = j + dx
                if y >= 0 and h > y and x >= 0 and w > x:
                    if grid[y][x] == '#':
                        count += 1
            if count == 0:
                achievable = False

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

Submission status: Accepted

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

achievable = True

for i in range(h):
    for j in range(w):
        x = grid[i][j]
        
        if x == '#':
            count = 0
            if i - 1 >= 0 and grid[i - 1][j] == '#':
                count += 1
            if i + 1 < h and grid[i + 1][j] == '#':
                count += 1
            if j - 1 >= 0 and grid[i][j - 1] == '#':
                count += 1
            if j + 1 < w and grid[i][j + 1] == '#':
                count += 1
            
            if count == 0:
                achievable = False
                
print('Yes' if achievable else 'No')

Submission status: Accepted

Solution code:
import sys
H, W = map(int, input().split())
coordinates = [[0]] * (H + 2)
coordinates[0] = ["."] * (W + 2)
coordinates[H + 1] = ["."] * (W + 2)

for i in range(1, H + 1):
    coordinates[i] = ['.'] + [i for i in input()] + ['.']

for j in range(1, H + 1):
    for k in range(1, W + 1):
        if coordinates[j][k] == '#':
            if coordinates[j - 1][k] == '#' or coordinates[j][k - 1] == '#' or coordinates[j + 1][k] == '#' or coordinates[j][k + 1] == '#':
                continue
            else:
                print('No')
                sys.exit()
print('Yes')

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
grid = [list(input()) for i in range(h)]
error = False
for i in range(h):
    for j in range(w):
        if grid[i][j] == "#":
            if i > 0 and grid[i-1][j] == "B":
                grid[i][j] = "B"
            elif j > 0 and grid[i][j-1] == "B":
                grid[i][j] = "B"
            elif i < h-1 and (grid[i+1][j] == "B" or grid[i+1][j] == "#"):
                grid[i][j] = "B"
                grid[i+1][j] = "B"
            elif j < w-1 and (grid[i][j+1] == "B" or grid[i][j+1] == "#"):
                grid[i][j] = "B"
                grid[i][j+1] = "B"
            else:
                error = True
                break
    if error == True:
        break
if error == True:
    print("No")
else:
    print("Yes")

Submission status: Accepted

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

for i in range(h):
    for j in range(w):
        if pic[i][j] == "#":
            # 上下左右の順に判定
            # １つでもあれば次へ
            if ((i-1 >= 0 and pic[i-1][j] == "#")
                or 
                (i+1 < h and pic[i+1][j] == "#")
                or
                (j-1 >= 0 and pic[i][j-1] == "#")
                or
                (j+1 < w and pic[i][j+1] == "#") ):
                continue
            else:
                print("No")
                exit()

print("Yes")

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
check=[[0 for _ in range(W+2)] for _ in range(H+2)]
canvas=[['.' for _ in range(W+2)] for _ in range(H+2)]
for i in range(H):
    s=list(input())
    for j in range(W):
        canvas[i+1][j+1]=s[j]

for i in range(H):
    for j in range(W):
        if canvas[i+1][j+1]=='#':
            if canvas[i+1][j]=='#' \
            or canvas[i+1][j+2]=='#' \
            or canvas[i][j+1]=='#' \
            or canvas[i+2][j+1]=='#' :
                check[i+1][j+1]=1
ans='Yes'
for i in range(H):
    for j in range(W):
        if canvas[i+1][j+1]=='#':
            if check[i+1][j+1]==0:
                ans='No'
                break
            else:
                continue
            break
print(ans)

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
Pic = [[i=="." for i in input()] for j in range(H)]
ans = "Yes"
for h,row in enumerate(Pic):
    for w,p in enumerate(row):
        if p:continue
        if all([
            h==0 or Pic[h-1][w],
            h==H-1 or Pic[h+1][w],
            w==0 or Pic[h][w-1],
            w==W-1 or Pic[h][w+1]
            ]):ans = "No"
print(ans)

Submission status: Accepted

Solution code:
def resolve():
	h,w=map(int,input().split())
	s=[input() for i in range(h)]
	kekka='Yes'
	for i in range(h):
		for j in range(w):
			if s[i][j]=='#':
				ans=0
				if j==0:
					ans+=s[i][1]=='#'
				elif j==w-1:
					ans+=s[i][-2]=='#'
				else:
					ans+=s[i][j-1]=='#' 
					ans+=s[i][j+1]=='#'
					
				if i==0:
					ans+=s[1][j]=='#'
				elif i==h-1:
					ans+=s[-2][j]=='#'
				else:
					ans+=s[i-1][j]=='#' 
					ans+=s[i+1][j]=='#'
				if ans==0:
					kekka='No'
					break
		else:
			continue
		break
	print(kekka)
resolve()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10000)
h, w = map(int, input().split())
cam = [input() for _ in range(h)]
painted = [[False for i in range(w)] for j in range(h)]

def dfs(x, y):
    global cnt
    if x < 0 or y < 0 or x > h-1 or y > w-1:
        return
    elif cam[x][y] == ".":
        return
    elif painted[x][y]:
        return
    cnt += 1
    painted[x][y] = True
    dfs(x-1, y)
    dfs(x, y-1)
    dfs(x+1, y)
    dfs(x, y+1)
    
for i in range(h):
    for j in range(w):
        cnt = 0
        if cam[i][j] == "#":
            dfs(i, j)
        if cnt == 1:
            painted[i][j] = False
            
flag = False
for i in range(h):
    for j in range(w):
        if cam[i][j] == "#":
            if not painted[i][j]:
                flag = True
                break
        elif cam[i][j] == ".":
            if painted[i][j]:
                flag = True
                break
    if flag:
        break
if flag:
    print("No")
else:
    print("Yes")

Submission status: Accepted

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

lst = ["." + input() + "." for _ in range(h)]
matrix = ["." * (w + 2)] + lst + ["." * (w + 2)]


ans = "Yes"
for i in range(1, h + 1):
    for j in range(1, w + 1):
        center = matrix[i][j]
        up = matrix[i - 1][j]
        down = matrix[i + 1][j]
        left = matrix[i][j - 1]
        right = matrix[i][j + 1]
        if center == "#" and up == "." and down == "." and left == "." and right == ".":
            ans = "No"
print(ans)

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
a = [[0 for i in range(w+2)] for j in range(h+2)]
for i in range(1, h+1):
  s = input()
  for j in range(1, w+1):
    if ord(s[j-1]) is 35:
      a[i][j] = 1

for i in range(1, h+1):
  for j in range(1, w+1):
    if a[i][j] is 1:
      if a[i-1][j] is 0 and a[i+1][j] is 0 and a[i][j-1] is 0 and a[i][j+1] is 0:
        print('No')
        exit()
print('Yes')

Submission status: Accepted

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

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

ans = "Yes"

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

flg1 = 0
flg2 = 0

for y in range(H) :
    if flg1 : break
    for x in range(W) :
        if flg2 : break
        if L[y][x] == "#" :
            flg3 = 0
        
            for dy,dx in dxy :


                ny = y+dy
                nx = x+dx

                if (ny < 0) or (ny >= H) or (nx < 0) or (nx >= W) :
                    continue

                if L[ny][nx] == "#" :
                    flg3 = 1
                    break
            
            if not flg3 :
                ans = "No"
                flg1 = 1
                flg2 = 1
                flg3 = 1
                break
print(ans)

Submission status: Accepted

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

line_list = []

for i in range(H):
    line = input()
    line_list.append(line)

flag = True
for i in range(len(line_list)):
    for j in range(len(line_list[i])):
        if flag == False:
            break
        if line_list[i][j] == "#":
            if i == 0:
                if j == 0:
                    if "#" not in [line_list[i + 1][j], line_list[i][j + 1]]:
                        flag = False
                elif j == W - 1:
                    if "#" not in [line_list[i + 1][j], line_list[i][j - 1]]:
                        flag = False
                else:
                    if "#" not in [line_list[i + 1][j], line_list[i][j - 1], line_list[i][j + 1]]:
                        flag = False
            elif i == H - 1:
                if j == 0:
                    if "#" not in [line_list[i - 1][j], line_list[i][j + 1]]:
                        flag = False
                elif j == W - 1:
                    if "#" not in [line_list[i - 1][j], line_list[i][j - 1]]:
                        flag = False
                else:
                    if "#" not in [line_list[i - 1][j], line_list[i][j - 1], line_list[i][j + 1]]:
                        flag = False
            else:
                if j == 0:
                    if "#" not in [line_list[i - 1][j], line_list[i + 1][j], line_list[i][j + 1]]:
                        flag = False
                elif j == W - 1:
                    if "#" not in [line_list[i - 1][j], line_list[i + 1][j], line_list[i][j - 1]]:
                        flag = False
                else:
                    if "#" not in [line_list[i - 1][j], line_list[i + 1][j], line_list[i][j - 1], line_list[i][j + 1]]:
                        flag = False

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

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
ss=[list(input()) for i in range(h)]
f=True
s=[["."]*(w+2)]
s.extend(ss)
for i in range(1,h+1):
    s[i].insert(0,".")
    s[i].append(".")
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+1][j]!="#" and s[i][j-1]!="#" and s[i][j+1]!="#":
                f=False
if f:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
a=["."*(w+2)]
for i in range(h):
    arr = "."+str(input())+"."
    a.append(arr)
a.append("."*(w+2))

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

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
s=[]
for _ in range(h):
  s.append(str(input()))
for i in range(h):
  for j in range(w):
    flag=False
    if s[i][j]!="#":
      continue
    for k in [-1,1]:
      if 0<=i+k<h:
        if s[i+k][j]=="#":
          flag=True
      if 0<=j+k<w:
        if s[i][j+k]=="#":
          flag=True
    if not flag:
      print("No")
      exit()
print("Yes")

Submission status: Accepted

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

for i in range(H):
    for j in range(W-1):
        if S[i][j]!='.' and S[i][j+1]!='.':
            S[i][j]='x'
            S[i][j+1]='x'

for j,s in enumerate(zip(*S)):
    for i in range(H-1):
        if s[i]!='.' and s[i+1]!='.':
            S[i][j]='x'
            S[i+1][j]='x'

for i in range(H):
    for j in range(W):
        if S[i][j]=='#':
            print('No')
            exit()
print('Yes')

Submission status: Accepted

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

dset=((-1,0),(0,1),(1,0),(0,-1))

for i in range(1,h+1):
    for j in range(1,w+2):
        if s[i][j]=='.':continue
        flag=False
        for y,x in dset:
            if s[i+y][j+x]=='#':
                flag=True
                break
        if not flag:
            print('No')
            exit()
print('Yes')

Submission status: Accepted

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

dx = [-1, 0, 0, 1]
dy = [0, 1, -1, 0]
for i in range(H):
    for j in range(W):
        flag = False
        for k in range(4):
            if s[i][j] == '#':
                nx = j + dx[k]
                ny = i + dy[k]
                if nx < 0 or nx >= W or ny < 0 or ny >= H:
                    continue
                if s[ny][nx] == '#':
                    flag = True
            else:
                flag = True
        if flag == False:
            print('No')
            exit()        

print('Yes')

Submission status: Accepted

Solution code:
nl = lambda: list(map(int, input().split()))
sl = lambda: input().split()
n = lambda: int(input())
s = lambda: input()

H, W = nl()

mas = []
for i in range(H):
    mas.append(s())

for i in range(H):
    for j in range(W):
        if mas[i][j] == '#':
            if (i > 0 and mas[i-1][j] == '#') or \
               (i < H-1 and mas[i+1][j] == '#') or \
               (j > 0 and mas[i][j-1] == '#') or \
               (j < W-1 and mas[i][j+1] == '#'):
                pass
            else:
                print('No')
                import sys
                sys.exit()

print('Yes')

Submission status: Accepted

Solution code:

H,W = map(int,input().split())

lis = []

for i in range(H):

    s = input()
    lis.append(s)

ans = "Yes"

for i in range(H):

    for j in range(W):

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

            flag = True

            if i != 0 and lis[i-1][j] == "#":
                flag = False

            if i != H-1 and lis[i+1][j] == "#":
                flag = False

            if j != 0 and lis[i][j-1] == "#":
                flag = False

            if j != W-1 and lis[i][j+1] == "#":
                flag = False


            if flag:
                ans = "No"
                break

print (ans)

Submission status: Accepted

Solution code:
def resolve():
    H, W = map(int, input().split())
    S = [list(input()) for __ in range(H)]

    dl = [(-1, 0), (0, 1), (1, 0), (0, -1)]
    for i, row in enumerate(S):
        for j, val in enumerate(row):
            if val == '.':
                continue

            can_paint = False
            for dx, dy in dl:
                di, dj = i + dx, j + dy
                if di < 0 or H <= di or dj < 0 or W <= dj:
                    continue
                if S[di][dj] == '#':
                    can_paint = True
                    break
            if not can_paint:
                return 'No'
    return 'Yes'

print(resolve())

Submission status: Accepted

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

flag=True
count=0
for i in range(h):
    for j in range(w):
        if s[i][j]=="#":
            if h==1:
                count+=1
            elif i!=0 and i!=h-1:
                if s[i-1][j]=="#" or s[i+1][j]=="#":
                    continue
            elif i==0:
                if s[i+1][j]=="#":
                    continue
            elif i==h-1:
                if s[i-1][j]=="#":
                    continue
            
            if w==1:
                count+=1
            elif j!=0 and j!=w-1:
                if s[i][j-1]=="#" or s[i][j+1]=="#":
                    continue
            elif j==0:
                if s[i][j+1]=="#":
                    continue
            elif j==w-1:
                if s[i][j-1]=="#":
                    continue
            flag=False

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

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
s = [input() for _ in range(H)]
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
def solve():
    for x in range(H):
        for y in range(W):
            if s[x][y] == '#':
                flag = True
                for k in range(4):
                    nx = x + dx[k]
                    ny = y + dy[k]
                    if 0 <= nx and nx < H and 0 <= ny and ny < W and s[nx][ny] == '#':
                        flag = False
                if flag:
                    return False
    return True
print("Yes" if solve() else "No")

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
S=[input()+'.'for _ in range(H)]+['.'*W]
print(('Yes','No')[any(S[y-1][x]==S[y+1][x]==S[y][x-1]==S[y][x+1]=='.'for y in range(H)for x in range(W)if S[y][x]=='#')])

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
S=[input()+'.'for _ in range(H)]+['.'*(W+2)]
print(('Yes','No')[any(S[y-1][x]==S[y+1][x]==S[y][x-1]==S[y][x+1]=='.'for y in range(H)for x in range(W)if S[y][x]=='#')])

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
f = ['.' * (w+2)]
for _ in range(h):
    f += ['.' + input() + '.']
f += ['.' * (w+2)]

for i in range(1, h+1):
    for j in range(1, w+1):
        if not f[i][j] == '#':
            continue
        else:
            if f[i][j-1]=='#' or f[i-1][j]=='#' or f[i+1][j]=='#' or f[i][j+1]=='#':
                pass
            else:
                print('No')
                exit()

print('Yes')

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
a=[0]*(h+2)
a[0]='.'*(w+2)
a[h+1]=a[0]
for _ in range(1,h+1):
  j=input()
  a[_]='.'+j+'.'
x='Yes'
for i in range(1,h+1):
  for j in range(1,w+1):
    if a[i][j]=='#':
      if a[i-1][j]=='.' and a[i+1][j]=='.' and a[i][j-1]=='.' and a[i][j+1]=='.':
        x='No'
        break
print(x)

Submission status: Accepted

Solution code:
# 孤立点

import numpy as np
import sys
buf = sys.stdin.buffer

H,W = map(int,buf.readline().split())
S = np.frombuffer(buf.read(H*(W+1)), dtype='S1').reshape((H,W+1))
S = S[:,:-1]

black = (S == b'#')
black_nbd = np.zeros((H,W), dtype=np.bool)

# 左
black_nbd[1:,:] |= black[:-1,:]
# 右
black_nbd[:-1,:] |= black[1:,:]
# 下
black_nbd[:,1:] |= black[:,:-1]
# 上
black_nbd[:,:-1] |= black[:,1:]

impossible = (black & ~black_nbd).any()
answer = 'Yes' if not impossible else 'No'
print(answer)

Submission status: Accepted

Solution code:
import sys

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

li = [[0] * (w+2)]  + [[0] + list(input()) + [0] for _ in range(h)] + [[0] * (w+2)]

for i in range(h+2):
    for j in range(w+2):
        if li[i][j] == "#":
            if li[i][j-1] == "#" or li[i][j+1] == "#" or li[i-1][j] == "#" or li[i+1][j] == "#":
                pass
            else:
                print("No")
                sys.exit()
else:
    print("Yes")